13

I have a popup menu that appears when the user clicks an image. The code used is:

PopupMenu popupMenu = new PopupMenu(mContext, mImageView);
popupMenu.setOnMenuItemClickListener(MyClass.this);
popupMenu.inflate(R.menu.menu_my_class_options);
popupMenu.show();

The menu has three actions, and I would like that the text in one of them to be colored red (instead of black).

Is that possible? I've found some answers (e.g., here) that show how to do this with the activity's Options Menu, but they require access to the onCreateOptionsMenu method...

Thanks in advance.

-- EDIT --

The menu file:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/item_edit"
        app:showAsAction="ifRoom|withText"
        android:title="@string/action_edit_address"
        android:visible="true"/>
    <item
        android:id="@+id/item_retake_photo"
        app:showAsAction="ifRoom|withText"
        android:title="@string/action_retake_photo"
        android:visible="true"/>
    <item
        android:id="@+id/item_delete"
        app:showAsAction="ifRoom|withText"
        android:title="@string/action_delete_shipment"
        android:visible="true"/>
</menu>
Community
  • 1
  • 1
Filipe Ramos
  • 1,755
  • 2
  • 18
  • 24
  • Could you provide the xml of menu_my_class_options ? – NSimon Mar 27 '15 at 15:53
  • Edited the message to provide the menu xml file. – Filipe Ramos Mar 27 '15 at 17:29
  • did you come accross this answer then ? http://stackoverflow.com/a/25731668/4706693 – NSimon Mar 27 '15 at 17:34
  • @NicolasSimon: Yes, but that would change the text color of all items, and I only want to change the color of one of them. And note that I'm not using the options menu, I'm using a pop up menu (_PopupMenu_ class), and I only have access to the _onMenuItemClick()_ method, and not to any _onCreate..._ method. – Filipe Ramos Mar 30 '15 at 10:03

3 Answers3

15

You can achieve this by following steps.

In your style.xml--- add the lines

<style name="style" parent="android:Theme.Holo.Light"> 
    <item name="textAppearanceSmallPopupMenu">@style/myPopupMenuTextAppearanceColor</item>
    <item name="android:popupMenuStyle">@style/myPopupMenuStyle</item>

</style>

And below code

<style name="myPopupMenuStyle" parent="@style/Widget.AppCompat.Light.PopupMenu">

</style>
<style name="myPopupMenuTextAppearanceColor" parent="@style/TextAppearance.AppCompat.Light.Widget.PopupMenu.Small">
    <item name="android:textColor">#000000(Your Color Code)</item>
</style>

Update for single menu item color

For single menu item color you can simply use the below code

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.your_menu, menu);

    int positionOfMenuItem = 0; 
// 0 or whatever your item position at which you want to change the color...
    MenuItem item = menu.getItem(positionOfMenuItem);
    SpannableString s = new SpannableString("My red MenuItem");
    s.setSpan(new ForegroundColorSpan(Color.RED//your color), 0, s.length(), 0);
    item.setTitle(s);
}
Shadik Khan
  • 1,217
  • 1
  • 8
  • 18
  • Thanks for the quick answer, Sadiq. Wouldn't that change the text color of all items? I only want to change the color of one of them... – Filipe Ramos Mar 27 '15 at 17:35
  • @FilipeRamos yes you are right. I also added the code as per your requirment in update. Please check – Shadik Khan Mar 27 '15 at 17:52
  • The problem with that approach is that I'm not using the options menu, I'm using a pop up menu (_PopupMenu_ class), and I only have access to the _onMenuItemClick()_ method, and not to any _onCreate..._ method! :-/ – Filipe Ramos Mar 30 '15 at 09:59
  • Then why don't you use the custom popup. It will be easy for you – Shadik Khan Mar 30 '15 at 17:01
  • @Sadiq How can we change the textSize in a custom PopupMenu? – Anshul Tyagi Aug 13 '15 at 07:01
7

I know that this answer is belated, but maybe it will be useful for somebody. Try to use this

<string name="action_delete_shipment"><font fgcolor='#FF1400'>Delete Shipment</font></string>
Pretorian
  • 109
  • 1
  • 8
1

Based on the answer of @Shadik Khan, actually you just need to create SpannableString, change the color or it before putting the item into the menu list. Like below

if (messageActionMap.keyAt(i) == LIST_ITEM_DELETE) {
                    SpannableString itemRecall = new SpannableString(messageActionMap.valueAt(i));
                    itemRecall.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.red)), 0, itemRecall.length(), 0);
                    mMessagePopupMenu.getMenu().add(Menu.NONE, messageActionMap.keyAt(i), 1, itemRecall);
                }
Michael
  • 183
  • 2
  • 10