Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to make Spinner in android ?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 233
    Comment on it

    Here I have created spinner function in android. Spinner can be used to display the multiple options to the user in which only one item can be selected by the user. Android spinner works like dropdown menu. In dropdown menu there will be multiple values from which the user have to select only one value. In the below code example I have described how to make spinner in android. Step (1)- activity_main.xml.

     <RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"  
       xmlns:tools="http://schemas.android.com/tools"  
       android:layout_width="match_parent"  
         android:layout_height="match_parent"  
         tools:context=".MainActivity" >  
       
         <Spinner  
             android:id="@+id/spinner1"  
             android:layout_width="wrap_content"  
             android:layout_height="wrap_content"  
             android:layout_alignParentTop="true"  
            android:layout_centerHorizontal="true"  
           android:layout_marginTop="83dp" />  
      
     </RelativeLayout>  
    

    Step (2)-MainActivity.

      public class MainActivity extends Activity implements  
     AdapterView.OnItemSelectedListener {  
      
        String[] country = {"UK", "India", "USA", "China", "Japan", "Other" };  
      
         @Override  
       protected void onCreate(Bundle savedInstanceState) {  
             super.onCreate(savedInstanceState);  
             setContentView(R.layout.activity_main);  
           //Getting the instance of Spinner and applying OnItemSelectedListener on it  
            Spinner spin = (Spinner) findViewById(R.id.spinner1);  
            spin.setOnItemSelectedListener(this);  
             ArrayAdapter aa = new ArrayAdapter(this,android.R.layout.simple_spinner_item,country);  
           aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);  
         //Setting the ArrayAdapter data on the Spinner  
           spin.setAdapter(aa);  
        }  
         @Override  
         public void onItemSelected(AdapterView<?> arg0, View arg1, int position,long id) {  
            Toast.makeText(getApplicationContext(),country[position] ,Toast.LENGTH_LONG).show();  
         }  
         @Override  
       public void onNothingSelected(AdapterView<?> arg0) {    
        }  
     
        @Override  
         public boolean onCreateOptionsMenu(Menu menu) {  
          getMenuInflater().inflate(R.menu.activity_main, menu);  
            return true;  
        }  
     }  
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: