2

I'm trying to make an Android activity that bleeds into the notification area as shown below. I have a solution, but I don't like it because it's pure magic -- I have no idea why it works, it just does. In the code sample below, I have a CoordinatorLayout holding an AppBarLayout holding a CollapsingToolbarLayout. If any of this is removed or changed in any way, the effect no longer works. The LinearLayout at the bottom contains the actual code layout info I want to use.

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    >
    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        >
        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />
    </android.support.design.widget.AppBarLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        >
        <ImageView
            android:id="@+id/backdrop"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"
            />
    </LinearLayout>
</android.support.design.widget.CoordinatorLayout>

Screenshot of an image bleeding into the notification area

Cœur
  • 37,241
  • 25
  • 195
  • 267
JohnnyLambada
  • 12,700
  • 11
  • 57
  • 61
  • Could this answer help you? http://stackoverflow.com/questions/22192291/how-to-change-the-status-bar-color-in-android – Jackpile Sep 06 '15 at 23:39
  • @Teddis No, that questions is more about the color of the status bar. In my case, I want the status bar to be transparent, and more importantly, I want the top of my activity to appear under the status bar as the example above shows. – JohnnyLambada Sep 07 '15 at 14:43

2 Answers2

1

2 ways to do this:

Method #1: onCreate method:

getWindow().getDecorView().setSystemUiVisibility(
    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
getWindow().setStatusBarColor(Color.TRANSPARENT);

Method #2:

getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getWindow().setStatusBarColor(Color.TRANSPARENT);

For me, method #1 is better because it does not set navigation bar transparent

Norutan
  • 1,480
  • 2
  • 14
  • 27
  • For API 21 and above, the `setStatusBarColor` does not seem to be necessary. It is unavailable for API levels below 21. – JohnnyLambada Sep 07 '15 at 14:38
  • @JohnnyLambada its already transparent and you don not need to set, but when you want to set status bar with some color, you will need it – Norutan Sep 08 '15 at 02:23
1

You could set the SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN flag:

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

Then, the only thing that you need in your layout is the ImageView. No fitsSystemWindows required.

<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/backdrop"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        />
tachyonflux
  • 20,103
  • 7
  • 48
  • 67
  • This works on versions 21 (Lollipop) and above. It will compile and execute on previous versions, but the status bar will not be transparent and will cover the top of the activity. As far as I can tell, it's only possible to make this work on Lollipop and above. Thanks! – JohnnyLambada Sep 07 '15 at 14:37