DropDownView in Android - GeeksforGeeks

18 Feb, 2021
DropDownView is another exciting feature used in most Android applications. It is a unique way of representing the menu and other options in animated form. We can get to see the list of options under one heading in DropDownView. In this article, we are going to see how to implement DropDownView in Android. A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language. 
A unique way of representing data in the Animated form.
Make it easy to navigate and find many options under one heading.
Large options can be displayed easily.
Attributes
Description
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Add dependency and JitPack Repository
Navigate to the Gradle Scripts > build.gradle(Module:app) and add the below dependency in the dependencies section.   
implementation ‘com.github.AnthonyFermin:DropDownView:1.0.1’
Add the JitPack repository to your build file. Add it to your root build.gradle at the end of repositories inside the allprojects{ } section.
allprojects {
 repositories {
   ...
   maven { url “https://jitpack.io” }
     }
}
After adding this dependency sync your project and now we will move towards its implementation.  
Step 3: 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"?><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:layout_height="match_parent"    android:orientation="vertical"    tools:context=".MainActivity">      <!--DropDown Menu-->    <com.anthonyfdev.dropdownview.DropDownView        android:id="@+id/drop_down_view"        android:layout_width="match_parent"        android:layout_height="wrap_content"        app:containerBackgroundColor="@color/purple_200"        app:overlayColor="#EEEEEE" />  </LinearLayout>
Step 4: Create new layout resource files
Navigate to the app > res > layout > right-click > New > Layout resource file and name the files as header and footer.
Below is the code for the header.xml file:
XML
<?xml version="1.0" encoding="utf-8"?><LinearLayout     xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:padding="10dp">      <!--Header for drop down-->    <TextView        android:id="@+id/textView2"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Amazing" />  </LinearLayout>
Below is the code for the footer.xml file:
XML
<?xml version="1.0" encoding="utf-8"?><LinearLayout     xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:padding="10dp">      <!--Items in drop down-->    <TextView        android:id="@+id/textView"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Awesome" />          <TextView        android:id="@+id/textView1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginTop="10dp"        android:text="Line 1" />      <TextView        android:id="@+id/textView2"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginTop="10dp"        android:text="Line 2" />          <TextView        android:id="@+id/textView3"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Line 3" />          <TextView        android:id="@+id/textView4"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Line 4" />          <TextView        android:id="@+id/textView5"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Line 5" />          <TextView        android:id="@+id/textView6"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Line 6" />          <TextView        android:id="@+id/textView7"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Line 7" />  </LinearLayout>
Step 5: Working with the 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
import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.widget.Button;  import androidx.appcompat.app.AppCompatActivity;  import com.anthonyfdev.dropdownview.DropDownView;  public class MainActivity extends AppCompatActivity {      Button button;      @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);          // Drop down menu given using id        DropDownView dropDownView = (DropDownView) findViewById(R.id.drop_down_view);        View collapsedView = LayoutInflater.from(this).inflate(R.layout.header, null, false);        View expandedView = LayoutInflater.from(this).inflate(R.layout.footer, null, false);          dropDownView.setHeaderView(collapsedView);        dropDownView.setExpandedView(expandedView);          collapsedView.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                // on click the drop down                // will open or close                if (dropDownView.isExpanded()) {                    dropDownView.collapseDropDown();                } else {                    dropDownView.expandDropDown();                }            }        });    }}
Now click on the run option it will take some time to build Gradle. After that, you will get output on your device as given below.
android
Android-View
Technical Scripter 2020
Android
Java
Technical Scripter
Java
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Flutter - Custom Bottom Navigation Bar
How to Read Data from SQLite Database in Android?
Retrofit with Kotlin Coroutine in Android
Android Listview in Java with Example
How to Change the Background Color After Clicking the Button in Android?
Arrays in Java
Split() String method in Java with examples
For-each loop in Java
Arrays.sort() in Java with examples
Reverse a string in Java