File Name: MainActivity.java
package com.example.abbu.spinner_image; import android.content.res.TypedArray; import android.graphics.Color; import android.support.v7.app.ActionBarActivity; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.Spinner; import android.widget.TextView; import android.widget.Toast; import org.w3c.dom.Text; public class MainActivity extends ActionBarActivity { private String[] Objects; private TypedArray images; private ImageView itemImage; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Casting Objects = getResources().getStringArray(R.array.object_array); images = getResources().obtainTypedArray(R.array.object_image); TextView spinnerHeader = (TextView)findViewById(R.id.textView); itemImage = (ImageView)findViewById(R.id.imageView); final Spinner spinner = (Spinner)findViewById(R.id.spinner); ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>( getApplicationContext(),android.R.layout.simple_spinner_item, Objects); spinnerAdapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(spinnerAdapter); spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { itemImage.setImageResource(images.getResourceId( spinner.getSelectedItemPosition(), -1)); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); } }
File Name: activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.abbu.spinner_image.MainActivity"> <TextView android:text="@string/option_select" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginLeft="20dp" android:layout_marginStart="55dp" android:textSize="16sp" android:textStyle="bold" android:layout_marginTop="27dp" android:id="@+id/textView" /> <Spinner android:id="@+id/spinner" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginRight="20dp" android:layout_below="@+id/textView" android:layout_alignLeft="@+id/textView"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView" android:layout_below="@+id/spinner" android:layout_alignLeft="@+id/spinner" android:layout_alignStart="@+id/spinner" android:layout_marginTop="55dp" /> </RelativeLayout>File Name: strings.xml
<resources> <string name="app_name">Spinner_Image</string> <string name="option_select">Choose any one:</string> <string-array name="object_array"> <item>Shah Rukh Khan</item> <item>Hrithik Roshan</item> <item>Leonardo DiCaprio</item> <item>Bryan Cranston</item> </string-array> <integer-array name="object_image"> <item>@drawable/pic1</item> <item>@drawable/pic2</item> <item>@drawable/pic3</item> <item>@drawable/pic4</item> </integer-array> </resources>Output:
By default, the first item in the spinner is selected.Now, choosing another item.
Spinner in Android Application
Finally, when clicked on Leonardo DiCaprio.
Spinner in Android Application
Spinner in Android Application
No comments:
Post a Comment