Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SnackBar and fitsSystemWindow

Tags:

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

like image 373
ligi Avatar asked Jul 18 '15 14:07

ligi


People also ask

What does fitsSystemWindows do?

fitsSystemWindows = true sets the padding to your view (if needed) to prevent your content from being obscured by the system status bar or navigation bar. Now our screen looks like that: How to avoid content obscure and add necessary paddings? To do so, Android provides WindowInsets.


1 Answers

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.

like image 184
ianhanniballake Avatar answered Oct 31 '22 17:10

ianhanniballake