Introduction
This article explains how to create a Spinner in Android. A Spinner is similar to a drop down list. It provides a quick way to select a value from a list. It displays the current selected value and touching the spinner gives a drop down list from which we can choose the required value.
demo
Using the code
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="54dp"
android:layout_marginTop="52dp"
android:text="Select country and city" />
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="63dp" />
<Spinner
android:id="@+id/spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/spinner1"
android:layout_below="@+id/spinner1"
android:layout_marginTop="50dp" />
</RelativeLayout>
MainActivity.java:
package com.example.spinner;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class MainActivity extends Activity implements OnItemSelectedListener {
Spinner sp1,sp2;
int pos;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sp1=(Spinner)findViewById(R.id.spinner1);
sp2=(Spinner)findViewById(R.id.spinner2);
ArrayAdapter<CharSequence> adapte = ArrayAdapter.createFromResource(this, R.array.country_array, android.R.layout.simple_spinner_item);
sp1.setAdapter(adapte);
sp1.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> parent, View arg1, int pos,long arg3) {
// TODO Auto-generated method stub
if(pos==0)
{
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.city_select, android.R.layout.simple_spinner_item);
sp2.setAdapter(adapter);
}
if(pos==1)
{
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.city_india, android.R.layout.simple_spinner_item);
sp2.setAdapter(adapter);
}
else if (pos == 2) {
ArrayAdapter<CharSequence> adapter = ArrayAdapter
.createFromResource(this, R.array.city_pakisthan,
android.R.layout.simple_spinner_item);
sp2.setAdapter(adapter);
}
else if (pos == 3) {
ArrayAdapter<CharSequence> adapter = ArrayAdapter
.createFromResource(this, R.array.city_srilanka,
android.R.layout.simple_spinner_item);
sp2.setAdapter(adapter);
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
1 comments:
Write commentsPretty nice post. I simply stumbled upon your weblog and wanted to say that I've truly loved browsing your blog posts.
ReplyAfter all I will be subscribing for your feed and I am hoping
you write again soon!
Feel free to surf to my blog post lasertest
EmoticonEmoticon