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. 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:
- 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 click on the Sync Now button make sure that you are connected to network, so that it can download the required files.
- Refer to the below image if you can’t get the steps mentioned above:
Step 3: Add the FAB icons to the Drawble file
- For demonstration purpose 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.
- Now select your vector icon:
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:
The following output UI is produced:
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.
Output: Run on Emulator
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Floating Action Button (FAB) in Android with Example
- Extended Floating Action Button in Android with Example
- How to Add a Floating Action Button to Bottom Navigation Bar in Android?
- Theming Floating Action Buttons in Android with Example
- How to add a Snackbar in Android
- Snackbar Material Design Components in Android
- How to change Input Method Action Button in Android?
- How to change the color of Action Bar in an Android App?
- Different ways to hide Action bar in Android with Examples
- Assigning long values carefully in Java to avoid overflow
- How to avoid NullPointerException in Java using Optional class?
- How to add Toggle Button in an Android Application
- How to Change the Screen Orientation Programmatically using a Button in Android?
- How to Listen for Volume Button and Back Key Events Programmatically in Android?
- Double-Tap on a Button in Android
- How to Change the Background Color of Button in Android using ColorStateList?
- Floating Point Operations & Associativity in C, C++ and Java
- Check if a given string is a valid number (Integer or Floating Point) in Java
- Problem in comparing Floating point numbers and how to compare them correctly?
- Java Program to Illustrate the Usage of Floating
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.