DEV Community

Cover image for Dependable drop-down in android using Java
Sumit Singh
Sumit Singh

Posted on • Edited on

Dependable drop-down in android using Java

A dependable dropdown means that your drop-down list is dependent on another factor or value, which will change the content of the dropdown. In android it is known as spinner.

Spinner
Spinner provides functionality to select a value from a list of multiple values. The first value is set as the default value in the spinner and upon clicking a dropdown list appears from where the user can select the desired value.

Implementation
If you don't know how to create a simple dropdown, then refer to this article, spinner in android

XML file
Add the following code in your XML file to include spinner in your Activity

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="MissingConstraints">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="30sp"
            android:orientation="vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginVertical="10sp"
                android:text="Dependable dropdown"
                android:textSize="30sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginVertical="10sp"
                android:text="Select Gender"
                android:textSize="24sp" />

            <Spinner
                android:id="@+id/spinner_gender"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginVertical="5sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginVertical="10sp"
                android:text="Select Name"
                android:textSize="24sp" />

            <Spinner
                android:id="@+id/spinner_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="5sp"
                android:layout_marginBottom="10sp" />
        </LinearLayout>
    </androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
Enter fullscreen mode Exit fullscreen mode

Resource XML file
Now the spinner is needed to be provided with the list of values for that we need to create a resource file in the resource file. Follow the following steps to create the resource file

  1. Right-click on your project
  2. From the menu go to New > Android Resource File
  3. Give a name to your file and press **Enter*

Add the following code into your resource file

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="gender_type">
        <item>Male</item>
        <item>Female</item>
    </string-array>

    <string-array name="girls">
        <item>Priya</item>
        <item>Dolly</item>
        <item>Muskan</item>
        <item>Vaishnavi</item>
    </string-array>

    <string-array name="boys">
        <item>Sumit</item>
        <item>Ujjwal</item>
        <item>Jaskirat</item>
        <item>Sukrut</item>
        <item>Prince</item>
    </string-array>
</resources>
Enter fullscreen mode Exit fullscreen mode

JAVA file
To populate the spinner with a list of choices, you then need to specify a SpinnerAdapter in your Activity or Fragment source code. Following are the key classed:

  • Spinner
  • SpinnerAdapter
  • AdapterView.OnItemSelectedListener
package com.example.spinner;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

import com.google.android.material.snackbar.BaseTransientBottomBar;
import com.google.android.material.snackbar.Snackbar;

public class MainActivity extends AppCompatActivity {

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

        final Spinner spinner_gender
            = (Spinner)findViewById(R.id.spinner_gender);
        final Spinner spinner_name
            = (Spinner)findViewById(R.id.spinner_name);

        // Create an ArrayAdapter using the string array and
        // a default spinner layout
        ArrayAdapter<CharSequence> ad_gender
            = ArrayAdapter.createFromResource(
                this, R.array.gender_type,
                android.R.layout.simple_spinner_item);

        // Specify the layout to use when the list of
        // choices appears
        ad_gender.setDropDownViewResource(
            android.R.layout.simple_spinner_dropdown_item);

        // Apply the adapter to the spinner
        spinner_gender.setAdapter(ad_gender);

        spinner_gender.setOnItemSelectedListener(
            new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(
                    AdapterView<?> adapterView, View view,
                    int i, long l)
                {

                    if (adapterView.getSelectedItem()
                            .toString()
                            .equals("Female")) {
                        ArrayAdapter<CharSequence> ad_name
                            = ArrayAdapter.createFromResource(
                                getApplicationContext(),
                                R.array.girls,
                                android.R.layout
                                    .simple_spinner_item);
                        spinner_name.setAdapter(ad_name);
                    }
                    else {
                        ArrayAdapter<CharSequence> ad_name
                            = ArrayAdapter.createFromResource(
                                getApplicationContext(),
                                R.array.boys,
                                android.R.layout
                                    .simple_spinner_item);
                        spinner_name.setAdapter(ad_name);
                    }
                }

                @Override
                public void onNothingSelected(
                    AdapterView<?> adapterView)
                {
                }
            });

        spinner_name.setOnItemSelectedListener(
            new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(
                    AdapterView<?> adapterView, View view,
                    int i, long l)
                {
                    Snackbar
                        .make(findViewById(R.id.layout),
                              adapterView.getSelectedItem()
                                      .toString()
                                  + " is a "
                                  + spinner_gender
                                        .getSelectedItem()
                                        .toString(),
                              BaseTransientBottomBar
                                  .LENGTH_LONG)
                        .show();
                }

                @Override
                public void onNothingSelected(
                    AdapterView<?> adapterView)
                {
                }
            });
    }
}
Enter fullscreen mode Exit fullscreen mode

The setOnItemSelectedListener will work whenever anything selection is made.

Download Source code

Top comments (0)