Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 550
Mostafa Jamareh Avatar asked Dec 04 '14 10:12

Mostafa Jamareh


2 Answers

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

like image 166
carlo.marinangeli Avatar answered Oct 21 '22 02:10

carlo.marinangeli


<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);
like image 28
Thorben Avatar answered Oct 21 '22 03:10

Thorben