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
  • DSA Tutorial
  • Data Structures
  • Algorithms
  • Array
  • Strings
  • Linked List
  • Stack
  • Queue
  • Tree
  • Graph
  • Searching
  • Sorting
  • Recursion
  • Dynamic Programming
  • Binary Tree
  • Binary Search Tree
  • Heap
  • Hashing
  • Divide & Conquer
  • Mathematical
  • Geometric
  • Bitwise
  • Greedy
  • Backtracking
  • Branch and Bound
  • Matrix
  • Pattern Searching
  • Randomized
Open In App
Next Article:
ExpandableBottomBar in Android
Next article icon

ExpandableBottomBar in Android

Last Updated : 23 Feb, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, ExpandableBottomBar is added in android using Kotlin. It is the substitute for BottomNavigationView. BottomNavigationView makes it easy for users to explore and switch between top-level views in a single tap. They should be used when an application has three to five top-level destinations. ExpandableBottomBar also has different options but they expand when clicked. So it can accommodate more options and gives users a better experience. Suppose the developer wants to create a music player app and add album, songs, singer, and playlist options in the app. In this situation, it can be used.
 

ExpandableBottomBar in Android

Advantages:

  • It is Top-level destinations that can be accessed from anywhere in the app.
  • It is an extension of Material Design Component(BottomNavigationView).
  • Easy to use.

Disadvantages:

  • It is used only when only three to five Destinations are there.
  • Can only be used with Mobile and Tablets.
  • The length of Text Labels should be less.
  • It should be used when the user will spend more than 90% of the time in an app in the same window.

Approach: 

Step 1: Add the support library in build.gradle file and add the dependency in the dependencies section.  It allows the developers to directly use ExpandableBottomBar directly in XML files.
 

XML
dependencies {                  implementation 'com.github.st235:expandablebottombar:1.1.8'      } 

 

Step 2: Now add the following code in the string.xml file. In this file, add all the names of the fields that are to be shown in the ExpandableBottomBar.

XML
<resources>     <string name="algo">Algorithm</string>     <string name="course">Course</string>     <string name="profile">Profile</string> </resources> 

 

Step 3: Create an AlgorithmFragment by right click on java package, select new -> Fragment(Blank).

Step 4: Follow the above step for CourseFragment and LoginFragment.

Step 5: Now add the following code in the fragment_algorithm.xml file. Here a TextView is added in the layout. 
 

fragment_algorithm.xml
<?xml version="1.0" encoding="utf-8"?> <FrameLayout     xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:id="@+id/fragment_algo"     tools:context=".AlgorithmFragment">      <TextView         android:textSize="30sp"         android:gravity="center"         android:layout_width="match_parent"         android:layout_height="match_parent"         android:text="Algorithm" />  </FrameLayout> 


Step 6: Now add the following code in fragment_course.xml file. Here a TextView is added in the layout. 
 

fragment_course.xml
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:id="@+id/fragment_course"     tools:context=".CourseFragment">      <TextView         android:textSize="30sp"         android:gravity="center"         android:layout_width="match_parent"         android:layout_height="match_parent"         android:text="Course" />  </FrameLayout> 

 

Step 7: Now add the following code in fragment_profile.xml file. Here a TextView is added in the layout. 
 

fragment_profile.xml
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:id="@+id/fragment_profile"     tools:context=".ProfileFragment">      <TextView         android:textSize="30sp"         android:gravity="center"         android:layout_width="match_parent"         android:layout_height="match_parent"         android:text="Profile" />  </FrameLayout> 

 

Step 8: Now add the following code in the activity_main.xml file. In this file, add ExpandableBottomBar to our layout. 
 

activity_main.xml
<?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:layout_width="match_parent"     android:layout_height="match_parent"     tools:context=".MainActivity">      <FrameLayout         android:layout_width="match_parent"         android:layout_height="match_parent"         android:id="@+id/layout"/>      <github.com.st235.lib_expandablebottombar.ExpandableBottomBar         android:id="@+id/expandable_bottom_bar"         android:layout_width="match_parent"         android:layout_height="50dp"         app:exb_backgroundColor="#2e2e2e"         app:exb_backgroundCornerRadius="25dp"         app:exb_itemInactiveColor="#fff"         app:layout_constraintBottom_toBottomOf="parent"         app:layout_constraintEnd_toEndOf="parent"         app:layout_constraintHorizontal_bias="1.0"         app:layout_constraintStart_toStartOf="parent"         app:layout_constraintTop_toTopOf="parent"         app:layout_constraintVertical_bias="0.991" />  </androidx.constraintlayout.widget.ConstraintLayout> 

 

Step 9: Now add the following code in MainActivity.kt file. In this file, add onItemSelectedListener that helps to navigate between the fragments. It will switch the fragment when the user taps on the icon. Here addItem method is used explicitly to add items to ExpandableBottomBar but it can also be done by adding items in the menu folder. 
 

MainActivity.kt
package org.geeksforgeeks.expandablebottombar  import android.graphics.Color import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import github.com.st235.lib_expandablebottomba                             .ExpandableBottomBar import github.com.st235.lib_expandablebottombar                             .ExpandableBottomBarMenuItem  class MainActivity : AppCompatActivity() {     override fun onCreate(savedInstanceState: Bundle?) {         super.onCreate(savedInstanceState)         setContentView(R.layout.activity_main)          val bottomBar: ExpandableBottomBar =                         findViewById(R.id.expandable_bottom_bar)          //set up the base fragment         supportFragmentManager.beginTransaction()                               .add(R.id.layout, AlgorithmFragment())                               .commit()          //addItem function is used to set items in ExpandableBottomBar         bottomBar.addItems(             ExpandableBottomBarMenuItem.Builder(context = this)                 .addItem(R.id.fragment_algo, R.drawable.ic_algorithm,                          R.string.algo, Color.GREEN)                 .addItem(R.id.fragment_course, R.drawable.ic_course,                          R.string.course, Color.YELLOW)                 .addItem(R.id.fragment_profile, R.drawable.ic_account,                          R.string.profile, Color.MAGENTA)                 .build()         )          //It will help the user to switch between different fragment.         bottomBar.onItemSelectedListener = { view, menuItem ->             when(menuItem.itemId){                 R.id.fragment_algo ->                     supportFragmentManager.beginTransaction()                         .replace(R.id.layout, AlgorithmFragment())                         .commit()                 R.id.fragment_course ->                     supportFragmentManager.beginTransaction()                         .replace(R.id.layout, CourseFragment())                         .commit()                 R.id.fragment_profile ->                     supportFragmentManager.beginTransaction()                         .replace(R.id.layout, ProfileFragment())                         .commit()             }         }       } } 

 

Output:
 

Reference Link: https://github.com/st235/ExpandableBottomBar
 


Next Article
ExpandableBottomBar in Android

M

madhavmaheshwarimm20
Improve
Article Tags :
  • Kotlin
  • Android
  • DSA
  • Android-Bars

Similar Reads

    Expandable TextView in Android
    ExpandableTextView is an Android library which allows us to easily create a TextView which can expand/collapse when user clicks on it .we can use this feature in many apps such as movie review app or storytelling app and in many other apps. A sample GIF is given below to get an idea about what we ar
    4 min read
    ToolBar in Android with Example
    In Android applications, Toolbar is a kind of ViewGroup that can be placed in the XML layouts of an activity. It was introduced by the Google Android team during the release of Android Lollipop(API 21). The Toolbar is basically the advanced successor of the ActionBar. It is much more flexible and cu
    8 min read
    DrawableView in Android
    In this article, we are going to show the implementation of the DrawableView. We are going to going to implement paint as we have on our laptop.  Let's see the implementation of this feature. A sample video is given below to get an idea about what we are going to do in this article. Note that we are
    3 min read
    Expandable Text in Android using Jetpack Compose
    Many applications display tons of textual data in the form of passages and have the feature of expanding or contracting it. Generally, 2-3 out of n lines of a passage are displayed along with "View More", "Read More", and "..." at the end. These appear like hyperlinks that upon click expand the text
    2 min read
    Bottom Navigation Bar in Android
    We all have come across apps that have a Bottom Navigation Bar. Some popular examples include Instagram, WhatsApp, etc. In this article, let's learn how to implement such a functional Bottom Navigation Bar in the Android app. Why do we need a Bottom Navigation Bar? It allows the user to switch to di
    6 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