7

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 have an offset for 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 it doesn't work, nothing happens:

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

How can I do that?

ilyamuromets
  • 403
  • 1
  • 7
  • 18

2 Answers2

19

I have been dealing with this issue and found only this unanswered question.

Finally I've found the solution:

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

Setting fitsSystemWindows to true modifies the padding. Apparently disabling fitsSystemWindows keeps the padding set.

Milos Fec
  • 828
  • 6
  • 11
  • 1
    Man this was the response I've looking for! But what if I want to setFitsSystemWindows to true again? – Simon Oct 25 '16 at 03:04
  • You can set it again to true. Just remember to clear padding every time when setting to false. – Milos Fec Oct 25 '16 at 17:09
0

Try this one

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        Window w = getWindow(); // in Activity's 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 Status flag to remove statusbar offset and Navigationbar to remove soft-buttons.

Kirankumar Zinzuvadia
  • 1,249
  • 10
  • 17