How to get Android Spinner Dropdown?

Please Share On:

In this tutorial, I am going to show you how can you get android spinner dropdown items.

Android Spinner is a widget that has a dropdown. Once you click on dropdown you will see various options and you can one among these.

Now, let start how to achieve this task.

Step 1: Create new project or open existing project

Create a new android application project and named as a spinner or open your existing project. You can use your spinner anywhere you need.

Step 2: spinner.xml

Create your spinner layout in res ->layout ->spinner.xml and design your spinner.xml as below code.

<?xml version="1.0" encoding="utf-8"?>  
                  
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/background">

             <TextView
                 android:id="@+id/txtState"
                 android:layout_width="0dp"
                 android:layout_height="wrap_content"
                 android:text="@string/state"
                 android:layout_weight="1"
                 android:textStyle="bold"
                 android:textSize="16sp"/>

             <Spinner
                android:id="@+id/spinner"
                android:layout_width="0dp"
                android:layout_height="20dp"
                android:layout_weight="1"/>

</RelativeLayout>

Step 3: MainActivity.java

package com.elsebazaar.spinner;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

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

        // get Spinner reference
        Spinner spinner = (Spinner) findViewById(R.id.spinner);

       // call method Spinner click listener
       ItemSelectedListener();

        // State Spinner Dropdown elements
        List<String> state = new ArrayList<>();
        state.add("NSW");
        state.add("VIC");
        state.add("QLD");
        state.add("SA");
        state.add("WA");
        state.add("TAS");
        state.add("NT");
        state.add("ACT");

// Creating Array Adapter for Spinner
 ArrayAdapter<String> dataAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, state);

// Dropdown list with radio button
     dataAdapter.setDropDownViewResource(android.R.layout.select_dialog_singlechoice);

 // Attach data adapter to spinner
 spinner.setAdapter(dataAdapter);
}

private void ItemSelectedListener() {
        mstate.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                // get a selected item
                String item = parent.getItemAtPosition(position).toString();

                // Showing a selected spinner item in a toast
                //Toast.makeText(parent.getContext(), "Selected State: " + item, Toast.LENGTH_LONG).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
//Write code here if nothing selected
            }
        });
     }
    }

Step 4: Run your App

That’s it. You get what you want. You can see in the above image you get a state drop down list with a radio button next to each state name.



Donate to support writers


You may interest on the following topics:

Leave a Reply

Your email address will not be published. Required fields are marked *

Copyright @2023. All Right Reserved.


Social media & sharing icons powered by UltimatelySocial