Wednesday, 17 February 2016

Example for spinner in android?

Step1> Create a Project with the name of SpinnerEx and give a Activity name as MainActivity.java

Step 2> Give the .xml file name as activity.xml

Step3> write the below code into the activity.xml file.


<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" android:background="#FFFFFF">
   
 
       <TableRow android:layout_height="0dp"
           android:layout_weight="0.15"
       android:layout_width="match_parent">
     
             <TextView android:layout_height="wrap_content"
                 android:layout_width="match_parent"
                 android:text="Select Student Name"
                 android:textSize="40dp"
                 android:textColor="#000000"/>
         
       </TableRow>
 
 
   <TableRow android:layout_height="0dp"
           android:layout_weight="0.25"
       android:layout_width="match_parent" android:background="#000000">
 
     
       <Spinner
           android:id="@+id/spnStudentName"
           android:layout_height="wrap_content"
           android:layout_width="0dp"
           android:layout_weight="0.5"
         
           />
     
   </TableRow>
 
   <TableRow android:layout_height="0dp"
           android:layout_weight="0.6"
       android:layout_width="match_parent">
 
   </TableRow>

</TableLayout>


Spet4> Write the below code into the  MainActivity.java



package com.example.spinnerex;
import java.util.ArrayList;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Spinner;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;

public class MainActivity extends Activity
 {

    @Override
    protected void onCreate(Bundle savedInstanceState)
   {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity);


   // below line is used to import an ID of spinner from xml to .java class
        final Spinner spnStudentName=(Spinner)findViewById(R.id.spnStudentName);


// In order to bind few Items to spinner , First we are storing Items in ArrayList as below.
        ArrayList<String> list=new ArrayList<String>();
        list.add("Achut");
        list.add("Rajitha");
        list.add("Praveen");
        list.add("Prakash");
        list.add("Somiyo");
        list.add("Majusha");
             

// Directly we can't assign ArrayList object to Spinner, so 1st assign ArrayList obejct to ArrayAdapter


        ArrayAdapter<String> adp=new ArrayAdapter<String>(getApplicationContext(),                   android.R.layout.simple_list_item_1,  list);


//Below line is to set ArrayAdapter obejct to Spinner by using spinner ID.

        spnStudentName.setAdapter(adp);  
     

// if you want to get selected item position and selected Item Name use OnItemSelectedListener

spnStudentName.setOnItemSelectedListener(new OnItemSelectedListener()
 {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,int position, long arg3)
{
           // This is one way of getting an position
Toast.makeText(getApplicationContext(), "Position onway:"+position, 2000).show();

          // 2nd way of getting an position

Toast.makeText(getApplicationContext(), "position second    way:"+spnStudentName.getSelectedItemPosition(), 2000).show();


       //below line is used to display selected item name of spinner in toast message

Toast.makeText(getApplicationContext(), "Student name:"+spnStudentName.getSelectedItem(), 2000).show();

}

@Override
public void onNothingSelected(AdapterView<?> arg0)
         {
// TODO Auto-generated method stub

}
});
    }
}


OutPut:



























No comments:

Post a Comment