android – SnackBar and fitsSystemWindow-ThrowExceptions

Exception or error:

I have an app that uses fitsSystemWindows to be able to draw an background behind Navigation- and Status-Bar – unfortunately the SnackBar seems to ignore the fitsSystemWindows=true from the container. I boiled the problem down to this minimal app:

problem

the style:

<resources>

    <style name="AppTheme" parent="Theme.AppCompat.Light">

        <item name="android:windowBackground">@color/accent_material_dark</item>
        <item name="android:fitsSystemWindows">false</item>

        <item name="android:windowTranslucentStatus">true</item>
        <item name="android:windowTranslucentNavigation">true</item>
    </style>

</resources>

the layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"

                android:fitsSystemWindows="true"
                tools:context=".MainActivity">

    <Button

            android:id="@+id/button"
            android:text="@string/hello_world"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

</RelativeLayout>

the activity:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                Snackbar.make(v,"This Snackbar should fitSystemWindows",Snackbar.LENGTH_INDEFINITE).show();
            }
        });
    }

}

Anyone knows some workaround?

I published the minimal app to show the problem here:
https://github.com/ligi/SnackBarFitsSystemWindowProblem

How to solve:

Snackbar will always look for a CoordinatorLayout to anchor itself in: when you don’t have one, it uses the full content view (which in your case, includes the area under the status bar) – adding a CoordinatorLayout that has fitsSystemWindows=true should do it.

###

You can also use the following line in place of any CoordinatorLayout in Snackbar declaration with findViewById(android.support.design.R.id.design_navigation_view) .

###

Well The correct answer is add
android:paddingbottom="50dp" to your CoordinatorLayout
although i’m not sure if all navigation bar size’s is 50 dp

Leave a Reply

Your email address will not be published. Required fields are marked *