NavigationView menu item title color - android

Lets say I have a menu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="#string/menu_title">
<menu>
<item
android:id="#+id/nav_menu_item_1"
android:icon="#drawable/nav_menu_item_1"
android:title="#string/nav_menu_item_1" />
</menu>
</item>
</menu>
I can easily change color of "nav_menu_item_1" title like this:
<android.support.design.widget.NavigationView
...
app:itemTextColor="#color/colorCustom"
... />
However this doesn't change color of
<item android:title="#string/menu_title">
How can I change color of first level menu item?

You need add android:textColorSecondary into your style.xml
<item name="android:textColorSecondary">#color/colorPrimary</item>
it works for me
Ouptput

Set in Your theme... android:actionMenuTextColor
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:actionMenuTextColor">#color/colorAccent</item>
</style>
Or By Code
MenuItem settingsMenuItem = menu.findItem(R.id.action_settings);
SpannableString s = new SpannableString(settingsMenuItem.getTitle());
s.setSpan(new ForegroundColorSpan(Color.BLACK), 0, s.length(), 0);
settingsMenuItem.setTitle(s);

You can change it set theme to your NavigationView
Create in style.xml
<style name="NavigationView">
<item name="android:background">?attr/clPrimary</item>
<item name="android:textColorSecondary">#color/textSecond</item>
</style>
Set theme in layout
<com.google.android.material.navigation.NavigationView
android:id="#+id/sheetAdd_nv_menu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/NavigationView"
app:itemTextColor="?attr/clText"
app:menu="#menu/menu_sheet_add" />

Related

How to change the navigation icon color

I have a navigationView in my app and after add two lines in app style :
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
The Navigation icon color changed to black and for this problem i added this line in style :
<item name="android:textColorSecondary">#android:color/white</item>
Now navigation icon is white again but now i have a new problem . With above code , the title of navigation items is change color too.
I didn't find a solution to this Interference
Style :
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:textColorSecondary">#android:color/white</item>
</style>
Navigation Menu :
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<group
android:id="#+id/testGRP"
android:checkableBehavior="single"
>
<item android:title="جلسه : انتخاب نشده"
android:id="#+id/menuItem_StudentList11"
android:icon="#drawable/ic_arrow_drop_up"
/>
<item android:title="مرتب سازی"
android:id="#+id/menuItem_StudentList_Quize1"
android:icon="#drawable/all_exam_icon"
/>
<item android:title="#string/Navigation_StudentList3"
android:id="#+id/menuItem_StudentList_Quize"
android:icon="#drawable/all_exam_icon"
/>
</group>
<item android:title="رویدادها:"> //this line is title
<menu>
<item android:title="#string/Navigation_StudentList4"
android:id="#+id/menuItem_StudentList_MidTerm"
android:icon="#drawable/all_exam_icon"
/>
<item android:title="#string/Navigation_StudentList5"
android:id="#+id/menuItem_StudentList_EndTerm"
android:icon="#drawable/all_exam_icon"
/>
<item android:title="امتحان عملی"
android:id="#+id/menuItem_StudentList_Practical"
android:icon="#drawable/all_exam_icon"
/>
<item android:title="#string/Navigation_StudentList6"
android:id="#+id/menuItem_StudentList_Project"
android:icon="#drawable/all_projcet_icon"
/>
<item android:title="#string/Navigation_StudentList2"
android:id="#+id/menuItem_StudentList_Conference"
android:icon="#drawable/all_conference_icon"
/>
</menu>
</item>
<item android:title="#string/Navigation_StudentList1"
android:id="#+id/menuItem_StudentList1"
android:icon="#drawable/all_notifiicon_icon"
/>
<item android:title="#string/Navigation_StudentList7"
android:id="#+id/menuItem_StudentList7"
android:icon="#drawable/main_newclass_icon"
/>
</menu>
enter image description here
enter image description here
Using a Toolbar, to change the navigation icon color you can use:
With a MaterialToolbar or (androidx and a Material theme):
<com.google.android.material.appbar.MaterialToolbar
android:theme="#style/MyThemeOverlay_Toolbar"
...>
<style name="MyThemeOverlay_Toolbar" parent="ThemeOverlay.MaterialComponents.Toolbar.Primary">
<!-- color used by navigation icon and overflow icon -->
<item name="colorOnPrimary">#color/secondaryColor</item>
</style>
With the support Library:
<android.support.v7.widget.Toolbar
app:theme="#style/ThemeToolbar" />
<style name="ThemeToolbar" parent="Theme.AppCompat.Light">
<!-- navigation icon color -->
<item name="colorControlNormal">#color/my_color</item>
</style>
If you are looking to change text colors/appearance in the NavigationView Items, you should use the attribs
app:itemTextColor
app:itemIconTint
app:itemTextAppearance
Otherwise, Specify which view's color you want to change. Setting android:textColorSeconday is a bad idea according to me.

Android Menu Text Color Change

I am trying Material theme and changing the background and text color of Menu and items. Using the below style it works fine on the Toolbar, but the menu pop up while pressing device menu button shows no change in text color though the background got changed. Please help to change the text color as mentioned.
<resources>
<style name="MyMaterialTheme" parent="MyMaterialTheme.Base">
</style>
<style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:textColorPrimary">#color/accent</item>
<item name="android:textColorSecondary">#color/accent</item>
<item name="android:textColorHint">#color/accent</item>
<!-- Menu Items -->
<item name="android:textColor">#color/accent</item>
<item name="android:actionMenuTextColor">#color/accent</item>
<item name="actionMenuTextColor">#color/accent</item>
<item name="actionBarStyle">#style/MyActionBar</item>
<item name="android:itemBackground">#color/colorPrimaryDark</item>
<item name="android:itemTextAppearance">#style/MyActionBar.MenuTextStyle</item>
<!-- Menu Items -->
</style>
<style name="MyActionBar" parent="#style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="background">#color/colorPrimaryDark</item>
<item name="titleTextStyle">#style/MyActionBarTitle</item>
<item name="actionMenuTextColor">#color/accent</item>
</style>
<style name="MyActionBarTitle" parent="#style/TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:textColor">#color/accent</item>
<item name="android:actionMenuTextColor">#color/accent</item>
<item name="actionMenuTextColor">#color/accent</item>
</style>
<style name="MyActionBar.MenuTextStyle">
<item name="android:textColor">#color/accent</item>
<item name="android:textStyle">bold</item>
<item name="android:actionMenuTextColor">#color/accent</item>
<item name="actionMenuTextColor">#color/accent</item>
</style>
One simple line in your theme :)
<item name="android:actionMenuTextColor">#color/your_color</item>
try this create this theme
<style name="TextAppearance" parent="#style/Theme.Sherlock.Light">
<item name="actionMenuTextColor">#color/white</item>
<item name="android:actionMenuTextColor">#color/white</item>
</style>
than apply this theme in your Activity theme like this
<style name="your_theme" parent="your_parent">
<item name="android:itemTextAppearance">#style/TextAppearance</item>
</style>
or try this
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.your_menu, menu);
int positionOfMenuItem = 0;
MenuItem item = menu.getItem(positionOfMenuItem);
SpannableString s = new SpannableString("My red MenuItem");
s.setSpan(new ForegroundColorSpan(Color.RED), 0, s.length(), 0);
item.setTitle(s);
}
You can change the color of the overflow menu background by adding a new style in style.xml.
<style name="OverflowMenu"
parent="#android:style/Theme.Holo">
<item name="android:popupMenuStyle">#style/MyOverflowMenu</item>
<item name="android:itemTextAppearance">#style/TextAppearance</item>
</style>
<style name="MyOverflowMenu"
parent="#android:style/Widget.Holo.ListPopupWindow">
<item name="android:popupBackground">#color/your_color</item>
</style>
<style name="TextAppearance">
<item name="android:textColor">#color/your_color</item>
</style>
// note if you want to style popup menu in toolbar
step 1-- Add your theme in toolbar
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="#style/MyPopupTheme" />
step 2-- add style in styles.xml
<style name="MyPopupTheme" parent="ThemeOverlay.AppCompat.Dark">
<item name="android:colorControlActivated">#color/red </item>
<item name="android:colorControlHighlight">#color/red</item>
<item name="android:colorControlNormal">#color/yellow</item>
<item name="android:textColorPrimary">#color/yellow</item>
</style>
// note if you want to style menu text color in toolbar itself
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:theme="#style/Mytoolbartheme"/ />
<style name="Mytoolbartheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="actionMenuTextColor">#color/text_color</item>
</style>
// if you use android x then toolbar is
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:id="#+id/toolbar"
app:titleTextColor="#color/white"
app:popupTheme="#style/MyPopupTheme"
android:background="#color/colorsplash"
android:layout_height="wrap_content">
</androidx.appcompat.widget.Toolbar>
Add one simple line in your theme .I hope this will solve your problem.
<item name="android:textColor">#color/button_color</item>
enjoy..

Navigation Drawer, support design. Text color of highlighted item

I'm building a navigation drawer using the android's support design lib. I would like to customize the colors of the text showed in that navigation drawer, so I've used a style to do that.
My Navigation Drawer.
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:theme="#style/NavigationViewTheme"
app:headerLayout="#layout/drawer_header"
app:menu="#menu/menu_navigation" />
My style
<style name="NavigationViewTheme" parent="AppTheme">
<item name="textAppearanceListItem">#style/TextAppearance.AppCompat.Body2</item>
<!--CHANGE TEXT COLOR -->
<item name="android:textColorPrimary">#android:color/white</item>
<!-- CHANGE ICON COLOR -->
<item name="android:textColorSecondary">#android:color/white</item>
</style>
What I want to change now, is the color of the highlighted text and icon. I've tried with so many items but nothing changes.
I've tried with all of these
<item name="textColorPrimary">#color/primary_text_dark</item>
<item name="textColorSecondary">#color/secondary_text_dark</item>
<item name="textColorTertiary">#color/tertiary_text_dark</item>
<item name="textColorPrimaryInverse">#color/primary_text_light</item>
<item name="textColorSecondaryInverse">#color/secondary_text_light</item>
<item name="textColorTertiaryInverse">#color/tertiary_text_light</item>
<item name="textColorPrimaryActivated">#color/primary_text_dark</item>
<item name="textColorSecondaryActivated">#color/secondary_text_dark</item>
<item name="textColorPrimaryDisableOnly">#color/primary_text_dark_disable_only</item>
<item name="textColorPrimaryInverseDisableOnly">#color/primary_text_light_disable_only</item>
<item name="textColorPrimaryNoDisable">#color/primary_text_dark_nodisable</item>
<item name="textColorSecondaryNoDisable">#color/secondary_text_dark_nodisable</item>
<item name="textColorPrimaryInverseNoDisable">#color/primary_text_light_nodisable</item>
<item name="textColorSecondaryInverseNoDisable">#color/secondary_text_light_nodisable</item>
<item name="textColorHint">#color/hint_foreground_dark</item>
<item name="textColorHintInverse">#color/hint_foreground_light</item>
<item name="textColorSearchUrl">#color/search_url_text</item>
<item name="textColorHighlight">#color/highlighted_text_dark</item>
<item name="textColorHighlightInverse">#color/highlighted_text_light</item>
<item name="textColorLink">#color/link_text_dark</item>
<item name="textColorLinkInverse">#color/link_text_light</item>
<item name="textColorAlertDialogListItem">#color/primary_text_light_disable_only</item>
Alternatively, set app:itemTextColor="#color/navigation_view_color" to your NavigationView.
navigation_view_color:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
android:color="#color/highlighted_color" />
<item android:color="#color/black" />
</selector>
I've found the solution. I only need to add
<item name="colorPrimary">#android:color/holo_red_dark</item>
to my navigation drawer style.

Change text color of option menu when we use app:showAsAction="always"

I am using toolbar and use option for corresponding actions. My problem is this I want to show "SAVE" text on toolbar with white color, I apply many styles but it always appear as black. But when option appear as popup then text color is always white . I google this a lot , but have no solution.
Here is my code :
toolbar
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/tools"
android:id="#+id/my_awesome_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:theme="#android:style/ThemeOverlay.Material.Light"
app:popupTheme="#style/Theme.AppCompat.NoActionBar"
android:background="#drawable/tb_header"
android:minHeight="?attr/actionBarSize"/>
styles
<style name="MStyle" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowBackground">#android:color/white</item>
<item name="android:colorBackground">#android:color/black</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="android:textAllCaps">false</item>
<item name="android:popupMenuStyle">#style/MyPopupMenu</item>
<item name="android:panelBackground">#android:color/holo_green_light</item>
<item name="android:actionBarWidgetTheme">#style/AppTheme</item>
</style>
<style name ="MyPopupMenu" parent="android:Theme.Holo.Light">
<item name="android:popupBackground">#android:color/holo_green_light</item>
</style>
****Option menu****
<menu 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"
tools:context="com.doubtfire.userprofiles.ParentProfileActivity">
<item
android:id="#+id/action_skip"
android:title="#string/menu_str_skip"
android:orderInCategory="100"
app:showAsAction="always" />
</menu>
****I even apply code at onPrepareOptionMenu****
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
for (int i=0; i<menu.size(); i++) {
MenuItem mi = menu.getItem(i);
String title = mi.getTitle().toString();
Spannable newTitle = new SpannableString(title);
newTitle.setSpan(new ForegroundColorSpan(Color.WHITE), 0, newTitle.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
mi.setTitle(newTitle);
}
return true;
}
Use
<item name="android:actionMenuTextColor">#FFFFFF</item>
in MStyle, as is shown below:
<style name="MStyle" parent="Theme.AppCompat.Light.NoActionBar">
...
<item name="android:actionMenuTextColor">#FFFFFF</item>
<!-- for appcompat-->
<item name="actionMenuTextColor">#FFFFFF</item>
</style>
This change colour of menu item when SAVE item will be visible on ActionBar
To change the color of the text in Toolbar menu use below code.
<style name="MStyle" parent="Theme.AppCompat.Light.NoActionBar">
....
<item name="actionMenuTextColor">#FFFFFF</item>
</style>

Change Toolbar Menu Item color (non-hidden action)

Say I have a menu (options_menu.xml) similar to the following:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/action_login"
android:title="Login"
app:showAsAction="always"/>
</menu>
which I inflate into the new Toolbar item
mToolbar.inflateMenu(R.menu.options_home);
This results in something like
Is there a way to change this text color without using an image, changing the rest of the Toolbar text color, or by adding a custom view to the toolbar? Looking for an answer for minSdk 15 (appcompat).
Update:
My relevant style:
<style name="AppTheme" parent="AppTheme.Base">
<item name="actionMenuTextColor">#color/ww_red</item>
</style>
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/red</item>
<item name="colorAccent">#color/theme_accent</item>
<item name="android:textColor">#color/text_dark</item>
<item name="android:colorEdgeEffect">#color/gray</item>
</style>
In your theme file you have to put this :
<style name="AppTheme.ActionBar" parent="Theme.AppCompat.Light.DarkActionBar">
...
<item name="actionMenuTextColor">#color/text_color</item>
...
</style>
and apply this theme to your Toolbar view like this :
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/main_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:layout_gravity="top"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
android:theme="#style/AppTheme.ActionBar"/>
android:theme="#style/AppTheme.ActionBar" don't forget this line in your toolbar
in your style file, place the following:
<style name="MyAppTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">#style/MyActionBar</item>
<item name="android:titleTextStyle">#style/MyActionBar.TitleTextStyle
<item name="android:actionMenuTextAppearance">#style/MyActionBar.MenuTextStyle</item>
<item name="android:actionMenuTextColor">#color/action_bar_red</item>
</style>
<style name="MyActionBar.TitleTextStyle"
parent="android:style/TextAppearance.Holo.Widget.ActionBar.Title">
<item name="android:textColor">#F0F</item>
<item name="android:textStyle">bold</item>
<item name="android:textSize">24dip</item>
</style>
<style name="MyActionBar.MenuTextStyle"
parent="android:style/TextAppearance.Holo.Widget.ActionBar.Menu">
<item name="android:textColor">#F0F</item>
<item name="android:textStyle">bold</item>
<item name="android:textSize">24dip</item>
</style>
You can make changes in the above style items to suit your requirements. I have not added separate styling for color though. As you might observe, I am just assigning red color (which I have declared in my colors file) to it. You may change as required.
For me, this worked: Setting the "android:textColor"
<style name="AppTheme.ActionBar" parent="Theme.AppCompat.Light.DarkActionBar">
...
<!--This would set the menu item color-->
<item name="android:textColor">#000</item>
...
</style>
Not sure if you can set specific backgrounds to separate MenuItems just through styling but you could just setActionView for your MenuItem.
First create a layout like item_av.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
android:padding="5dp">
<TextView
android:text="LOGIN"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textSize="12sp"
android:gravity="center"/>
</LinearLayout>
Then set it to your MenuItem in onCreateOptionsMenu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem item = menu.findItem(R.id.action_login);
item.setActionView(R.layout.item_av);
return true;
}
to change menu item text color use below code
<style name="AppToolbar" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:itemTextAppearance">#style/menu_item_color</item>
</style>
where
<style name="menu_item_color">
<item name="android:textColor">#color/app_font_color</item>
</style>
You can do it programmatically like that:
private AppCompatButton menuTextView;
void initToolbar(){
toolbar.inflateMenu(R.menu.your_menu);
menuTextView = AppCompatButton(context);
menuTextView.setTextColor(ContextCompat.getColor(
getContext(), R.color.your_color));
TypedValue outValue = new TypedValue();
getContext().getTheme().resolveAttribute(
R.attr.selectableItemBackgroundBorderless,
outValue,
true
)
toolbar.getMenu().getItem(0).setActionView(menuTextView);

Categories

Resources