Android Widget

Android Widget – how to access the Spinner and get the value of selected item for the beginner’s from XIC Coimbatore

In your ADT tool create a new Android Application name as SpinnerExample.

Open activity_main.xml drag and drop the Spinner from the widget.

 

Move to MainActivity.java page. Create a global object for the spinner as spn_item.

onCreate method use findviewbyid method connect with the widget xml page to the java page like follow

spn_item=(Spinner) findViewById(R.id.spn_item);

 

Now manually add the details for the spinner. Using Array list we can create the group of items. These things have to attach into spinner for that we need adapter like ArrayAdapter.

 

Create Array list and Array Adapter instance object.

 

String carlist[] = {“Audi”,”BMW”,”Chevrolet”,”Maruti”,”Volkswagen”,”Toyota”};

 

ArrayAdapter<String> adpt;

 

then set the list item to the adapter.

 

adpt = new ArrayAdapter<String>(MainActivity.this,   android.R.layout.simple_spinner_item, carlist);

 

finally set the adapter object to spinner event.

 

spn_item.setAdapter(adpt);

 

above steps are load the list items into the spinner widget. If you can select any one item from spinner.

 

Next step to fetch the which element selected by the user. So have to use OnItemSelectedListener. Import the listener package and add the unimplemented methods as onItemSelected and onNothingSelected methods.

 

Change the parameter name in onItemSelected third int value as position.

 

String selected_value = spn_item.getSelectedItem().toString();

 

Toast.makeText(MainActivity.this, selected_value, Toast.LENGTH_LONG).show();

 

To display the result using Toast to the selected_value string object.

 

 

Sample Demo Input :

Output Screen

Before select the value

 

After Selected value

For More Android Application Event kindly visit our Blog page.

October 20, 2017

0 responses on "Android Widget"

Leave a Message

Your email address will not be published. Required fields are marked *

© Xplore IT Corp 2017. All rights reserved. Designed by ProPlus Logics
Xplore WhatsApp Chat
Send via WhatsApp
X