Skip to content
geeksforgeeks
  • 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
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java
  • Android
  • Kotlin
  • Flutter
  • Dart
  • Android with Java
  • Android Studio
  • Android Projects
  • Android Interview Questions
Open In App
Next Article:
How to Change Colors of a Floating Action Button in Android?
Next article icon

How To Avoid Snackbar Overlap Floating Action Button in Android?

Last Updated : 08 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

If one has implemented the Floating Action Button (FAB) in his Android application and also there exists a Snackbar implementation in the same application, then the Snackbar is definitely going to overlap the Floating Action Button. 

Prerequisite: 

  • Floating Action Button (FAB) in Android with Example
  • How to add a Snackbar in Android

Have a look at the following GIF which shows the Snackbar and Floating Action Button issue:


How to fix this issue?

So in this article let’s fix this issue by taking a simple example in Android. Note that we going to implement this example using Java language.

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 on the app-level Gradle file.

  • Here we are using the Floating action button which is designed and developed by Google Material Design Team.
  • Add the dependency in the build.gradle(app) file as:

implementation ‘com.google.android.material:material:1.3.0-alpha02’

  • Make sure that add the dependency to the app-level Gradle file. After adding the dependency you need to click on the “Sync Now” button which appears at the top right corner of the Android Studio IDE.
  • When the user clicks on the Sync Now button make sure that you are connected to the network so that it can download the required files.
  • Refer to the below image if you can’t get the steps mentioned above:
applevelgradlegooglemdc



Step 3: Add the FAB icons to the Drawable file

For demonstration purposes will import 3 icons in the Drawable folder, one can import the icons of his/her choice. One can do that by right-clicking drawable folder -> New -> Vector Asset. Refer to the following image to import the vector Icon.

importvector



Now select your vector icon:

vectorpopup



Step 4: Working with the activity_main.xml file

  • In the activity_main.xml file add a Floating Action Button (FAB) and a Button. So whenever the user clicks on the Button then a Snackbar will pop up.
  • Invoke the following code inside the activity_main.xml file. For clear understanding refer the comments inside the code given below:
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">      <Button         android:id="@+id/show_snackbar_button"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:gravity="center"         android:text="SHOW SNACKBAR"         app:layout_constraintBottom_toTopOf="@+id/add_fab"         app:layout_constraintEnd_toEndOf="parent"         app:layout_constraintStart_toStartOf="parent"         app:layout_constraintTop_toTopOf="parent"         tools:ignore="MissingConstraints" />      <!--Add simple Parent Floating Action Button-->     <com.google.android.material.floatingactionbutton.FloatingActionButton         android:id="@+id/add_fab"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="end"         android:layout_marginEnd="16dp"         android:layout_marginBottom="16dp"         android:src="@drawable/ic_baseline_ac_unit_24"         app:fabSize="normal"         app:layout_constraintBottom_toBottomOf="parent"         app:layout_constraintEnd_toEndOf="parent" />  </androidx.constraintlayout.widget.ConstraintLayout> 

 The following output UI is produced:

GFGframenexus52



Step 5: Working with MainActivity.java file

  • Now resolve the issue programmatically in MainActivity.java file.
  • One can observe below code that SHOW SNACKBAR button onclick listener we have set the AnchorView to the parent FAB. So that the parent FAB will be visible, even after the Snackbar pops up, and the Snackbar will be shown at the top of the FAB.
  • Invoke the following code and comments are added inside the code to understand the code in more detail.
Java
import android.os.Bundle; import android.view.View; import android.widget.Button; import androidx.appcompat.app.AppCompatActivity; import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.google.android.material.snackbar.Snackbar;  public class MainActivity extends AppCompatActivity {      Button mSnackBarButton;     FloatingActionButton mAddFab;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          // Register the show snackbar button with appropriate ID         mSnackBarButton = findViewById(R.id.show_snackbar_button);          // also register the floating action button with appropriate ID         mAddFab = findViewById(R.id.add_fab);          // Build and show the simple Snackbar with action button on it         mSnackBarButton.setOnClickListener(new View.OnClickListener() {             @Override             public void onClick(View view) {                 Snackbar snackbar = Snackbar.make(view, "This is sample snackbar", Snackbar.LENGTH_SHORT);                 // Set the Anchor View to particular view to display the snackbar to top of it                 snackbar.setAnchorView(mAddFab);                 snackbar.setAction("OKAY", new View.OnClickListener() {                     @Override                     public void onClick(View view) {                         // Do appropriate action on click of snackbar action button                     }                 });                 snackbar.show();             }         });     } } 
Kotlin
import android.os.Bundle; import android.view.View; import android.widget.Button; import androidx.appcompat.app.AppCompatActivity; import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.google.android.material.snackbar.Snackbar;  class MainActivity : AppCompatActivity() {     var mSnackBarButton: Button? = null     var mAddFab: FloatingActionButton? = null     override fun onCreate(savedInstanceState: Bundle?) {         super.onCreate(savedInstanceState)         setContentView(R.layout.activity_main)          // Register the show snackbar button with appropriate ID         mSnackBarButton = findViewById(R.id.show_snackbar_button)          // also register the floating action button with appropriate ID         mAddFab = findViewById(R.id.add_fab)          // Build and show the simple Snackbar with action button on it         mSnackBarButton.setOnClickListener(object : OnClickListener() {             fun onClick(view: View?) {                 val snackbar = Snackbar.make(view, "This is sample snackbar", Snackbar.LENGTH_SHORT)                 // Set the Anchor View to particular view to display the snackbar to top of it                 snackbar.anchorView = mAddFab                 snackbar.setAction("OKAY", object : OnClickListener() {                     fun onClick(view: View?) {                         // Do appropriate action on click of snackbar action button                     }                 })                 snackbar.show()             }         })     } } // this code is added by Ujjwal Kumar Bhardwaj 

 Output: Run on Emulator

https://media.geeksforgeeks.org/wp-content/uploads/20200922024503/GFG_frame_nexus_5.mp4


Next Article
How to Change Colors of a Floating Action Button in Android?
author
adityamshidlyali
Improve
Article Tags :
  • Android
  • Android Projects
  • Java-Android
  • Kotlin Android

Similar Reads

  • How to Add Image on Floating Action Button in Android?
    A floating action button (FAB) is a user interface element in the mobile application that is typically circular and floats above the main content. It usually has a prominent color and icon, and it is used to provide quick access to the most commonly used action within the app. Step-by-Step Implement
    1 min read
  • Android Floating Action Button in Kotlin
    Floating action buttons are used in android applications to indicate the user for some priority-based task. Generally floating action buttons in the android applications are found aligned to the bottom end of the application. In this article, we will take a look at How to implement the Floating Acti
    4 min read
  • How to Add a Floating Action Button to Bottom Navigation Bar in Android?
    The floating action button is a bit different button from the ordinary buttons. Floating action buttons are implemented in the app’s UI for primary actions (promoted actions) for the users and the actions under the floating action button are prioritized by the developer. For example the actions like
    3 min read
  • How to Change Colors of a Floating Action Button in Android?
    Android applications use the Floating Action Button for prompting the user to perform some important action within the android application. Floating Action Buttons in android applications are used to perform some important functionality within android applications. Many times in the android applicat
    3 min read
  • How to Add Action Snackbar in Android?
    Snackbar provides lightweight feedback about an operation. The message appears at the bottom of the screen on mobile and lowers left on larger devices. SnackBars plays a very important role in the user experience. The snack bar which may or may not contain the action button to do shows the message w
    2 min read
  • Floating Action Button using Fab Option Library in Android
    Floating Action Button using Fab Options is another unique way of displaying various options. With the help of this, we can Navigate to different screens easily. This Floating Action button display various menu with Animation. So it increases user experience. In this article, we are going to learn h
    3 min read
  • Floating Action Button in Android using Jetpack Compose
    Floating Action Button is added to the android application to perform some important within the android application. These are implemented in the android application UI for some primary actions within the application. There are different types of floating action buttons such as simple, square, and e
    3 min read
  • How to Make Animated Submit and Failed Buttons in Android?
    In Android applications, a Button is a user interface that is used to perform some action when clicked or tapped. It is a very common widget in Android and developers often use it. This article demonstrates how to create Animated Submit and Failed Buttons in Android Studio. What we are going to buil
    4 min read
  • How to change Input Method Action Button in Android?
    In this article, IME(Input Method Action) Option is changed in android according to our requirement. Input Method Action Button is located in the bottom right corner of the soft keyboard. By default, the system uses this button for either a Next or Done action unless your text field allows multi-lin
    2 min read
  • Theming Floating Action Button with Bottom Navigation Bar in Android
    In the previous article How to Add a Floating Action Button to Bottom Navigation Bar in Android?, it's well discussed how to add the Floating Action Button to the Bottom Navigation Bar in Android. Now to increase the UI/UX experience theming also can be done for the Bottom Navigation Bar. So in this
    4 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