In Android, you can use “android.widget.Spinner” class to render a dropdown box selection list.
File : res/values/strings.xml
File : MyAndroidAppActivity.java
File : CustomOnItemSelectedListener.java
1. Result, two spinners are displayed :
2. Select “France” from spinner1, item selection listener is fired :
3. Select “list2″ from spinner2, and click on the submit button :
Note
Spinner is a widget similar to a drop-down list for selecting items.
In this tutorial, we show you how to do the following tasks :Spinner is a widget similar to a drop-down list for selecting items.
- Render a Spinner in XML, and load the selection items via XML file also.
- Render another Spinner in XML, and load the selection items via code dynamically.
- Attach a listener on Spinner, fire when user select a value in Spinner.
- Render and attach a listener on a normal button, fire when user click on it, and it will display selected value of Spinner.
1. List of Items in Spinner
Open “res/values/strings.xml” file, define the list of items that will display in Spinner (dropdown list).File : res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">MyAndroidApp</string> <string name="country_prompt">Choose a country</string> <string-array name="country_arrays"> <item>Malaysia</item> <item>United States</item> <item>Indonesia</item> <item>France</item> <item>Italy</item> <item>Singapore</item> <item>New Zealand</item> <item>India</item> </string-array> </resources>
2. Spinner (DropDown List)
Open “res/layout/main.xml” file, add two spinner components and a button.- In “spinner1″, the “
android:entries
” represents the selection items in spinner. - In “spinner2″, the selection items will be defined in code later.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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/spinner1" android:layout_width="match_parent" android:layout_height="wrap_content" android:entries="@array/country_arrays" android:prompt="@string/country_prompt" /> <Spinner android:id="@+id/spinner2" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/btnSubmit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Submit" /> </LinearLayout>
3. Code Code
Read the code and also code’s comment, it should be self-explanatory.File : MyAndroidAppActivity.java
package com.mkyong.android; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.Spinner; import android.widget.Toast; public class MyAndroidAppActivity extends Activity { private Spinner spinner1, spinner2; private Button btnSubmit; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); addItemsOnSpinner2(); addListenerOnButton(); addListenerOnSpinnerItemSelection(); } // add items into spinner dynamically public void addItemsOnSpinner2() { spinner2 = (Spinner) findViewById(R.id.spinner2); List<String> list = new ArrayList<String>(); list.add("list 1"); list.add("list 2"); list.add("list 3"); ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list); dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner2.setAdapter(dataAdapter); } public void addListenerOnSpinnerItemSelection() { spinner1 = (Spinner) findViewById(R.id.spinner1); spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener()); } // get the selected dropdown list value public void addListenerOnButton() { spinner1 = (Spinner) findViewById(R.id.spinner1); spinner2 = (Spinner) findViewById(R.id.spinner2); btnSubmit = (Button) findViewById(R.id.btnSubmit); btnSubmit.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MyAndroidAppActivity.this, "OnClickListener : " + "\nSpinner 1 : "+ String.valueOf(spinner1.getSelectedItem()) + "\nSpinner 2 : "+ String.valueOf(spinner2.getSelectedItem()), Toast.LENGTH_SHORT).show(); } }); } }
package com.mkyong.android; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.Toast; public class CustomOnItemSelectedListener implements OnItemSelectedListener { public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) { Toast.makeText(parent.getContext(), "OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(), Toast.LENGTH_SHORT).show(); } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }
4. Demo
Run the application.1. Result, two spinners are displayed :
Download Source Code
Download it – Android-Spinner-DropDownList-Example.zip (16 KB)
packers and movers noida sector 12 @ http://www.noidapackers.co.in/
ReplyDeletepackers and movers noida sector 53 @ http://www.noidapackers.co.in/
packers and movers noida sector 51 @ http://www.noidapackers.co.in/
packers and movers noida sector 50 @ http://www.noidapackers.co.in/
packers and movers noida sector 49 @ http://www.noidapackers.co.in/
packers and movers noida sector 41 @ http://www.noidapackers.co.in/
packers and movers noida sector 39 @ http://www.noidapackers.co.in/
packers and movers noida sector 62 @ http://www.noidapackers.co.in/
packers and movers noida sector 61 @ http://www.noidapackers.co.in/
lista_k.add("4");
ReplyDeleteAndroid Tutorial Basic to Advance: Click Here
ReplyDeleteAndroid User Interface Tutorial: Click Here
very nice example. Keep sharing . Thanks liveblogging
ReplyDeletechenenen0506
ReplyDeletetory burch boots
jordan retro 11
nike trainers uk
air force 1
cheap toms
louis vuitton outlet
kate spade outlet
louis vuitton outlet stores
true religion jeans
celine handbags
nike store
air max 90
jordan retro 8
coach outlet
louis vuitton purses
cheap jordans
hollister uk
tory burch outlet online
pandora outlet
fitflops shoes
michael kors outlet
jordan 11
discount jordans
louis vuitton outlet
hollister
christian louboutin shoes
kobe 10
gucci handbags
christian louboutin sale
fit flops
Thank you for sharing any good knowledge and thanks for fantastic efforts
ReplyDeletefire fighting jobs
firefighter
fire fighting academy
Arsisc
fire fighting course
Everythings Works Perfect!
ReplyDeleteBest BCA Colleges in Noida
nike air max 270
ReplyDeletegolden goose sneakers
timberlands
moncler jackets
coach handbags
louboutin shoes
air max 90
moncler outlet
hermes online
balenciaga trainers