Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set elevation to Bottom navigation

So by the support V25. We have new component called Bottom navigation.

Follow the Design guidelines, the Bottom Navigation's elevation should be 8dp (https://material.io/guidelines/components/bottom-navigation.html#bottom-navigation-specs)

But I can't set the elevation to it.

Any suggestion, example would be appreciated. Thank you!

UPDATE XML CODE

<RelativeLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent">

<android.support.design.widget.BottomNavigationView
    android:id="@+id/bottom_navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:elevation="8dp"
    app:elevation="8dp"
    app:itemBackground="@color/colorPrimary"
    app:itemIconTint="@drawable/bottom_nav_color_state"
    app:itemTextColor="@drawable/bottom_nav_color_state"
    app:menu="@menu/bottom_navigation_main"/>

<FrameLayout
    android:id="@+id/contentFrame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@id/bottom_navigation"
    android:background="#EDEDED"/>

like image 930
Banana droid Avatar asked Jan 24 '17 08:01

Banana droid


People also ask

How do I set my navigation to the bottom?

To create a Menu Resource File , click on the app -> res -> menu(right-click) -> New -> Menu Resource File and name it bottom_nav_menu. Now the user can create as many items as he wants in the bottom_nav_menu. xml file. The user also needs to create an icon for each of these items.

What is the height of bottom navigation bar android?

Because snackbars have a lower (6dp) elevation, they appear behind the bottom navigation bar (8dp elevation).

What is bottom navigation?

↳ com.google.android.material.bottomnavigation.BottomNavigationView. Represents a standard bottom navigation bar for application. It is an implementation of material design bottom navigation. Bottom navigation bars make it easy for users to explore and switch between top-level views in a single tap.

What is the bottom bar on apps called?

As the name depicts, the navigation bar is designed and placed at the extreme bottom of the app. As per standard, it usually covers the entire horizontal space, running from left to right, at the bottom of an app screen.


2 Answers

So, for now (25.1.0) we have to set the android:background of BNV to @android:color/white to have the shadow. It will not display if you set to other color (ie your primary color)

like image 164
Banana droid Avatar answered Sep 17 '22 21:09

Banana droid


I had the same issue and to have a @android:color/white as OP suggested was not acceptable in my case. So since we "can't" get shadow with elevation and custom background we need to hack it.

My approach is adding a shadow view inside the frame layout to "mimic" elevation.

<RelativeLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent">

<android.support.design.widget.BottomNavigationView
    android:id="@+id/bottom_navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:elevation="8dp"
    app:elevation="8dp"
    app:itemBackground="@color/colorPrimary"
    app:itemIconTint="@drawable/bottom_nav_color_state"
    app:itemTextColor="@drawable/bottom_nav_color_state"
    app:menu="@menu/bottom_navigation_main"/>

<FrameLayout
    android:id="@+id/contentFrame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@id/bottom_navigation"
    android:background="#EDEDED"/>

    <some.kind.of.pager.or.other.content.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <View
        android:id="@+id/shadow_view"
        android:layout_width="match_parent"
        android:layout_height="4dp"
        android:layout_gravity="bottom"
        android:background="@drawable/shadow_gradient" />

</FrameLayout>

where the background of the shadow view is nothing more than a shape gradient that is positioned over all other just above the bottom nav view, something like:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:angle="90"
        android:endColor="@android:color/transparent"
        android:startColor="#8f000000" />
</shape>

Hope this helps someone.

like image 44
bko Avatar answered Sep 17 '22 21:09

bko