gbenroscience
gbenroscience

Reputation: 1098

How to change title color of menu's enclosing item in NavigationView's xml files

I have a menu which is shown in a NavigationView (Side navigation).

The menu has this structure in which menu tags not only have their own children item tags, but they(the menu tags) are also enclosed within parent item tags.

Example:

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

    <item android:title="User Profile">
        <menu>
            <item
                android:id="@+id/profile"
                android:icon="@drawable/ic_user"
                android:title="Profile" />
            <item
                android:id="@+id/account"
                android:icon="@android:drawable/ic_secure"
                android:title="Account" />
        </menu>
    </item>

    <item android:title="Ride Now">
        <menu>
            <item
                android:id="@+id/find_ride"
                android:icon="@drawable/car_1"
                android:title="Find A Ride" />
            <item
                android:id="@+id/offer_ride"
                android:icon="@drawable/car_1"
                android:title="Offer A Ride" />
        </menu>
    </item>
    <item android:title="Booking History">
        <menu>
            <item
                android:id="@+id/offered_rides"
                android:icon="@drawable/ic_menu_share"
                android:title="Rides offered"
                />
            <item
                android:id="@+id/booked_rides"
                android:icon="@drawable/ic_menu_send"
                android:title="Rides Booked" />
            <item
                android:id="@+id/cancelled_rides"
                android:icon="@drawable/ic_menu_send"
                android:title="Rides Cancelled" />

        </menu>
    </item>

</menu>

I have been able to set the item title colors within the menu tags using:

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:background="@color/app_dark_green_scheme"
    app:itemTextColor="@color/app_white_scheme"
    app:itemIconTint="@color/app_white_scheme"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer" />

but I need to set the title colors for items that are parents of the menu tags. How do I accomplish this?

Note: @+id/nav_view is the NavigationView that owns the menu

Please, I would prefer the answer to be xml based, instead of Java based

Upvotes: 0

Views: 36

Answers (2)

user8959091
user8959091

Reputation:

Since you don't want to change the color of all items you have to do it by code:

public SpannableString getColoredSpannableString(String s, int color) {
    SpannableString str = new SpannableString(s);
    str.setSpan(new ForegroundColorSpan(color), 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    return str;
}

public void makeMenuItemColored(MenuItem mi, int color) {
    mi.setTitle(getColoredSpannableString(mi.getTitle().toString(), color));
}


and use it like this:

makeMenuItemColored(mi, ContextCompat.getColor(this, R.color.colorPrimary));

or makeMenuItemColored(mi, yourColor);
where mi is your menu item and you can choose a color resource or a color by int

Upvotes: 0

JDevoloper
JDevoloper

Reputation: 237

You need add android:textColorSecondary into your style.xml

paste the following code into the style you are using as the theme. You can change the color as you like.

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

it works for me

Upvotes: 1

Related Questions