So I have a menu item, that's defined as:
<item android:id="@+id/action_live" android:title="@string/action_live" android:orderInCategory="1" app:showAsAction="ifRoom|withText" />
It shows as text, as you can see below:
And I want to programmatically change the "LIVE" text color. I've searched for a while and I found a method:
With globally defined:
private Menu mOptionsMenu;
and:
@Override public boolean onCreateOptionsMenu(Menu menu) { mOptionsMenu = menu; getMenuInflater().inflate(R.menu.menu_main, menu); return true; }
I do:
MenuItem liveitem = mOptionsMenu.findItem(R.id.action_live); SpannableString s = new SpannableString(liveitem.getTitle().toString()); s.setSpan(new ForegroundColorSpan(Color.RED), 0, s.length(), 0); liveitem.setTitle(s);
But nothing happens!
If I do the same for an item of the overflow menu, it works:
Is there some limitation for app:showAsAction="ifRoom|withText" items? Is there any workaround?
Thanks in advance.
Android TextView – Text Color. TextView Text Color – To change the color of text in TextView, you can set the color in layout XML file using textColor attribute or change the color dynamically in Kotlin file using setTextColor() method.
Open the colors. xml file by navigating to the app -> res -> values -> colors. xml. Create a color tag inside the resources tag with a name and set a color with its hex code.
Just go to res/values/styles.edit the xml file to change the color of action bar.
Bit late to the party with this one, but I spent a while working on this and found a solution, which may be of use to anyone else trying to do the same thing. Some credit goes to Harish Sridharan for steering me in the right direction.
You can use findViewById(R.id.MY_MENU_ITEM_ID)
to locate the menu item (provided that the menu had been created and prepared), and cast it to a TextView
instance as suggested by Harish, which can then be styled as required.
public class MyAwesomeActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { getWindow().requestFeature(Window.FEATURE_ACTION_BAR); super.onCreate(savedInstanceState); // Force invalidatation of the menu to cause onPrepareOptionMenu to be called invalidateOptionsMenu(); } private void styleMenuButton() { // Find the menu item you want to style View view = findViewById(R.id.YOUR_MENU_ITEM_ID_HERE); // Cast to a TextView instance if the menu item was found if (view != null && view instanceof TextView) { ((TextView) view).setTextColor( Color.BLUE ); // Make text colour blue ((TextView) view).setTextSize(TypedValue.COMPLEX_UNIT_SP, 24); // Increase font size } } @Override public boolean onPrepareOptionsMenu(Menu menu) { boolean result = super.onPrepareOptionsMenu(menu); styleMenuButton(); return result; }
}
The trick here is to force the menu to be invalidated in the activity's onCreate
event (thereby causing the onPrepareMenuOptions
to be called sooner than it would normally). Inside this method, we can locate the menu item and style as required.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With