Tuesday, March 16, 2021

Android Spinner

 

Android Spinner Example

android spinner

Android Spinner is like the combox box of AWT or Swing. It can be used to display the multiple options to the user in which only one item can be selected by the user.

Android spinner is like the drop down menu with multiple values from which the end user can select only one value.

Android spinner is associated with AdapterView. So you need to use one of the adapter classes with spinner.







Android Spinner class is the subclass of AsbSpinner class.

Android Spinner Example

In this example, we are going to display the country list. You need to use ArrayAdapter class to store the country list.

Let's see the simple example of spinner in android.

activity_main.xml

Drag the Spinner from the pallete, now the activity_main.xml file will like this:

File: activity_main.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     tools:context="example.javatpoint.com.spinner.MainActivity">  
  8.   
  9.     <Spinner  
  10.         android:id="@+id/spinner"  
  11.         android:layout_width="149dp"  
  12.         android:layout_height="40dp"  
  13.         android:layout_marginBottom="8dp"  
  14.         android:layout_marginEnd="8dp"  
  15.         android:layout_marginStart="8dp"  
  16.         android:layout_marginTop="8dp"  
  17.         app:layout_constraintBottom_toBottomOf="parent"  
  18.         app:layout_constraintEnd_toEndOf="parent"  
  19.         app:layout_constraintHorizontal_bias="0.502"  
  20.         app:layout_constraintStart_toStartOf="parent"  
  21.         app:layout_constraintTop_toTopOf="parent"  
  22.         app:layout_constraintVertical_bias="0.498" />  
  23.   
  24. </android.support.constraint.ConstraintLayout> 

Activity class

Let's write the code to display item on the spinner and perform event handling.

File: MainActivity.java
  1. package example.javatpoint.com.spinner;  
  2.   
  3. import android.support.v7.app.AppCompatActivity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.widget.AdapterView;  
  7. import android.widget.ArrayAdapter;  
  8. import android.widget.Spinner;  
  9. import android.widget.Toast;  
  10.   
  11. public class MainActivity extends AppCompatActivity implements  
  12.         AdapterView.OnItemSelectedListener {  
  13.     String[] country = { "India""USA""China""Japan""Other"};  
  14.   
  15.     @Override  
  16.     protected void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.activity_main);  
  19.        //Getting the instance of Spinner and applying OnItemSelectedListener on it  
  20.         Spinner spin = (Spinner) findViewById(R.id.spinner);  
  21.         spin.setOnItemSelectedListener(this);  
  22.   
  23.         //Creating the ArrayAdapter instance having the country list  
  24.         ArrayAdapter aa = new ArrayAdapter(this,android.R.layout.simple_spinner_item,country);  
  25.         aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);  
  26.         //Setting the ArrayAdapter data on the Spinner  
  27.         spin.setAdapter(aa);  
  28.   
  29.     }  
  30.   
  31.     //Performing action onItemSelected and onNothing selected  
  32.     @Override  
  33.     public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long id) {  
  34.         Toast.makeText(getApplicationContext(),country[position] , Toast.LENGTH_LONG).show();  
  35.     }  
  36.     @Override  
  37.     public void onNothingSelected(AdapterView<?> arg0) {  
  38.         // TODO Auto-generated method stub  
  39.     }  
  40. }  

No comments:

Post a Comment

Inapp update

  Inapp update https://desk.zoho.com/portal/vegabirdtech/en/kb/articles/how-to-use-burp-suite-with-android-mobile