1

I have seen this questions at many places but everywhere they have used a custom listview inside the navigation menu.

My code for navigationview is like this :

<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_insta_drawer"
        app:menu="@menu/activity_insta_drawer_drawer" />

where my @menu/activity_insta_drawer_drawer is like this :

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_applnform"
            android:icon="@drawable/application_form"
            android:title="Application Form"
            android:checkable="true"

            />
        <item
            android:id="@+id/nav_penddocs"
            android:icon="@drawable/pending_doc"
            android:title="Pending Documents"

            />
        <item
            android:id="@+id/nav_viewdocs"
            android:icon="@drawable/view_doc"
            android:title="View Documents" />
        <item
            android:id="@+id/nav_payfee"
            android:icon="@drawable/pay_free"
            android:title="Pay Fee" />
         <item
                android:id="@+id/nav_refereedetail"
                android:icon="@drawable/refree_detail"
                android:title="Referee Details" />
         <item
                android:id="@+id/nav_informcorner"
                android:icon="@drawable/information"
                android:title="Information Corner" />
        <item
            android:id="@+id/nav_changepass"
            android:icon="@drawable/change_pass"
            android:title="Change Password" />
        <item
            android:id="@+id/nav_postquery"
            android:icon="@drawable/ic_menu_send"
            android:title="Post Query" />
      </group>
</menu>

code for navigationview :

 navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

How to i change the selected item of the menu?

Parth Anjaria
  • 3,961
  • 3
  • 30
  • 62
  • Do you want to change the color or set it to checked? If checked, just use the method `MenuItem#setChecked`. You didn't upload your java code, so I have no idea how you using the navigation drawer. – OneCricketeer Mar 02 '16 at 05:20
  • i need to show that it is checked and i want to add a particular color to it, till now it was working fine, now suddenly i dont know how but the color changed, and since it was bydefult so i dont have idea how to change it back – Parth Anjaria Mar 02 '16 at 05:23
  • From my experience, nothing magically changes color. You may need to define a custom `selector` drawable to define both checked and unchecked states' colors – OneCricketeer Mar 02 '16 at 05:26
  • but where do i define it? and where do i add that selector? – Parth Anjaria Mar 02 '16 at 05:32
  • I don't know if menu items can define a selector, but in a regular View Xml, you'd set the `android:background` to an `@drawable/selector` and you'd define it in `res/drawable` – OneCricketeer Mar 02 '16 at 05:34
  • yeah that is my problem we cannot define selector or backgound to menu items? – Parth Anjaria Mar 02 '16 at 05:36
  • You can change the app theme from the `styles.xml` to get different background colors overall, but right, I don't think individual menu items can have custom background drawables – OneCricketeer Mar 02 '16 at 05:40
  • have you seen gmail navigation view? i want something similar, or you can also see this question : http://stackoverflow.com/questions/30967851/change-navigation-view-item-color-dynamicly-android – Parth Anjaria Mar 02 '16 at 05:45

2 Answers2

1

Finally after trying out everything i got the answer You must change the colorAccent in the colors file to which ever color you want :

  <color name="colorAccent">whichever color required</color>

This solution worked for me

Parth Anjaria
  • 3,961
  • 3
  • 30
  • 62
1

Try to change your NavigationView like below with adding app:itemTextColor="@drawable/nav_draw_selector" to change navigation menu text color:

<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:itemTextColor="@drawable/nav_draw_selector"
    app:headerLayout="@layout/nav_header_insta_drawer"
    app:menu="@menu/activity_insta_drawer_drawer" />

nav_header_insta_drawer.xml

 <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="#4281ca" android:state_checked="true" />     <!-- This is the default text color -->
    <item android:color="#DEDEDE" />
</selector>
pRaNaY
  • 24,642
  • 24
  • 96
  • 146