Android: fitsSystemWindows at runtime

My Android app starts with the following layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click"/>
</RelativeLayout>

Activity has an offset for the status bar.

I want to remove this offset at runtime by clicking on the button. By clicking on the button, I do the following, but this does not work, nothing happens:

findViewById(R.id.root).setFitsSystemWindows(false);

How can i do this?

+4
source share
2 answers

I deal with this problem and found only this unanswered question.

Finally, I found a solution:

View view = findViewById(R.id.root);
view.setFitsSystemWindows(false);
view.setPadding(0, 0, 0, 0);

Setting fitsSystemWindows to true changes the addition. Obviously disabling fitsSystemWindows supports a set of add-ons.

+11
source

Try this one

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

Set the status flag to remove the status bar offset and the navigation bar to remove the soft buttons.

0

All Articles