Spinner is a widget which provides a quick way to
select a value from a set. A spinner presents multiple options/items to the
user from which user can select one option.
For ex. In game application when we want user to
select a particular level he/she wants to play, we can use spinner to multiples
levels in which can select one level.
Using Spinner
Steps to use spinner in your application
1: Defining the spinner in your .xml/layout
file by using “Spinner” tag
<!—Declaring
Spinner in xml -->
<Spinner
android:id="@+id/spinner"
android:layout_marginTop="20dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="@string/spinner_title"
/>
2: Create an array/arraylist of strings
that will be set as the items of spinner.
String spinnerItems[]={"Level 1","Level 2","Level 3","Level 4","Level 5",};
3: Create adapter and set it with
spinner
// Create adapter
for spinner
ArrayAdapter dataAdapter = new
ArrayAdapter(this, android.R.layout.simple_spinner_item, spinnerItems);
// attaching data
adapter to spinner
mySpinnerObject.setAdapter(dataAdapter);
4: Set
clicklistener on Spinner
// set clicklistener
on Spinner
mySpinnerObject.setOnItemSelectedListener(this);
5: override onItemSelected() method, this method gets called by the System when an item is
selected. This method receives the position parameter which signifies the index
of the array/arraylist which is selected by the user.
@Override
public void
onItemSelected(AdapterView<?> parent, View view, int
position, long id)
{
//
get the selected Spinner's Item
String itemSelected =
parent.getItemAtPosition(position).toString();
}
Let put all this together and
develop an android application to learn how to use spinner.
Demo Application:
“Working with Spinner”
What we will do: We will present
a list of levels “Level 1,Level 2,Level 3,Level 4,Level 5" to
the user and show the level selected by the user using Toast.
activity_main.xml
<?xml version="1.0"
encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="10dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Select the
Level"
android:layout_marginTop="20dp"
android:textColor="#0000FF"
android:textSize="20dp"
android:textStyle="bold"
/>
<!-- Spinner Element -->
<Spinner
android:id="@+id/spinner"
android:layout_marginTop="20dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
SpinnerMainActivity.java
public class
SpinnerMainActivity extends Activity implements
OnItemSelectedListener
{
Spinner
mySpinner;
/* create array of string of levels
* to be set as Spinner Item
*/
String spinnerItems[]={"Level
1","Level 2","Level 3","Level
4","Level 5",};
@Override
protected void onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get the
Spinner reference
mySpinner = (Spinner)
findViewById(R.id.spinner);
// set clicklistener on Spinner
mySpinner.setOnItemSelectedListener(this);
// Create
adapter for spinner
ArrayAdapter dataAdapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_item, spinnerItems);
// Drop down
layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching
data adapter to spinner
mySpinner.setAdapter(dataAdapter);
}
/*
onItemSelected() method gets called by the System
* when an item is
selected
* This method receives
the position parameter
* which signifies the
index of the array
* which is selected
*/
@Override
public void
onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
// get the
selected Spinner's Item
String
itemSelected = parent.getItemAtPosition(position).toString();
// Showing
selected spinner item
Toast.makeText(getApplicationContext(), itemSelected+" Selected", Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView
view)
{
// TODO
Auto-generated method stub
}
}
New Advance Topics: Android LiveWallpaer Tutorial
Android ImageSwitcher Android TextSwitcher Android ViewFlipper
Android Gesture Detector Handling/Detecting Swipe Events Gradient Drawable
Detecting Missed Calls Hide Title Bar GridView Animation
Android AlarmManager Android BootReceiver Vibrate Phone In a Desirable Pattern
Developing for Different Screen Sizes Showing Toast for Longer Time Publishing your App
How to publish Android App on Google Play
Android TextWatcher Android ExpandableListView
Beginning With Android
Android : Introduction(What is Android) Configuring Eclipse for Android Development
Creating Your First Android Project Understanding Android Manifest File of your android app
Advance Android Topics Customizing Android Views
Working With Layouts Working With Views
Understanding Layouts in Android Using Buttons and EditText in Android
Working with Linear Layout (With Example) Using CheckBoxes in Android
Nested Linear Layout (With Example) Using AutoCompleteTextView in Android Grid View
Relative Layout In Android ListView
Table Layout Android ProgressBar
Frame Layout(With Example) Customizing ProgressBar
Absolute Layout Customizing Radio Buttons
Grid Layout Customizing Checkboxes In Android
Android Advance Views
Android Spinner Android GalleryView
Android TabWidget Android ExpandableListView
Android Components Dialogs In Android
Activity In Android Working With Alert Dialog
Activity Life Cycle Adding Radio Buttons In Dialog
Starting Activity For Result Adding Check Boxes In Dialog
Sending Data from One Activity to Other in Android Creating Customized Dialogs in Android
Returning Result from Activity Creating Dialog To Collect User Input
Android : Service DatePicker and TimePickerDialog
BroadcastReceiver Using TimePickerDialog and DatePickerDialog In android
Menus In Android ListView:
Creating Option Menu Populating ListView With DataBase
Creating Context Menu In Android Populating ListView with ArrayList
ListView with Custom Adapter
Toast Working With SMS
Customizing Toast In Android How to Send SMS in Android
Customizing the Display Time of Toast How To Receive SMS
Customizing Toast At Runtime Accessing Inbox In Android
Adding Image in Toast
Showing Toast for Longer Time
TelephonyManager Storage: Storing Data In Android
Using Telephony Manager In Android SharedPreferences In Android
Reading and Writing files to Internal Stoarage
Working With Incoming Calls DataBase : Introduction of SQLiteDataBase
How To Handle Incoming Calls in Android Working With Database in Android
How to Forward an Incoming Call In Android Creating Table In Android
CALL States In Android Inserting, Deleting and Updating Records In Table in Android
Miscellaneous
Notifications In Android
How To Vibrate The Android Phone
Sending Email In Android
Opening a webpage In Browser
How to Access PhoneBook In Android
Prompt User Input with an AlertDialog
How to Hide Title Bar In Android
How to show an Activity in Landscape or Portrait Mode only.
How to Set an Image as Wallpaper.
I know this if off topic but I'm looking into starting my own weblog and was wondering what all is
ReplyDeleteneeded to get set up? I'm assuming having a blog like
yours would cost a pretty penny? I'm not very web savvy so I'm not 100% certain. Any suggestions or advice would be greatly appreciated.
Cheers
I'm pretty pleased to uncover this website.
ReplyDeleteI wanted to thank you for ones time just for this fantastic read!!
I definitely really liked every little bit of it and I have you book marked to look at new things in your
website.
And indeed, I’m just always astounded concerning the remarkable things served by you. Some four facts on this page are undeniably the most effective I’ve had.
ReplyDeleteC C++ Training in Chennai |C C++ Training course in Chennai
linux Training in Chennai |linux Training in Chennai
Unix Training in Chennai |Unix Training in Chennai
uipath training in chennai | uipath training in chennai
Rprogramming Training in Chennai | Rprogramming Training in Chennai
Thanks for sharing valuable information. Your blogs were helpful to learners.
ReplyDeleteAndroid App development Course
iOS development course
Thank you for sharing with us.
ReplyDeletemoviebox for pc
Mumbai
ReplyDeleteHi,
ReplyDeleteThanks for sharing the information with us it was very informative. Hangup.in