How to change Navigation Drawer menu group divider color programmatically in android

In this android programming source code example, we are going to change navigation view menu group divider color programmatically 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 change navigationview menu group divider color programmatically 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.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawerLayout"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:layout_below="@id/toolbar">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Navigation Drawer"
            android:gravity="center_horizontal|center_vertical"/>

        <android.support.design.widget.NavigationView
            android:id="@+id/shitstuff"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:headerLayout="@layout/drawer_header"
            app:itemTextColor="#000000"
            app:menu="@menu/drawermenu" />

    </android.support.v4.widget.DrawerLayout>


</RelativeLayout>

MainActivity.java

import android.support.design.internal.NavigationMenuView;
import android.support.design.widget.NavigationView;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.Toolbar;


public class NavigationViewActivity12 extends AppCompatActivity{
    private DrawerLayout mDrawerLayout;
    private NavigationView mNavigationView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_navigation_view12);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
        mNavigationView = (NavigationView) findViewById(R.id.shitstuff) ;
        mNavigationView.setItemBackground(getDrawable(R.drawable.navviewline));
        ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this,mDrawerLayout, toolbar,R.string.app_name, R.string.app_name);

        mDrawerLayout.addDrawerListener(mDrawerToggle);

        mDrawerToggle.syncState();

    }
}

res/drawable/navviewline.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>

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