Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How To Avoid Snackbar Overlap Floating Action Button in Android?

  • Last Updated : 16 Oct, 2021


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:

The 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 ‘’

  • 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:

Gradle file

Step 3: Add the FAB icons to the Drawable 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.

Vector Asset

  • Now select your vector icon:

 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:


<?xml version="1.0" encoding="utf-8"?>
        android:text="SHOW SNACKBAR"
        tools:ignore="MissingConstraints" />
    <!--Add simple Parent Floating Action Button-->
        app:layout_constraintEnd_toEndOf="parent" />


The following output UI is produced:


Output UI


Step 5: Working with file

  • Now resolve the issue programmatically in 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.



import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
    Button mSnackBarButton;
    FloatingActionButton mAddFab;
    protected void onCreate(Bundle savedInstanceState) {
        // Register the show snackbar button with appropriate ID
        mSnackBarButton = findViewById(;
        // also register the floating action button with appropriate ID
        mAddFab = findViewById(;
        // Build and show the simple Snackbar with action button on it
        mSnackBarButton.setOnClickListener(new View.OnClickListener() {
            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.setAction("OKAY", new View.OnClickListener() {
                    public void onClick(View view) {
                        // Do appropriate action on click of snackbar action button


Output: Run on Emulator


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!