ligi ligi - 3 months ago 20
Android Question

SnackBar and fitsSystemWindow

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

Answer

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.