Mostafa Jamareh
Mostafa Jamareh

Reputation: 1439

issue with windowTranslucentNavigation

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

Upvotes: 12

Views: 8647

Answers (2)

carlo.marinangeli
carlo.marinangeli

Reputation: 3038

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

Upvotes: 22

Thorben
Thorben

Reputation: 1029

<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);

Upvotes: 9

Related Questions