android:fitsSystemWindows not working-ThrowExceptions

Exception or error:

In my theme I defined the following rules to draw my views behind the status bar:

<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>

And in the Activity (onCreate):

getWindow.getDecorView.setSystemUiVisibility(
    View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
)

Then, in my View im using a Barcodescanner that is supposed to draw behind the status bar, which is working. However when I apply the android:fitsSystemWindows to any child view they don’t get their position adjusted. It’s working when I apply it to the root element, though.

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content">

    <my.Scanner
        android:id="@+id/scanner"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <FrameLayout android:layout_width="match_parent"
                 android:layout_height="match_parent"
                 android:fitsSystemWindows="true"> <!-- Should receive a top padding, right? -->

        <View android:layout_width="50dp"
              android:layout_height="50dp"
              android:background="@color/red" />

    </FrameLayout>

</FrameLayout>

Screenshot

How to solve:

Use CoordinatorLayout as root for your view, it worked for me.

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

###

My problem was most likely related to multiple/nested fitsSystemWindows attributes which does not work as I found out later. I solved my problem by applying the attribute to one view, and then copy the paddings to other views that need them via an ViewTreeObserver.OnGlobalLayoutListener. That is an ugly hack, but does its job for now.

###

I meet the same problem in Android 4.4

My problem is <item name="android:windowTranslucentStatus">true</item>

conflict with <item name="android:fitsSystemWindows">true</item>

My way is remove
<item name="android:windowTranslucentStatus">true</item>

###

for me the problem was that the keypad hides an editText field which is placed at the end of the activity ..I have tried every solution out there and nothing works.
I was using this code before the call to setContentView() method to make the status bar transparent:

   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window w = getWindow(); // in Activity's onCreate() for instance
            w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        }

I removed it and also removed android:windowSoftInputMode="adjustResize|adjustPan" from the maneifest file and now it works.

###

Make this changes

1. Manifest - create a theme and add window background of your like
2. toolbar in xml -  add the same background in toolbar

this works for me.

###

There is an easy way to do this. Make sure you set the 2 elements in styles of the theme applied to your activity.

<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
    ....
    <item name="colorPrimaryDark">@android:color/transparent</item>
    <item name="android:fitsSystemWindows">true</item>
    ...
</style>

No need to do add android:fitsSystemWindows in your layout. This will directly add to it.

Leave a Reply

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