Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Navigation Drawer Menu Item Title Color in Android

I have a problem changing the Menu Item Title Color in the Navigation Drawer I set the itemTextColor but it only changes the Color of the Items not the Title of the menu.

Here is my Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <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"
        app:itemTextColor="#ffffff"
        android:background="#283135"
        />


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

activity_main_drawer.xml

<?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_home"
            android:icon="@mipmap/hospital"
            android:title="Home"
            />
<item
            android:id="@+id/nav_reminder"
            android:icon="@mipmap/alarm"
            android:title="Reminders" />

    </group>

    <item android:title="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>

</menu>

Check this:This is the one i am talking about

like image 895
RoyalCediee Avatar asked Jul 06 '16 04:07

RoyalCediee


People also ask

How to set Navigation drawer item color in android?

Just add app:itemTextColor="@color/accent property in your Navigation view tag like below.

How to change Navigation menu item text color in android?

This example demonstrates how do I change the text color of the menu item in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

What is NavigationView in Android?

com.google.android.material.navigation.NavigationView. Represents a standard navigation menu for application. The menu contents can be populated by a menu resource file. NavigationView is typically placed inside a DrawerLayout .


3 Answers

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 :

enter image description here

like image 131
Harshad Pansuriya Avatar answered Oct 22 '22 14:10

Harshad Pansuriya


Just add app:itemTextColor="@color/accent property in your Navigation view tag like below.

<com.google.android.material.navigation.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:backgroundTint="@color/primary"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer"
    app:itemTextColor="@color/accent"/>
like image 6
Team Android Developers Avatar answered Oct 22 '22 15:10

Team Android Developers


You can also define new style in styles.xml:

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

and then applying this style in NavigationView's theme:

<android.support.design.widget.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:theme="@style/NavigationView" />
like image 22
madim Avatar answered Oct 22 '22 16:10

madim