Android uses Spinner as drop-down list item in activity or action
bar frequently.It is a quick way to select a value from the list.
Here in my example I have implemented a spinner view which
contains the list of cities. If we click on show button it will take the current
selected value from the spinner and show
as toast message .
Reset button will reset the default value in spinner.
Step-1: Create a new
project and add the spinner layout in activity_main.xml file
<Spinner
android:id="@+id/spinner1"
android:layout_width="150dp"
android:layout_height="40dp">
</Spinner>
You can define your
layout size according to requirement Or leave as match_parent or wrap_content
Also add two buttons.
After this our Activity file will we like this:
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"
android:background="#2EFEF7" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/spinner1"
android:layout_marginTop="54dp"
android:text="Show" />
<Spinner
android:id="@+id/spinner1"
android:layout_width="150dp"
android:layout_height="40dp"
android:layout_alignLeft="@+id/button1"
android:layout_alignParentTop="true"
android:layout_marginTop="47dp" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_centerHorizontal="true"
android:text="Reset" />
</RelativeLayout>
Step-2: Define
the list of items which you want to show in Spinner. You can do it in many
ways. Like you can define in resources.xml file as string-array. See here. You
can also take values from database and store in string-array. Here I have taken
static values in string-array . Add this in to your MainActivity.java file
String[] items=new String[] {"<Select City>","Bangalore","New Delhi","Koltaka","Chennai"};
Step-3: Define the Spinner layout.
final Spinner spinner=(Spinner)
findViewById(R.id.spinner1);
Step-4: Create an ArrayAdapter to show the values in list. Set the adapter
with your string-array as follows.
// Create an ArrayAdapter using the
string array and a default spinner layout
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item,items); //
Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
Now our Spinner is ready for use.
Step-5: For buttonClickListeners get the values from
spinner by following:
// get the particular item from
spinner
String item=
spinner.getSelectedItem().toString();
//set the particular item in spinner
spinner.setSelection(0);
Final
Code:
MainActivity.java
import android.os.Bundle;
import
android.app.Activity;
import android.view.Menu;
import android.view.View;
import
android.view.View.OnClickListener;
import
android.widget.AdapterView;
import
android.widget.AdapterView.OnItemSelectedListener;
import
android.widget.ArrayAdapter;
import
android.widget.Button;
import
android.widget.Spinner;
import
android.widget.Toast;
public class MainActivity extends Activity implements
OnItemSelectedListener {
//items to show in
spinners
String[]
items=new String[] {"<Select
City>","Bangalore","New Delhi","Koltaka","Chennai"};
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button
bShow=(Button) findViewById(R.id.button1);
Button
bReset=(Button) findViewById(R.id.button2);
final Spinner
spinner=(Spinner) findViewById(R.id.spinner1);
// Create an
ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<String>
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item,items);
// Specify the
layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the
adapter to the spinner
spinner.setAdapter(adapter);
bShow.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// get the
particular item from spinner
String
item= spinner.getSelectedItem().toString();
Toast.makeText(getApplicationContext(),"Selected
item:"+item,0).show();
}
});
bReset.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//set the
particular item in spinner
spinner.setSelection(0);
Toast.makeText(getApplicationContext(),"Spinner
Resetted",0).show();
}
});
}
@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 void
onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
// An item was selected.
You can retrieve the selected item using
//parent.getItemAtPosition(pos);
//Here
you can do more implementations according to your requirements based on selecting particular item
}
@Override
public void
onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated
method stub
}
}
Step-6: Run the project . You can your additional logic as per requirements in above three functions.
Please comment and feedback for any issue .Thank You!!
No comments:
Post a Comment