Search code examples
androidandroid-relativelayoutandroid-snackbar

Android moving buttons upwards when showing snack bar


I want to move my buttons upwards when showing snack bar, so it won't hide them. I've tried different solutions from stack overflow but I can't come up to the solution. This is my layout file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:paddingLeft="0dp"
    android:paddingRight="0dp" >

    <include
        android:id="@+id/header"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        layout="@layout/fragment_expiry_dates_list_header" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/products_list"
        android:name="com.example.expirydate.ui.main.ExpiryDatesFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/header"
        android:layout_above="@id/buttonNewOrOpened"
        android:layout_marginLeft="0dp"
        android:layout_marginRight="0dp"
        android:layout_marginBottom="3dp"
        app:layoutManager="LinearLayoutManager"
        tools:context=".expirydatefragment.ExpiryDatesFragment"
        tools:listitem="@layout/fragment_expiry_dates_list_item" />

    <Button
        android:id="@+id/buttonNewOrOpened"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerInParent="false"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:enabled="false"
        android:text="New" />

    <Button
        android:id="@+id/buttonUsed"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_toEndOf="@id/buttonNewOrOpened"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:enabled="false"
        android:text="Used" />

    <ImageButton
        android:id="@+id/buttonScanner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_gravity="center"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:layout_toStartOf="@id/buttonAdd"
        android:background="@drawable/roundcorner"
        android:padding="8dp"
        android:src="@android:drawable/ic_menu_gallery"
        android:contentDescription="scan product" />

    <ImageButton
        android:id="@+id/buttonAdd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_gravity="center"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:padding="8dp"
        android:background="@drawable/roundcorner"
        android:src="@android:drawable/ic_input_add"
        android:contentDescription="add product" />

</RelativeLayout>

What I would like to do is to move those 4 buttons on the bottom above snack bar. Code for showing snack bar:

Snackbar snackbar = Snackbar.make(getActivity().findViewById(android.R.id.content), "msg", Snackbar.LENGTH_SHORT);
snackbar.setAction("Undo", v -> {});
snackbar.show();

Currently it looks like this (there are 4 buttons hide on the bottom): current look

I've tried several solutions with adding my own layout_behavior or adding app:insetEdges="bottom", but nothing worked.


Solution

  • You were nearly in the right way when try using app:insetEdges="bottom". But for first insetEdges and layout_dodgeInsetEdges work properly only with the CoordinatorLayout. You need to change your RelativeLayout to CoordinatorLayout. After that group your buttons in one layout, for example it can be RelativeLayout and add app:layout_dodgeInsetEdges="bottom" and android:layout_gravity="bottom" to it. Since Snackbar by default has app:insetEdges="bottom" you were done only setting that properties to buttons container.

    To make CoordinatorLayout work properly it's also supposed that your header is wrapped in AppBarLayout and add app:layout_behavior="@string/appbar_scrolling_view_behavior" to RecyclerView. Here I can provide the template for this solution.

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/container"
        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" >
    
        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <!-- Your header here or your header need to be already wrapped in AppBarLayout -->
    
        </com.google.android.material.appbar.AppBarLayout>
    
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/products_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layoutManager="LinearLayoutManager"
            tools:context=".expirydatefragment.ExpiryDatesFragment"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
    
        <RelativeLayout
            android:id="@+id/buttonsContainer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_dodgeInsetEdges="bottom"
            android:layout_gravity="bottom">
    
            <!--Your Buttons here-->
    
        </RelativeLayout>
    
    </androidx.coordinatorlayout.widget.CoordinatorLayout>