How to create a drop-down list in Android Studio?

 Android Spinner is just a drop down list similar to what’s seen in other programming languages such as in HTML pages. In Android, Spinner is used to select one value from a set of values. In the default state, a spinner shows its currently selected value. Touching the spinner displays a drop down menu with all other available values, from which the user can select a new one.


Step by Step Implementation

Step 1 - Creating a New Project with Android Studio

  1. Open Android Studio and select "Create New Project".
  2. Name the project as per your wish and select your activity template.
     
     


  3. Click the Finish  button to create a new project in Android Studio.

Step 2 -  Working with the activity_main.xml file

<?xml version="1.0" encoding="utf-8"?>
<!--Constraint layout which contain Spinner widget-->
 
<android.support.constraint.ConstraintLayout
 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.geeksforgeeks.Spinner.MainActivity">
 
    <!--Spinner widget-->
    <Spinner
        android:id="@+id/coursesspinner"
        android:layout_height="50dp"
        android:layout_width="160dp"
        android:layout_marginEnd="10dp"
        android:layout_marginStart="10dp"   
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>
   
</android.support.constraint.ConstraintLayout>

Step 3 -  Working with the MainActivity.java file

import android.support.v7.app.AppCompatActivity;
import android.widget.AdapterView;
import android.view.View;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.os.Bundle;
 
// Main Activity implements Adapter view
public class MainActivity
    extends AppCompatActivity
    implements AdapterView.OnItemSelectedListener {
 
  
    String[] courses = { "C#", "C++",
                        "Android", "Java",
                        "Ethical Hacking" };
 
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        Spinner spino = findViewById(R.id.coursesspinner);
        spin.setOnItemSelectedListener(this);
 
        ArrayAdapter ad
            = new ArrayAdapter(
                this,
                android.R.layout.simple_spinner_item,
                courses);
 
        ad.setDropDownViewResource(
            android.R.layout
                .simple_spinner_dropdown_item);
 
        spino.setAdapter(ad);
    }
 
    @Override
    public void onItemSelected(AdapterView<*> arg0,
                            View arg1,
                            int position,
                            long id)
    {
 
        
        Toast.makeText(getApplicationContext(),
                    courses[position],
                    Toast.LENGTH_LONG)
            .show();
    }
 
    @Override
    public void onNothingSelected(AdapterView<*> arg0)
    {
        
    }
}

Enjoy Your Coding...

Previous Post Next Post

Contact Form