Changing Text Color of Menu Item in Navigation Drawer

Changing text color of menu item in navigation drawer

 <android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:background="#000"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/header"
app:itemTextColor="your color"
app:menu="@menu/drawer" />

How to change the text and icon color of selected menu item on Navigation Drawer programmatically using java

Firstly thank you all for responding back with your solutions :) Learning from the answers above and doing some research on ColorStateList I finally managed to create a method which sets the color of the checked item on the navigation drawer to match the color of my app theme color which is generated randomly at runtime.

Here's the method:

public void setNavMenuItemThemeColors(int color){
//Setting default colors for menu item Text and Icon
int navDefaultTextColor = Color.parseColor("#202020");
int navDefaultIconColor = Color.parseColor("#737373");

//Defining ColorStateList for menu item Text
ColorStateList navMenuTextList = new ColorStateList(
new int[][]{
new int[]{android.R.attr.state_checked},
new int[]{android.R.attr.state_enabled},
new int[]{android.R.attr.state_pressed},
new int[]{android.R.attr.state_focused},
new int[]{android.R.attr.state_pressed}
},
new int[] {
color,
navDefaultTextColor,
navDefaultTextColor,
navDefaultTextColor,
navDefaultTextColor
}
);

//Defining ColorStateList for menu item Icon
ColorStateList navMenuIconList = new ColorStateList(
new int[][]{
new int[]{android.R.attr.state_checked},
new int[]{android.R.attr.state_enabled},
new int[]{android.R.attr.state_pressed},
new int[]{android.R.attr.state_focused},
new int[]{android.R.attr.state_pressed}
},
new int[] {
color,
navDefaultIconColor,
navDefaultIconColor,
navDefaultIconColor,
navDefaultIconColor
}
);

mNavView.setItemTextColor(navMenuTextList);
mNavView.setItemIconTintList(navMenuIconList);
}

you can call this method with any int color you want :)

Navigation Drawer Menu Item Title Color in Android

To give Menu item title color and textSize Create this way..

add this to your styles.xml file.

 <style name="TextAppearance44">
<item name="android:textColor">#FF0000</item>
<item name="android:textSize">20sp</item>
</style>

now in activity_main_drawer.xml file give id attribute to title..

<item android:title="Tools"
android:id="@+id/tools">
<menu>
<item
android:id="@+id/nav_settings"
android:icon="@mipmap/settings"
android:title="Settings" />
<item
android:id="@+id/nav_help"
android:icon="@mipmap/information"
android:title="Help" />
<item
android:id="@+id/nav_about"
android:icon="@mipmap/team"
android:title="About Us" />
</menu>
</item>

now In MainActivity.java file use this way..

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

Menu menu = navigationView.getMenu();

MenuItem tools= menu.findItem(R.id.tools);
SpannableString s = new SpannableString(tools.getTitle());
s.setSpan(new TextAppearanceSpan(this, R.style.TextAppearance44), 0, s.length(), 0);
tools.setTitle(s);
navigationView.setNavigationItemSelectedListener(this);

It will change your tools color to Red and TextSize to 20sp.. Implement it..

In my case Output :

Sample Image

Changing text color of menu heading title item in navigation drawer

Add <item name="android:textColorSecondary">@color/white</item> inside your AppTheme and AppTheme.NoActionBar styles.xml file like this:

<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>

<item name="android:textColorSecondary">@color/white</item>
</style>

<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>

<item name="android:textColorSecondary">@color/white</item>
</style>
...
</resources>

where the color value <color name="white">#FFFFFF</color> is placed in colors.xml.

The result looks exactly like you wanted:

Sample Image

Navigation Drawer: How to change the colour of item category?

Try this

create a theme like this

<style name="MyTheme">
<item name="android:textColor">#0000ff</item>
</style>

than make your menu 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_camera"
android:icon="@drawable/ic_menu_camera"
android:title="Import" />
<item
android:id="@+id/nav_gallery"
android:icon="@drawable/ic_menu_gallery"
android:title="Gallery" />
<item
android:id="@+id/nav_slideshow"
android:icon="@drawable/ic_menu_slideshow"
android:title="Slideshow" />
<item
android:id="@+id/nav_manage"
android:icon="@drawable/ic_menu_manage"
android:title="Tools" />
</group>

<item android:title="Communicate"
android:id="@+id/communicate">
<menu>
<item
android:id="@+id/nav_share"
android:icon="@drawable/ic_menu_share"
android:title="Share" />
<item
android:id="@+id/nav_send"
android:icon="@drawable/ic_menu_send"
android:title="Send" />
</menu>
</item>

</menu>

than Java code

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();

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

// code for changes colour of Communicate
Menu myMenu = navigationView.getMenu();
MenuItem tools= myMenu.findItem(R.id.communicate);
SpannableString s = new SpannableString(tools.getTitle());
s.setSpan(new TextAppearanceSpan(this, R.style.MyTheme), 0, s.length(), 0);
tools.setTitle(s);
navigationView.setNavigationItemSelectedListener(this);
}

navigation view

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

OUTPUT

Sample Image

Change the color of a checked menu item in a navigation drawer

Well you can achieve this using Color State Resource. If you notice inside your NavigationView you're using

app:itemIconTint="@color/black"
app:itemTextColor="@color/primary_text"

Here instead of using @color/black or @color/primary_test, use a Color State List Resource. For that, first create a new xml (e.g drawer_item.xml) inside color directory (which should be inside res directory.) If you don't have a directory named color already, create one.

Now inside drawer_item.xml do something like this

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="checked state color" android:state_checked="true" />
<item android:color="your default color" />
</selector>

Final step would be to change your NavigationView

<android.support.design.widget.NavigationView
android:id="@+id/activity_main_navigationview"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/drawer_header"
app:itemIconTint="@color/drawer_item" // notice here
app:itemTextColor="@color/drawer_item" // and here
app:itemBackground="@android:color/transparent"// and here for setting the background color to tranparent
app:menu="@menu/menu_drawer">

Like this you can use separate Color State List Resources for IconTint, ItemTextColor, ItemBackground.

Now when you set an item as checked (either in xml or programmatically), the particular item will have different color than the unchecked ones.

Change text color of a portion of navigation drawer item

Use a spannable string:

MenuItem item = (MenuItem) navigation.getMenu().findItem(R.id.miItem);
String str = item.getTitle().toString();
SpannableString span = new SpannableString(str);
span.setSpan(new ForegroundColorSpan(Color.RED), str.length() - 3, str.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
item.setTitle(span);

replace

navigation with your NavigationView variable

miItem with the corresponding MenuItem's id

How do I change Item title font color for an Item that contains a menu in Navigation Drawer

To change the menu title instead of the individual item titles I just used html tags in the strings resource for color The title and referenced the strings xml as the title. This can be done for doing individual colors for each item also.

<string name="testTitle"><font color="#303F9F">The title</font></string>

<group
android:id="something"
android:checkableBehavior="single>
<item
android:title="@string/testTitle"> <--This is what I want to change-->
<menu>
<item android:title="stuff"/> <--This i can change-->
<item android:title="stuff"/>
</menu>
</item>

I also noticed that this doesn't work if you have secondary text color in set in your styles.

Changing colors of Navigation Drawer menu items

There are 2 ways to customize the navigation drawer menu items individually.

First way:

MenuItem menuItem = navigationView.getMenu().findItem(R.id.menu_item);
SpannableString s = new SpannableString(menuItem.getTitle());
s.setSpan(new ForegroundColorSpan(TEXT_COLOR), 0, s.length(), 0);
s.setSpan(new AbsoluteSizeSpan(TEXT_SIZE_in_dip, true), 0, s.length(), 0);
menuItem.setTitle(s);

Second way:

MenuItem menuItem = navigationView.getMenu().findItem(R.id.menu_item);
SpannableString s = new SpannableString(menuItem.getTitle());
s.setSpan(new TextAppearanceSpan(this, R.style.TextAppearance), 0, s.length(), 0);
menuItem.setTitle(s);

res / values / styles.xml

<style name="TextAppearance">
<item name="android:textColor">TEXT_COLOR</item>
<item name="android:textSize">TEXT_SIZE_in_sp</item>
</style>

How to Change the Text Color of Activity Main Drawer in Android

  • You need to initialize the colors you want to use using the

    ColorStateList object.

  • After setting up the colors for each state you set the navitaionView
    itemTextColor (navigation.setItemTextColor(yourCustomColorStateList);

This is where I got the answer

Here is another related Stackoverflow questions

Official Android documentation of all the available kind of state

example:
Use this inside your onCreate method of your main class

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

/**
* start of code configuration for color of text of your Navigation Drawer / Menu based on state
*/
int[][] state = new int[][] {
new int[] {-android.R.attr.state_enabled}, // disabled
new int[] {android.R.attr.state_enabled}, // enabled
new int[] {-android.R.attr.state_checked}, // unchecked
new int[] { android.R.attr.state_pressed} // pressed

};

int[] color = new int[] {
Color.WHITE,
Color.WHITE,
Color.WHITE,
Color.WHITE
};

ColorStateList colorStateList1 = new ColorStateList(state, color);

// FOR NAVIGATION VIEW ITEM ICON COLOR
int[][] states = new int[][] {
new int[] {-android.R.attr.state_enabled}, // disabled
new int[] {android.R.attr.state_enabled}, // enabled
new int[] {-android.R.attr.state_checked}, // unchecked
new int[] { android.R.attr.state_pressed} // pressed

};

int[] colors = new int[] {
Color.WHITE,
Color.WHITE,
Color.WHITE,
Color.WHITE
};
ColorStateList colorStateList2 = new ColorStateList(states, colors);
navigationView.setItemTextColor(colorStateList1);
navigationView.setItemIconTintList(colorStateList2);
/**
* end of code configuration for color of text of your Navigation Drawer / Menu based on state
*/


Related Topics



Leave a reply



Submit