displaying my toolbar image as background to status bar effecting navigation bar in android
M

3

8

I want my toolbar to take the space of status bar and the image used for it be the background of the status bar and I successfully do that with this code inside my activity

//for using status bar space
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
            WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
}

The problem is this code make my activity layout take the full screen even the space with navigation bar so how to solve this?

Here is a screenshot:

Maitund answered 8/2, 2020 at 21:28 Comment(0)
A
10

This is the solution I used for testing on android 10 and other versions, so check if this is of any help.

In your styles.xml create a custom theme, you can set parent theme to what your app theme is if it has any of the NoActionBar (AppCompat/DayNight/material etc.) set as parent or set one directly(since without NoActionBar a default one will get generated on top), the major requirement is these three lines:

<style name="CustomTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <item name="android:windowTranslucentStatus">false</item>
    <item name="android:statusBarColor">#00000000</item>
</style>

You can check the documentation for statusBarColor if you wish, basically tells you what needs to be done.

The color for the status bar. If the color is not opaque, consider setting {@link android.view.View#SYSTEM_UI_FLAG_LAYOUT_STABLE} and {@link android.view.View#SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}. For this to take effect, the window must be drawing the system bar backgrounds with {@link android.R.attr#windowDrawsSystemBarBackgrounds} and the status bar must not have been requested to be translucent with {@link android.R.attr#windowTranslucentStatus}. Corresponds to {@link android.view.Window#setStatusBarColor(int)}.

So simply set a transparent color or any color you wish to set to the status bar as per your requirement. And set this theme to the activity you want.

Now in the activity create this method as shown below:

private void showCustomUI() {
        View decorView = getWindow().getDecorView();
        decorView.setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
    }

Those are the flags you require, don't add View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION while setting flags as it hides the navigation bar which your requirement is to show.

You'll need to call the method otherwise statusbar won't be created properly.

So try this, and let me know if it worked for you.

EDIT: Don't forget to remove FLAG_LAYOUT_NO_LIMITS flags incase you have them in activity as it will create problems. Here's what i got on testing for android 10.

enter image description here

Artema answered 12/2, 2020 at 17:49 Comment(3)
this works fine but I used the parent theme like this <style name="CustomTheme" parent="Theme.AppCompat.Light.NoActionBar"> to workMaitund
In my case the AppTheme was set to <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> so it was the same case, since you had coordinator layout with action bar i assumed your default app theme to have parent="Theme.AppCompat.Light.NoActionBar", but yeah you need to set NoActionBar so it doesn't show up on top. I'll edit the answer to clear the confusion.Artema
Important Note: remove android:fitsSystemWindows="true" from every layout in your xml file if you have.Gnathion
D
2

Method 1

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

styles.xml

<style name="TranslucentStatusBar" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowTranslucentStatus">true</item>
</style>

v21\styles.xml

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

status bar will be transparent or translucent, navigation bar won't

if setting windowTranslucentStatus to true won't work you can try this

Method 2

styles.xml ; just change the parent and don't add windowTranslucentStatus

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

override style for v21 & v23

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
     <item name="android:windowDrawsSystemBarBackgrounds">false</item>
</style>

and add this in your activity.class

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

reference from answer1, answer2

hope this helps!

Dado answered 11/2, 2020 at 7:29 Comment(1)
Method one works but I want the transparent color is zero as in the image with the questions it gives me dark transparent on the status bar on android 10,I will try it on other versions of android to see it's only happens on android 10, on the other hand method 2 have the same problem on android 10 it works fine only if you use gestures and removes systems navigation buttonsMaitund
G
0

By using these flags you can achieve that.

private void showSystemUI() {

    View decorView = getWindow().getDecorView();
    decorView.setSystemUiVisibility(
        View.SYSTEM_UI_FLAG_LAYOUT_STABLE
        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}

call this method.

Globular answered 11/2, 2020 at 10:25 Comment(1)
it works but the transparent color to status bar not zero transparent it dark transparent on android 10 will try it on other version of androidMaitund

© 2022 - 2024 — McMap. All rights reserved.