Reputation: 148
Is it possible to define different style for menu item depending on its enabled/disabled status?
For example, I want the text color of menu item to be gray in disabled mode and white in enabled mode.
I didn't have success with changing color dinamycally just as many people didn't on stackoverflow.
Upvotes: 5
Views: 6715
Reputation: 101
Using:
<style name="Theme.WordsTrainer" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
I've added the line:
<item name="android:textColor">?android:attr/textColorPrimary</item>
to themes.xml and night\themes.xml, and this works fine with day and nigth mode both.
I've spend a day diggin it, and finally found the answer in obvious place: https://developer.android.com/guide/topics/ui/look-and-feel/darktheme
Upvotes: 0
Reputation: 29
You can use a drawable as the text colour, and in drawable you can use selector to select the colour according to enabled status. Using following drawable definition as colour will make your disabled menu items grey and the rest black.
In e.g. res/drawable/default_text_colour.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:color="@android:color/darker_gray"/>
<item android:color="@android:color/white"/>
</selector>
Then, using the drawable:
<item name="android:textColor">@drawable/default_text_colour</item>
Upvotes: 1
Reputation: 1176
For some one else may be needed: use ToolBar instead of ActionBar, add TextView and set style as MenuItem:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_explorer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:theme="@style/CustomActionBar.Theme"
android:background="@mipmap/bg_naviber">
<TextView
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="@string/title_activity_explorer2"
android:layout_gravity="start"
android:padding="@dimen/dimen_8"
style="@style/CustomActionBar.Tittle"
/>
<TextView
android:id="@+id/toolbar_delete"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
android:text="@string/action_delete"
android:textColor="@drawable/selector_text_view"
android:padding="@dimen/dimen_16"
style="@style/CustomActionBar.Menu"
/>
<TextView
android:id="@+id/toolbar_do_delete"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
android:text="@string/action_do_delete"
android:textColor="@drawable/selector_text_view"
android:padding="@dimen/dimen_16"
style="@style/CustomActionBar.Menu"
android:visibility="gone"
/>
</android.support.v7.widget.Toolbar>
set textColor by selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
>
<!-- order is important -->
<item android:state_enabled="false" android:color="@color/white_disabled"/>
<item android:state_pressed="true" android:color="@color/white"/>
<item android:color="@color/white"/>
</selector>
Upvotes: 0
Reputation: 44118
It really depends on the item you wish to customize.
Basically you can create a custom color that changes depending on its state:
colors/custom_color.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#FF0000" android:state_enabled="false" />
<item android:color="#CCCCCC"/>
</selector>
Then set it to your menu item like this:
menu.findItem(R.id.action_search).getActionView().
setBackgroundResource(R.colors/custom_color.xml);
Or perhaps in the xml if available:
android:textColor="@color/custom_color"
Upvotes: 5