Home » Android » android – issue with windowTranslucentNavigation

android – issue with windowTranslucentNavigation

Posted by: admin May 14, 2020 Leave a comment

Questions:

i want to make my navigation bar translucent , so i did some thing like this :

 <style name="BlueToolBar" parent="Theme.AppCompat">
    <item name="windowActionBar">false</item>
    <item name="android:colorBackground">@color/action_blue</item>
    <item name="colorPrimary">@color/action_blue</item>
    <item name="colorPrimaryDark">@color/status_bar</item>
    <item name="android:windowTranslucentNavigation">true</item>
</style>

and it is working nicely, but my toolbar and status bar are merged,
merged toolbar

when i use this:

<style name="BlueToolBar" parent="Theme.AppCompat">
    <item name="windowActionBar">false</item>
    <item name="android:colorBackground">@color/action_blue</item>
    <item name="colorPrimary">@color/action_blue</item>
    <item name="colorPrimaryDark">@color/status_bar</item>
</style>

the resualt is :

nice toolbar

How to&Answers:

<item name="android:windowTranslucentNavigation">true</item> not only makes the Navigationbar translucent, it also causes the activity being drawn behind the statusbar.

To get around this you can set a margin at the top of your toolbar.

Getting the height of the Statusbar can be done via:

public static int getStatusBarHeight(Context context) {
    int result = 0;
    int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
    if (resourceId > 0) {
        result = context.getResources().getDimensionPixelSize(resourceId);
    }
    return result;
}

Now set the margin to the toolbar via this (if your ToolBar is in a LinearLayout):

((LinearLayout.LayoutParams) toolbar.getLayoutParams()).setMargins(0, getStatusBarHeight(this), 0, 0);

Answer:

there is a better solution:

just set android:fitsSystemWindows="true" to the xml of your parent layout. the system will take care of setting the proper paddingTop