Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AppCompat Snackbar not centered on tablet

I'm trying to show a snackbar (via appcompat) to display a message to the user. It works perfectly fine on phones, however on tablets I'm getting

portrait

and

landscape

The code I'm using to generate the snackbar is

Snackbar.make(mHomeContainer, R.string.rate_snackbar, Snackbar.LENGTH_LONG)
        .setAction("Rate", ...)
        .show();

Any guidance on how to make this snackbar centered would be greatly appreciated

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <View
        android:id="@+id/status_bar"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="@color/main"
        android:elevation="8dp"/>

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

    <RelativeLayout
        android:id="@+id/home_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <FrameLayout
            android:id="@+id/main_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentBottom="true" />

        <ListView
            android:id="@+id/home_search_list"
            android:visibility="gone"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#88000000"/>

    </RelativeLayout>
</LinearLayout>

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

</android.support.v4.widget.DrawerLayout>
like image 721
Tim Mutton Avatar asked Jun 19 '15 13:06

Tim Mutton


2 Answers

You need to use the CoordinatorLayout in order to make this happen. Here is an article that describes how to do that. Additionally you will likely need to now provide layout files that you want to use for non-tablets vs. tablets so that you can retain full-width for your phones. This is also the layout component that you need to use if you use a combination of FAB, app bar and Snackbar.

like image 86
AkiAki007 Avatar answered Sep 22 '22 13:09

AkiAki007


In v22.2.0 of Design Support Library, Snackbar's default gravity was just Gravity.BOTTOM.

In v22.2.1 this issue was fixed: https://code.google.com/p/android/issues/detail?id=176383

like image 25
straya Avatar answered Sep 20 '22 13:09

straya