Open In App

How to Implement Dependent Dropdown in Android?

Last Updated : 27 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

The concept of dependent dropdown uses two spinners where the value of one spinner depends on the other spinner. Dropdown is implemented using spinners, spinners provide a quick way to select one value from a set. In the default state, a spinner shows its currently selected value. Touching the spinner displays a dropdown menu with all other available values, from which the user can select a new one. In this article, we will learn that how we can implement dependent dropdown in android studio. 

What we are going to build in this article?

In this article, we will be using two dropdown menus where the value of the second menu will be directly dependent on the first one i.e. if the value of the first menu will increase then the value of the second menu will also increase and if we will decrease the value of the first menu then the value of the second menu will also decrease by same difference. Here is a sample video of the application which we are going to build. Note that we are going to implement this project in Java Language.

Step by Step Implementation

Step 1: Create a new project

  • Open a new project.
  • We will be working on Empty Activity with language as Java. Leave all other options unchanged.
  • You can change the name of the project at your convenience.
  • There will be two default files named activity_main.xml and MainActivity.java.

If you don’t know how to create a new project in Android Studio then you can refer to How to Create/Start a New Project in Android Studio?  

Step 2: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. 

XML




<?xml version="1.0" encoding="utf-8"?>
<!--Linear layout as parent layout-->
<LinearLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:padding="16dp"
    android:gravity="center"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
  
    <!-- Dropdown number 1-->
    <Spinner
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:id="@+id/sp_min"
        android:layout_marginEnd="4dp"
        tools:listitem="@layout/item_dropdown"
        />
  <!-- Drop down number 2-->
  <Spinner
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:id="@+id/sp_max"
        android:layout_marginStart="4dp"
        tools:listitem="@layout/item_dropdown"
        />
  
</LinearLayout>


Step 3: Working with item_dropdown.xml file

Follow the path app > res > layout > right click > new > layout resource file and create a new file named as item_main.xml. Use the below code in item_dropdwon.xml file-

XML




<?xml version="1.0" encoding="utf-8"?>
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/text1"
    android:textSize="20sp"
    android:textStyle="bold"
    android:padding="12dp"
    android:gravity="center">
</TextView>


Step 4: Working with MainActivity.java file

Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.

Java




package com.example.dependentdropdown;
  
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 java.util.ArrayList;
  
public class MainActivity extends AppCompatActivity {
  
    // Initialize variables
    Spinner spMin,spMax;
    ArrayList<String> pricelist=new ArrayList<>();
    ArrayList<String> minList=new ArrayList<>();
    ArrayList<String> maxList=new ArrayList<>();
    ArrayAdapter<String> minAdapter,maxAdapter;
  
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // assign variables
        spMin=findViewById(R.id.sp_min);
        spMax=findViewById(R.id.sp_max);
  
        // use for loop
        for(int i=1;i<=15;i++)
        {
            // add values in price list
            pricelist.add("\u20b9"+i+"Lac");
            // check condition
            if(i>1)
            {
                // Not include first value  in max list
                maxList.add("\u20b9"+i+"Lac");
            }
  
            if(i!=15)
            {
                // Not include  last value in min list
                minList.add("\u20b9"+i+"Lac");
            }
        }
        
        // Initialize adapter
        minAdapter=new ArrayAdapter<>(this,R.layout.item_dropdown,minList);
        maxAdapter=new ArrayAdapter<>(this,R.layout.item_dropdown,maxList);
  
        // set adapter
        spMin.setAdapter(minAdapter);
        spMax.setAdapter(maxAdapter);
  
        // set item selected listener on min spinner
        spMin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                // clear max list
                maxList.clear();
                // use for loop
                for(int i=position+1;i<pricelist.size();i++)
                {
                    // add values in max list
                    maxList.add(pricelist.get(i));
                }
                // notify adapter
                maxAdapter.notifyDataSetChanged();
            }
  
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
  
            }
        });
    }
}


Here is the final output of our application.

Output:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads