Thursday, July 25, 2013

Android Spinner Example


According to official documentation "Spinners provide a quick way to select one value from a set". In the default state, a spinner shows its currently selected value. Touching the spinner displays a dropdown menu with all other available values, from which the user can select a new one.

So Spinner provide a way to choose/select an option from multiple available options.

The Syntax to add a spinner in layout

<Spinner
    android:id="@+id/planets_spinner"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
 

Spinner Example


In this example we are giving Easy,Medium,Hard and Too Hard options to users through
 Spinner and user selects an options, We show the user selected option in Toast.

The Spinner's item are populated form an ArrayAdapter, 
So we need to create an ArrayAdapter and set to Spinner.

Add Click Listener to Spinner

We need to add a click Listener to Spinner to handle the click event when User selects an option

main.xml

Android Spinner
 
 <LinearLayout 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:background="#B2CCCC"
    android:orientation="vertical">
    
    <TextView
        android:id="@+id/textView1"
        android:layout_marginTop="50dp"
        android:layout_marginLeft="5dp"
        android:textStyle="bold"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Select The Difficulty Level"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Spinner
        android:id="@+id/spinner1"
        android:layout_marginTop="15dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
           
</LinearLayout>  

MainActivity.java

public class MainActivity extends Activity 
{
 

 ArrayList<String> difficultyLevelOptionsList = new ArrayList<String>();
 TextView txtView1;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
  Spinner spinner = (Spinner) findViewById(R.id.spinner1); 
    
  difficultyLevelOptionsList.add("Easy");
  difficultyLevelOptionsList.add("Medium");
  difficultyLevelOptionsList.add("Hard");
  difficultyLevelOptionsList.add("Too Hard");
 
  // Create the ArrayAdapter
  ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(MainActivity.this
            ,android.R.layout.simple_spinner_item,difficultyLevelList);
  
                 // Set the Adapter
  spinner.setAdapter(arrayAdapter);
  
  // Set the ClickListener for Spinner
  spinner.setOnItemSelectedListener(new  AdapterView.OnItemSelectedListener() { 

              public void onItemSelected(AdapterView<?> adapterView, 
             View view, int i, long l) { 
             // TODO Auto-generated method stub
          Toast.makeText(MainActivity.this,"You Selected : "
           + difficultyLevelOptionsList.get(i)+" Level ",Toast.LENGTH_SHORT).show();
             
               }
                // If no option selected
    public void onNothingSelected(AdapterView<?> arg0) {
     // TODO Auto-generated method stub
          
    } 

        });

               
 }
    

}
 
 
Android Spinner

 

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.






7 comments:

  1. I want to compare two spinner values and then according to that perform different operations..
    How can i do that ?

    ReplyDelete
  2. I want to compare two spinner values and then perform operations through if statement !
    How to do this..?

    ReplyDelete
  3. I have read your blog its very attractive and impressive. I like it your post.

    ReplyDelete