Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
DropDownView in Android
Next article icon

DropDownView in Android

Last Updated : 23 Nov, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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. We are going to implement this project using both Java and Kotlin Programming Language for Android.

DropDownView in Android Sample GIF

Applications of DropDownView

  • 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 of DropDownView

Attributes

Description

layout_widthTo give width to the layout.
layout_heightTo give height to the layout.
containerBackgroundColorTo give Background color to the container.
overlayColorTo give Overlay Color.

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. The code for that has been given in both Java and Kotlin Programming Language for Android.

Step 2: Add the Required Dependencies

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 Support Library in your settings.gradle File. This library Jitpack is a novel package repository. It is made for JVM so that any library which is present in Github and Bitbucket can be directly used in the application.

dependencyResolutionManagement {     repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)     repositories {         google()         mavenCentral()                  // add the following         maven { url "https://jitpack.io" }     } }

After adding this Dependency, Sync the Project and now we will move towards its implementation.

Step 3: Working with the XML Files

Next, go to the activity_main.xml file, which represents the UI of the project. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail.

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> 

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 File

Go to the MainActivity File and refer to the following code. Below is the code for the MainActivity 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(v -> {             // on click the drop down will open or close             if (dropDownView.isExpanded()) {                 dropDownView.collapseDropDown();             } else {                 dropDownView.expandDropDown();             }         });     } } 
Kotlin
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  class MainActivity : AppCompatActivity() {      private lateinit var button: Button      override fun onCreate(savedInstanceState: Bundle?) {         super.onCreate(savedInstanceState)         setContentView(R.layout.activity_main)          // Drop down menu given using id         val dropDownView: findViewById<DropDownView>(R.id.drop_down_view)          val collapsedView: View = LayoutInflater.from(this).inflate(R.layout.header, null, false)         val expandedView: View = LayoutInflater.from(this).inflate(R.layout.footer, null, false)          dropDownView.setHeaderView(collapsedView)         dropDownView.setExpandedView(expandedView)         collapsedView.setOnClickListener {             // on click the drop down will open or close             if (dropDownView.isExpanded()) {                 dropDownView.collapseDropDown()             } else {                 dropDownView.expandDropDown()             }         }     } } 

Output:


Next Article
DropDownView in Android

C

chinmaymunje96
Improve
Article Tags :
  • Java
  • Technical Scripter
  • Kotlin
  • Android
  • Technical Scripter 2020
  • Android-View
Practice Tags :
  • Java

Similar Reads

    GravityView in Android
    In this article, we are going to show the GravityView in android. In this article, we are going to see the gravity effect on an image. As we move our phone we will see different parts of the image. Here we will be using Horizontal ScrollView so we will be moving our phone horizontally. In the below
    2 min read
    PopView in Android
    In this article, PopView is added to android. When users tap on the view a pop animation with a circular dust effect will appear. A new view can also appear after the popping of the older view. PopView can be used to hide the view or change the view. It makes the user interface more attractive. Supp
    3 min read
    Exposed Drop-Down Menu in Android
    The Exposed Drop-Down menu is the replacement for Spinner in Android because Spinner is not that customizable like the new exposed Drop-Down menu. Below is the sample GIF to give an idea of what we are going to build. Note that we are going to implement this project using the Kotlin language. Step b
    2 min read
    Scroll ImageView in Android
    In this article, we are going to implement a very important feature related to the ImageView. The image will keep on scrolling horizontally by itself. When we click on the image then it will stop scrolling and when we again click it will again keep scrolling. We can use this feature to show animatio
    3 min read
    Horizontal CalendarView in Android
    If we are making an application that provides services such as booking flights, movie tickets, or others we generally have to implement a calendar in our application. We have to align the calendar in such a way so that it will look better and will take less amount of space in the mobile app. Most of
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences