How to add dividers in Bottom Navigation View in Android

In this android programming source code example, we are going to add dividers in bottom navigation view in android.

You can copy and adopt this source code example to your android project without reinventing the wheel.

Below is a step by step source code to add dividers in bottom navigation view in android.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Bottom Navigation"
        android:gravity="center" />

    <View
        android:layout_width="match_parent"
        android:layout_height="3dp"
        android:layout_above="@id/bottom_nav"
        android:background="@drawable/shadow"/>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottom_nav"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_gravity="bottom"
        android:background="@android:color/white"
        app:itemBackground="@drawable/navviewline20"
        app:menu="@menu/bottombar_menu" />

</RelativeLayout>

res/drawable/navviewline20.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape
            android:shape="rectangle">
        <solid
            android:color="#ffffff" />
        </shape>
    </item>

    <item
        android:top="-2dp"
        android:right="-2dp"
        android:left="-2dp">
        <shape>
    <solid
        android:color="@android:color/transparent" />
    <stroke
        android:width="1dp"
        android:color="@color/colorAccent"/>
        </shape>
    </item>

</layer-list>

MainActivity.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;


public class NavigationViewActivity20 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_navigation_view20);
    }
}

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

 

Add a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.