shehzy shehzy - 1 month ago 13
Android Question

Changing color of navigation drawer item before clicking it

I am changing the color of selected item of the navigation drawer and that is working fine through the following code

<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:itemTextAppearance="@style/NavDrawerTextStyle"
app:menu="@menu/activity_main_drawer"
app:itemTextColor="@color/nav_item_color"
android:background="#000000"/>

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


following is the code line doing the trick for me

app:itemTextColor="@color/nav_item_color"


code of xml from color directory

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#81DDFF" android:state_checked="true" style="@style/NavDrawerTextStyle"/>
<item android:color="#FFFFFF" style="@style/NavDrawerTextStyle"/>
</selector>


Now the problem for me is I want to change the color of first item (before it is clicked as the screen corresponding to first item is there) of Navigation drawer when the drawer opens for the first time.

In other words how can I change the color of a single navigation drawer item from my activity class? Thank you

Here is the screenshot when drawer opens for the first time and I wanted the color of login item light blue



When login item is clicked and drawer opens again

enter image description here

Answer

you can set selected manually

navigationView.getMenu().getItem(0).setChecked(true);

or you can use

navigationView.setCheckedItem(menuItemid);
Comments