3

I was trying out the new material Bottom App bar. In design guidelines it's told "Inset a snackbar or toast above a bottom app bar and FAB" but I'm not sure to how to do it. Can anyone help me. Below are the codes that I'm using:

MainActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = findViewById(R.id.fab);
    fab.setOnClickListener(view ->
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show());
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </com.google.android.material.appbar.AppBarLayout>

    <include layout="@layout/content_main" />

    <com.google.android.material.bottomappbar.BottomAppBar
        style="@style/Widget.MaterialComponents.BottomAppBar"
        android:id="@+id/bar"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_gravity="bottom"
        android:backgroundTint="@color/colorAccent"
        app:fabAlignmentMode="end" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@android:drawable/ic_dialog_email"
        app:layout_anchor="@id/bar"
        app:layout_anchorGravity="bottom" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

styles.xml

<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.MaterialComponents.Dark" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.MaterialComponents.Light" />

gradle

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "..."
        minSdkVersion 22
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility = '1.8'
        targetCompatibility = '1.8'
    }
    buildToolsVersion = '28.0.2'
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.google.android.material:material:1.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'
}

The problem is that whenever I click the fab button the snackbar appears above the bottom app bar and also pushes the fab above. Below is the image for the problem.

material design snackbar plroblem

Bolt UIX
  • 5,988
  • 6
  • 31
  • 58
Rajarshi
  • 2,419
  • 3
  • 23
  • 36

4 Answers4

9

This was an issue which has been fixed. There's a new API setAnchorView which, I think, we will be getting on next update; for detail, head to: https://github.com/material-components/material-components-android/commit/868e80ed6ff183cceeb9c33523bf47f72772c52a

Rajarshi
  • 2,419
  • 3
  • 23
  • 36
5

Please use the setAnchorView() Method :-

Snackbar.make(CoordinatorLayoutView, "your text", Snackbar.LENGTH_SHORT)
                        .setAnchorView(BottomAppBarView or FabButtonView).show();
Pankaj Jain
  • 131
  • 1
  • 3
3

Please use the latest Material library:

Edit build.gradle (module: app) and update material:1.0.0 to 1.1.0-alpha04 or the latest:

implementation 'com.google.android.material:material:1.1.0-alpha04'
Manaus
  • 407
  • 5
  • 9
VasileM
  • 606
  • 7
  • 13
1
  1. Add a layout ID to your content_main layout so you can obtain a reference to it in your activity code via findViewById
  2. Instead of passing the fab view, use the content_main's layout view in Snackbar.make(view)

That should make your snackbar appear above the BottomAppBar.

josephus
  • 8,284
  • 1
  • 37
  • 57
  • To make this answer complete, check [this example](https://github.com/firatkarababa/BottomAppBarBehaviour) for that. – ʍѳђઽ૯ท Oct 01 '18 at 06:25
  • Thank you guys, I'll be testing it, it the meantime, can you guys help me to solve this question https://stackoverflow.com/questions/52575148/bottom-app-bar-show-hide-on-interacting-with-fragments-navigation-architecture – Rajarshi Oct 01 '18 at 07:15