2

As my question suggests, I am trying to figure out how to set my status bar color transparent while keeping my navigation bar black/natural color (without affecting the screen height) I referred to this site : Android Completely transparent Status Bar? one of the solutions that partially worked was :

getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

but that pushes my screen content below navigation bar making it transparent. I want the screen size to remain the same while keeping the navigation bar its natural color. Just want to make the status bar transparent, but seems like all other ways make it off-white even when I set it transparent with (setstatusbarcolor option).

Any ideas on how to go about it? My min SDK version is 23.

The first view without doing anything

The second view is the view after adding the code above where the status bar works but the screen slides below making the nav bar transparent as well (even setting fitsSystemWindows = false doesn't help).

enter image description here

Thanks in advance!

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98

2 Answers2

1

Try to do below mentioned things may help you in your example. Let me if you still facing issue. If it will solve your issue then please provided answer.

AndroidManifest.xml

<activity
   ....            
   android:theme="@style/TranslucentStatusBarTheme">
</activity>

values-v23\styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="TranslucentStatusBarTheme" parent="AppTheme">
        <item name="android:statusBarColor">@android:color/transparent</item>
        <item name="android:windowLightStatusBar">true</item>
    </style>
</resources>

Activity.java

private static void setWindowFlag(Activity activity, boolean on) {
    Window win = activity.getWindow();
    WindowManager.LayoutParams winParams = win.getAttributes();
    if (on) {
        winParams.flags |= WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
    } else {
        winParams.flags &= ~WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
    }
    win.setAttributes(winParams);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    // Check if the version of Android is Marshmallow or higher
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        int newUiOptions = getWindow().getDecorView().getSystemUiVisibility();
        newUiOptions ^= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;

        getWindow().getDecorView().setSystemUiVisibility(newUiOptions
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
    }
    // Check if the version of Android is Lollipop or higher
    else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        setWindowFlag(this, false);
        getWindow().setStatusBarColor(ContextCompat.getColor(this, R.color.colorTranslucent));
    }

    super.onCreate(savedInstanceState);
}
0

You need to add the following style in your activity theme.

In the styles.xml find the theme of your activity and add the following. As the minimum SDK version is 23 in your case, you do not need to worry about the previous versions of Android.

<item name="android:windowDrawsSystemBarBackgrounds">false</item>
<item name="android:windowTranslucentStatus">true</item>

Here is the screenshot of what I have got.

enter image description here

Here's the theme style that I have in my case.

<style name="NoActionBar" parent="Theme.AppCompat.NoActionBar">
    <item name="android:windowActionBar">false</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowDrawsSystemBarBackgrounds">false</item>
    <item name="android:windowTranslucentStatus">true</item>
</style>

Hope that solves your problem.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
  • will it apply throughout the app? I am looking to apply it to just one activity. Rest of the activities are fine. Let me know if this will affect anything else –  May 01 '19 at 16:32
  • Nope. This just affects one single activity. If you have the same theme attached to other activities, then it will affect the others. However, you may consider creating a separate theme for this activity having this kind of behaviors. – Reaz Murshed May 01 '19 at 16:42
  • the min sdk version for my app is 23 . do i still need to separate out values-v21? also, i added the above code in my styles and references that as style="@style/SignupActivity" in my activity. no luck. –  May 01 '19 at 16:47
  • Oh ... if you have the SDK 23, then add the code for the `values-21` in your normal `styles.xml` file and yes, of course, you do not need another folder for `v-21`. I have a similar setting here I suppose and its working in my case. – Reaz Murshed May 01 '19 at 16:49
  • strange.not sure why its causing an issue for me.have the same layout as you posted in the other question.i wonder if theres a missing link somewhere –  May 01 '19 at 17:26
  • I have added my test project here in Github - https://github.com/masudias/so-playground , please run the code to check if that works in your side. You can check the `MainActivity` and the theme used in the `MainActivity` in the `AndroidManifest.xml` file. – Reaz Murshed May 01 '19 at 17:31
  • Hi @MarissaNicholas, so are you still getting the problem? If you still have the problem, let me know. – Reaz Murshed May 02 '19 at 16:25
  • Hi Reaz I havent tested it today, will test it later and let you know if it works! –  May 02 '19 at 17:49
  • Great to know that helped. :) – Reaz Murshed May 03 '19 at 01:20