GTU DE Sem6 – 3360704 – 6 – Create spinner with strings taken from resource folder(res >> value folder) On changing spinner value change image

GTU DE Sem6 – 3360704 – 6 – Create spinner with strings taken from resource folder(res >> value folder) On changing spinner value change image

GTU DE Sem6 - 3360704 - 6 - Create spinner with strings taken from resource folder(res >> value folder) On changing spinner value change image

The most important functionality, ie. How to take input from user like combobox. Here in Android there is concept called as Spinner. In Spinner you can add values using Array and finally you can able to trace that values when someone selects any items from the Spinner. Following example explains how to use Spinner and Display images on Imageview on the selection of the Array Options.

Jump to Android Program 7

FileName: MainActivity.java

/*
 * 
Mobile Computing and Application Development : 3360704

GTU DE Sem6 - 3360704 - 6 - Create spinner with strings taken from resource folder(res >> value folder) On changing spinner value change image


*/

package gtu.de.mcad.ex06;


import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;

public class MainActivity extends Activity {
	Spinner sp1;
	ImageView img1;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);


		sp1 =(Spinner)findViewById(R.id.spinner1);
		img1 = (ImageView)findViewById(R.id.imageView1);
		sp1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
		
				@Override
				public void onItemSelected(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					// TODO Auto-generated method stub
			if(sp1.getSelectedItemId()==0)
			{
				img1.setImageResource(R.drawable.image1);
			}
			else if(sp1.getSelectedItemId()==1)
			{
				img1.setImageResource(R.drawable.image2);
			}
			else if(sp1.getSelectedItemId()==2)
			{
				img1.setImageResource(R.drawable.image3);
			}
			else if(sp1.getSelectedItemId()==3)
			{
				img1.setImageResource(R.drawable.image4);
			}
		}
		
		@Override
		public void onNothingSelected(AdapterView<?> parent) {
			// TODO Auto-generated method stub
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		
		return super.onOptionsItemSelected(item);
	}
}

 

FileName: activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Spinner android:id="@+id/spinner1" android:layout_height="wrap_content" android:layout_width="match_parent" android:entries="@array/myarry"></Spinner>
    <ImageView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/imageView1" android:src="@drawable/image1"></ImageView>


</LinearLayout>

 

 FileName: strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">006-Spinner Example To Display Image</string>
    <string-array name="myarry">
    <item>Image1</item>
    <item>Image2</item>
    <item>Image3</item>
    <item>Image4</item>
    </string-array>
</resources>

 

Output 1

GTU DE Sem6 - 3360704 - 6 - Create spinner with strings taken from resource folder(res >> value folder) On changing spinner value change image

Output 2

GTU DE Sem6 - 3360704 - 6 - Create spinner with strings taken from resource folder(res >> value folder) On changing spinner value change image

Leave a Reply