Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Android Example To Create Spinner

Hey, In this post you will get the code of Android Spinner, it’s mean if you want to show any data into the dropdown style, then you have to use the spinner. I will use the spinner and simple array to pass the data to spinner and show output on the screen. I have also used the toast, if you need just the code of toast check Android Example To Create Toast.

activity_main.xml
MainActivity.java
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.*;
import android.widget.AdapterView.OnItemSelectedListener;
 
public class MainActivity extends Activity implements OnItemSelectedListener
{
  Spinner s1;
  
  String[] city={"Lahore","Islamabad","Kashmir","Krachi","Peshawar"};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		s1=(Spinner)findViewById(R.id.spinner1);
		
		s1.setOnItemSelectedListener(this);
		
	  ArrayAdapter a1=new ArrayAdapter(this,android.R.layout.simple_spinner_item,city);
	  a1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	  
	  s1.setAdapter(a1);
	  
	}
@Override
	public void onItemSelected(AdapterView> arg0, View arg1, int arg2,
			long arg3) 
{
		// TODO Auto-generated method stub
       String s=city[arg2];
       Toast.makeText(getApplicationContext(),s,Toast.LENGTH_SHORT).show();
	}
 
	@Override
	public void onNothingSelected(AdapterView> arg0) {
		// TODO Auto-generated method stub
		
	}
 
}
 

The post Android Example To Create Spinner appeared first on Junaid Shahid | P-Blogger.



This post first appeared on Junaid Shahid | P-Blogger, please read the originial post: here

Share the post

Android Example To Create Spinner

×

Subscribe to Junaid Shahid | P-blogger

Get updates delivered right to your inbox!

Thank you for your subscription

×