Android spinners are not nothing but the dropdownlist seen in other
programming languages. It provides a easy way to select value from
list. Clicking spinner will display a dropdownlist with the available
values.
Lets see this concept with an example, which is quite simple with very few lines of code.
Step 1
-- Add a spinner object to your layout as shown below.
-- Add a text view control that displays the selected value from spinner.
Step 2
-- Declare a string array which is going to be the source for the spinner.
This source needs to be bound with the spinner through ArrayAdapter. We will be making use of the below constructor.
where we need to pass the current context, id, the objects to represent in the ListView - as shown below.
Step 3
-- Set the layout resource to create the drop down views and bind it to spinner object
adapter_state.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
-- Set the spinner to listen to selected events.
Step 4
-- Once an item is selected, the below callback fucntion will get invoked and we need to perform user defined functions here. I have displayed the selected value.
activity_main.xml
Main_activity.java
Source code of this application:
AndroidSpinners
Lets see this concept with an example, which is quite simple with very few lines of code.
Step 1
-- Add a spinner object to your layout as shown below.
<Spinner android:id="@+id/spinnerstate" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" />
-- Add a text view control that displays the selected value from spinner.
Step 2
-- Declare a string array which is going to be the source for the spinner.
private String[] state= {"Andra Pradesh","Arunachal Pradesh","Assam","Bihar","Haryana","Himachal Pradesh", "Jammu and Kashmir", "Jharkhand","Karnataka", "Kerala","Tamil Nadu"};
This source needs to be bound with the spinner through ArrayAdapter. We will be making use of the below constructor.
public ArrayAdapter (Context context, int resource, List<T> objects)
where we need to pass the current context, id, the objects to represent in the ListView - as shown below.
ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, state);
Step 3
-- Set the layout resource to create the drop down views and bind it to spinner object
adapter_state.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerState.setAdapter(adapter_state);
-- Set the spinner to listen to selected events.
spinnerState.setOnItemSelectedListener(this);
Step 4
-- Once an item is selected, the below callback fucntion will get invoked and we need to perform user defined functions here. I have displayed the selected value.
public void onItemSelected(AdapterView<?> parent, View view, int position,long id)
Android Spinners |
Selected value |
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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/osversions" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" /> <TextView android:id="@+id/selVersion" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/osversions" android:layout_marginLeft="10dp" android:layout_marginTop="20dp" /> </RelativeLayout>
Main_activity.java
import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.TextView; public class MainActivity extends Activity implements OnItemSelectedListener { Spinner spinnerOsversions; TextView selVersion; private String[] state = { "Cupcake", "Donut", "Eclair", "Froyo", "Gingerbread", "HoneyComb", "IceCream Sandwich", "Jellybean", "kitkat" }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); System.out.println(state.length); selVersion = (TextView) findViewById(R.id.selVersion); spinnerOsversions = (Spinner) findViewById(R.id.osversions); ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, state); adapter_state .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinnerOsversions.setAdapter(adapter_state); spinnerOsversions.setOnItemSelectedListener(this); } public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { spinnerOsversions.setSelection(position); String selState = (String) spinnerOsversions.getSelectedItem(); selVersion.setText("Selected Android OS:" + selState); } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }
Source code of this application:
AndroidSpinners
0 comments:
Post a Comment