4

My activity requests layout as fullscreen:

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

and in the xml layout sets the fitsSystemWindows property to padding the statusbar height:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

<!-- The main content view -->

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

            <android.support.design.widget.AppBarLayout
                android:id="@+id/appbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fitsSystemWindows="true">

                <android.support.design.widget.CollapsingToolbarLayout
                    android:id="@+id/collapsing_toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
                    app:contentScrim="@color/actionbar_title_color"
                    android:fitsSystemWindows="true">

Compiled with appcompat-v7:23.2.1, it works well on API21 device but on API16 device the padding is not applied. Any hints?

UPDATE

Bounty on: why on API16 device the padding is not applied?

Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
GPack
  • 2,494
  • 4
  • 19
  • 50

1 Answers1

-1

From official document Android development pattern

On KitKat and below, your custom View could override fitSystemWindows() and provide any functionality you wanted — just return true if you’ve consumed the insets or false if you’d like to give other Views a chance.

However, on Lollipop and higher devices, we provide some new APIs to make customizing this behavior much easier and consistent with other behaviors for Views. You’ll instead override onApplyWindowInsets(), which allows the View to consume as much or as little of the insets as you need and be able to call dispatchApplyWindowInsets() on child views as needed.

you don’t even need to subclass your Views if you only need custom behavior on Lollipop and higher you can use ViewCompat.setOnApplyWindowInsetsListener(), which will be given preference over the View’s onApplyWindowInsets(). ViewCompat also provides helper methods for calling onApplyWindowInsets() and dispatchApplyWindowInsets() without version checking.

Now, You need to check with condition that which api level is used and according to that apply condition like

if(API < Kitkat){
    fitSystemWindows()
}else if(API > Lollipop){
    onApplyWindowInsets()
}

Please check this answer for more details.

Also read http://developer.android.com/reference/android/view/View.html#fitSystemWindows%28android.graphics.Rect%29

Community
  • 1
  • 1
Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
  • thus, on API16 should I override fitSystemWindows()? I dont understand why, as the default implementation should work to add the needed padding. – GPack Mar 29 '16 at 09:42
  • if that is not working than you need to apply condion that if api is this(KitKat or below) that code for that else code for api level Lollipop – Amit Vaghela Mar 29 '16 at 09:46
  • thanks, I know all these posts, but the issue here is how to get working on API 16 the 'android:fitsSystemWindows="true"' as docs says. – GPack Mar 29 '16 at 12:26
  • as i have told you, you have to check condition written in my answer and it will work for sure. – Amit Vaghela Mar 29 '16 at 12:30
  • sorry, I dont see any solution in your code: `fitSystemWindows()` and `onApplyWindowInsets()` are to be overridden in a customized view to set its own behavior, not called. I have not customized view, I have a DrawerLayout that should honour the request to padding the statusbar's height and pass it to a CoordinatorLayout. – GPack Mar 29 '16 at 12:45