GTU Android Program 6 : Spinner Example Part 2

      Comments Off on GTU Android Program 6 : Spinner Example Part 2

Back to Part 1

 

Hope you have understood the concept of GTU Android Program 6 Spinner till now. Now let’s look into the remaining code and understand the rest of the part. I assure you will surely understand the concept of android spinner. Android spinner is the most frequently used view. Here is the complete easy to understand the code.

 

Location : res\values

FileName: string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">mca_andriod_prog6</string>

    <string-array name="color">
        <item> Red </item>
        <item> Green </item>
        <item> Blue </item>
    </string-array>
    
</resources>

Location : src\package\

FileName : MainActivity.java

package mca.andriod.prog6;


/*
 * GTU Android Program 6 Understand resource folders Create spinner with strings taken from resource folder(res >> value folder) On changing spinner value, change image
 *   
 */

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.AdapterView.OnItemSelectedListener;
import android.widget.ImageView;
import android.widget.Spinner;

public class MainActivity extends Activity {
	
	Spinner sp1;
	ImageView iv1;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		//====================================
		//Setting Reference to actual control
		//====================================
		sp1 = (Spinner) findViewById(R.id.spinner1);
		iv1 = (ImageView) findViewById(R.id.imageView1);

		//====================================
		//Spinner Item Selected Event Code
		//====================================
		sp1.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				
				
				//====================================
				//arg2 will return index of items, you can check with your values, 
				// or else you can find string from resource array values and take action
				//====================================
				if(arg2==0)
				{
					//==================================
					//Load red image from drawable folder and set it to ImageView1
					//==================================
					iv1.setImageResource(R.drawable.red);	
				}
				else if(arg2==1)
				{
					iv1.setImageResource(R.drawable.green);
				}
				else
				{
					iv1.setImageResource(R.drawable.blue);
				}
					
				
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// 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.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}

Output 1

spinner_1

Output 2

spinner_2

Output 3

spinner_3

Output 4

spinner_4

Download Complete Example with Images and Complete Code

 Download