All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Android Drop Down List Without Spinner

Last Updated : Mar 11, 2024

Android Drop Down List Without Spinner

In this article we will show you the solution of android drop down list without spinner, the dependent dropdown concept employs two spinners, the value of one of which is dependent on the value of the other.

A dropdown can be implemented using spinners as they offer a quick way to choose one value from a list.

A spinner's default state displays the value that is currently selected. An option selection menu appears when the spinner is touched, from which the user can choose another value.

In this article, we will use two dropdown menus, with the value of the second menu being directly dependent on the value of the first, i.e.

If the value of the first menu increases, the value of the second menu will also increase, and if the value of the first menu decreases, the value of the second menu will also decrease by the same amount.

Step By Step Guide On Android Drop Down List Without Spinner :-

XML code

<?xml version="1.0" encoding="utf-8"?>
<!--Linear layout as parent layout-->
<LinearLayout
    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: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="6dp"
     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="6dp"
        tools:listitem="@layout/item_dropdown"
     />
</LinearLayout>

XML code

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
 android:layout_height="wrap_content"
    android:id="@+id/text1"
   android:textSize="10sp"
    android:textStyle="bold"
    android:padding="14dp"
    android:gravity="center">
</TextView>

Java code

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 {
 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);
        spMin=findViewById(R.id.sp_min);
        spMax=findViewById(R.id.sp_max);
     for(int i=1;i<=15;i++)
     {
            pricelist.add("\u20b9"+i+"Lac");
         if(i>1)
         {
                maxList.add("\u20b9"+i+"Lac");
         }
         if(i!=15)
         {
                minList.add("\u20b9"+i+"Lac");
         }
     }
     minAdapter=new ArrayAdapter<>(this,R.layout.item_dropdown,minList);
     maxAdapter=new ArrayAdapter<>(this,R.layout.item_dropdown,maxList);
        spMin.setAdapter(minAdapter);
        spMax.setAdapter(maxAdapter);
        spMin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
         public void onItemSelected(AdapterView<?> parent, View , int position, long id) {
             maxList.clear();
                for(int i=position+1;i<pricelist.size();i++)
             {
                    maxList.add(pricelist.get(i));
             }
                maxAdapter.notifyDataSetChanged();
         }
         @Override
         public void onNothingSelected(AdapterView<?> parent) {
  }
     });
 }
}
  1. New projects can be started.
  2. A Java activity will be created from scratch.
  3. You can change the project's name whenever you want. The default files will be activity main.xml and MainActivity.java.
  4. We then work with the activity main.xml file, navigating to the app > res > layout > activity main.xml and adding the code below to that file.
  5. Next, while still working with the item dropdown.xml file, go to app > res > layout > right click > new > layout resource file and create a new file called item main.xml.
  6. Next, we will work with the MainActivity.java file.
  7. Then, in the MainActivity.java file, add execution code.
  8. The xml code will explain the project's layout, such as height, width, padding, and context.
  9. The dropdown number is then added in the code.
  10. Finally, we add java code to complete the program's execution.

Conclusion :-

In this article, we will use two dropdown menus, with the value of the second menu being directly dependent on the value of the first, i.e.

If the value of the first menu increases, the value of the second menu will also increase, and if the value of the first menu decreases, the value of the second menu will also decrease by the same amount.

I hope this article on android drop down list without spinner helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪