0

I'm working on Lollipop & I know how to color the status bar by setting this flag FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS & setting the color by getWindow().setStatusBarColor(color);

But I have a image & a transparent ActionBar & I want to show the image behind the action bar & when the users scrolls the ListView below the image the status bar should get fill with color as ActionBar

The problem is showing image behind the status bar & later after scrolling filling status bar with color. I don't know how this effect can be achieved.

Any help would be appreciated

Akshay Chordiya
  • 4,761
  • 3
  • 40
  • 52

3 Answers3

0

first of all you have to add TranslucentActionBar Style on your style.xml

    <style name="AppTheme.TranslucentActionBar">
    <item name="android:actionBarStyle">@style/Widget.ActionBar.Transparent</item>
    <item name="android:windowActionBarOverlay">true</item>
    <item name="actionBarStyle">@style/Widget.ActionBar.Transparent</item>
    <item name="windowActionBarOverlay">true</item>
    <item name="android:windowContentOverlay">@null</item>
   <!--  <item name="windowContentOverlay">@null</item> -->
</style>

Add this method in your class.

@TargetApi(19) 
private void setTranslucentStatus(boolean on) {
    Window win = getWindow();
    WindowManager.LayoutParams winParams = win.getAttributes();
    final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
    if (on) {
        winParams.flags |= bits;
    } else {
        winParams.flags &= ~bits;
    }
    win.setAttributes(winParams);
}

Next, Add this in OnCreate method before setContentView()

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setTranslucentStatus(true);
        }

then status bar will be overlay your activity.

add scroll listener and add set status bar alpha method like this.

getWindow().setStatusBarColor(Color.argb(alpha, 253, 152, 0));
Junghoon Lee
  • 154
  • 4
0

You need to use the following flags to get the system bar transparent and draw over it:

activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);

As explained here in method 1: Lollipop : draw behind statusBar with its color set to transparent

After that, setting the system bar to transparent should do the trick (note that this is only available in API 21 or up):

getWindow().setStatusBarColor(Color.TRANSPARENT);

Then, for make the change when the screen scrolls, you need to use a scroll listener to switch between solid and transparent for the system bar with something like this:

getWindow().setStatusBarColor(getPrimaryDarkkColor());

and

getWindow().setStatusBarColor(Color.TRANSPARENT);

taking into account the scroll position in the listener.

Hope this helps,

Community
  • 1
  • 1
fpanizza
  • 1,589
  • 1
  • 8
  • 15
0

Simply copy this code in your onCreate method and keep coding :)

Code:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
                WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);


nestedScrollView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
            @Override
            public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                if (scrollY > 50) {
                    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
                    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
                    getWindow().setStatusBarColor(Color.rgb(85, 155, 247));
                } else {
                    getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
                            WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
                    getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
                }
            }
        });
    }