James_Duh James_Duh - 1 month ago 8
Android Question

action item in menu set text color

I have a menu that is inflated on to a toolbar in 3 different Activities. One of the Activities has a Overflow menu with one Action behind the Overflow. I can change the background color of the Action. I can not change the text color of the text associated with the Action. Please do not down vote I have looked at and tried all 136 similar posted questions.
Here is the styles code and menu code below.

<resources>
<!-- Base application theme. -->
<!-- parent="Theme.AppCompat.NoActionBar"> -->
<!-- Line of CODE ABOVE gives WHITE color to items in ToolBar -->
<!-- Line of CODE BELOW gives BLACk color to items in ToolBar -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="android:itemBackground">@color/color_Red</item>
<item name="actionMenuTextColor">@style/MyActionBarTitleText</item>
</style>

<style name="MyActionBarTitleText" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:textColor">@color/color_Yellow</item>
</style>




Menu Code

<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=".MainActivity">

<item
android:id="@+id/action_ReSetPW"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never"
android:visible="true"/>




As a side note I also could not change the Title Text Color based on some parameter. I would like to know how to set or change the text color?
After some trial and ERROR I am posting a styles code with comments for a solution solution code below

<resources>
<!-- Base application theme. -->
<!-- parent="Theme.AppCompat.NoActionBar"> -->
<!-- Line of CODE ABOVE gives WHITE color to items in ToolBar -->
<!-- Line of CODE BELOW gives BLACk color to items in ToolBar -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="android:itemBackground">@color/color_Red</item>
<!--The line of CODE above styles the action background color -->

</style>

<style name="Toolbar.TitleText" parent="TextAppearance.Widget.AppCompat.Toolbar.Title">
<item name="android:textSize">21sp</item>
<item name="android:textStyle">italic</item>
</style>

<!--ADD THIS TO THE XML that defines the widget ToolBar android:theme="@style/ToolbarTheme" -->
<!--the line of CODE above tells the ToolBar where to find the style to use -->
<!--Code below will make the Overflow three dots Yellow and the text of the Action Yellow -->
<style name="ToolbarTheme" parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<item name="android:textColorPrimary">@color/color_Yellow</item>
</style>




It seems part of the solution is to call the style from the toolbar.xml

Answer

Edit:

This answer is working:

styles.xml:

<!-- 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="android:itemTextAppearance">@style/itemTextStyle.AppTheme</item>
</style>

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

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

<style name="itemTextStyle.AppTheme" parent="@android:style/TextAppearance.Widget.IconMenu.Item">
    <item name="android:textColor">@color/color_item_popup</item>
</style>

res/color/color_item_popup.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:color="#FFFF00"/>
    <item android:state_focused="true" android:color="#FFFF00"/>
    <item android:color="#00FFFF"/>
</selector>