Android Developer
Android Developer

Reputation: 125

Not able to change color of navigation menu icons in android

I am creating a navigation bar in my app. And according to requirement I had placed menu icons on right side of navigation view but problem is this I am not able to change color of of menu icon while I select menu item. And if icons are on left side then icons color changes. So I want to know how can I change Navigation menu icon color while they are on right side.

menu.xml file :

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<group android:checkableBehavior="single">
<item
    android:id="@+id/home"
    android:title="@string/home" />

<item
    android:id="@+id/my_profile"
    android:title="@string/my_profile" />

Code menu_home.xml

This file is used to add image on right on navigation view.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical">

    <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/home"/>

</LinearLayout>

Navigation View Activity Code

Here I have set code to align the image to right of navigation view.

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.getMenu().getItem(0).setActionView(R.layout.menu_home);
navigationView.getMenu().getItem(1).setActionView(R.layout.menu_profile);
navigationView.setNavigationItemSelectedListener(this);

Image of navigation drawer right side icon color not changed

Upvotes: 1

Views: 768

Answers (1)

Siddharth Patel
Siddharth Patel

Reputation: 205

First of all you need to use two image one is without color and second is with color(what color you want).

add that two image in drawable folder then

firstly default image(without color) set. when you click navigation item that time set color image.

so you did see color is change of image according to navigation icon color.

Try like this :

@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();
    img.setImageDrawable(getResources().getDrawable(R.mipmap.ic_launcher));
    if (id == R.id.nav_camera) {
        // Handle the camera action
        img.setImageDrawable(getResources().getDrawable(R.drawable.ic_arrow_back));
    } else if (id == R.id.nav_gallery) {

    }
}

Example Navigation Drawer xml

<android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer">

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

            <ImageView
                android:id="@+id/img"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentTop="true"
                android:layout_marginEnd="28dp"
                android:layout_marginRight="28dp"
                android:layout_marginTop="195dp"
                android:src="@mipmap/ic_launcher" />
        </RelativeLayout>
</android.support.design.widget.NavigationView>

Upvotes: 0

Related Questions