简体   繁体   中英

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

在此处输入图片说明

you can set selected manually

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

or you can use

navigationView.setCheckedItem(menuItemid);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM