Problem with Translucent Navigation bar in Android

Viewed 594

I'm using Translucent Navigation bar by adding below attribute to make my app mobile nav menu looks like some Google apps where mobile nav bar is a bit transparent and content will visible through it.

<item name="android:windowTranslucentNavigation">true</item>

But my output is itenter image description here

I don't want my views to go under it. I just need it when there is More content to scroll on screen like when there is a scrolling activity or in recyclerview. But at the end I need my activity views to not overlap by it.

Any suggestions please...

3 Answers

You may need to apply insets to have space between bottom of the device and navigationbar. RootView is the top layout on xml file.

 rootView.setOnApplyWindowInsetsListener { view, insets ->

        recyclerview.run {
            setPadding(paddingLeft,paddingTop,paddingRight, insets.systemWindowInsetBottom)
        }

        insets
    }

With this snippet you will have transparent navigationbar but your RecyclerView will always be above navigationBar.

And add

android:clipToPadding="false"

flag, this works with RecyclerView, and other scrollable views i guess.

You can check out this medium article or Chris Bane's tivi app to get familiar with insets.

Also put an example on github

enter image description here

What you could try for a static view is:

android:layout_marginBottom="?android:attr/actionBarSize"

If in a ScrollView this obviously won´t work but you can still try to play around wiht different layers inside the xml and fit a margin or padding if needed.

Otherwise it might be helpful if you post your xml file to actually see what you try to implement.

It is possible in both ScrollView and Recyclerview.

  • If there is scrollview in your Xml file then you should create two other layout(ex: LinearLayout1 as Scrollview child Layout and LinearLayout2 as LinearLayout1 child Layout) as a child layouts and set bottom margin in the Secode child layout(LinearLayout2) same as your bottom navigationBar height.

    Well, here is the example how you can get bottom navigationBar height.

                public int getNavigationBarHeight()
                 {
                     boolean hasMenuKey = ViewConfiguration.get(getApplicationContext()).hasPermanentMenuKey();
                     int resourceId = getResources().getIdentifier("navigation_bar_height", "dimen", "android");
                     if (resourceId > 0 && !hasMenuKey)
                     {
                         return getResources().getDimensionPixelSize(resourceId);
                     }
                     return 0;
                 }
    

Note: The height of the navigationBar is in px. So, you have to set margin of your child layout in px.

  • For Recyclerview, you can give bottom margin(RunTime) of parent layout in adapter class for the last item of the recyclerView same as navigationBar height.

Example:

               if (position==adapter_dueModelList.size()-1){// the list item
                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.WRAP_CONTENT,
                            LinearLayout.LayoutParams.WRAP_CONTENT
                    );
                    params.setMargins(0, 0, 0, 132);//give margin same as a navigationBar height.
                    ((ViewHolder) holder).linear_layout.setLayoutParams(params);
                }
Related