Taig Taig - 2 months ago 126
Android Question

android:fitsSystemWindows not working

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

Answer

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.