0

I want to change a single menuItem background in android but it's not working. What I did, set a appcompat supported actionview in MenuItem and trying to set background of actionview.

xml

<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">
    <item android:id="@+id/action_my_balance"
    android:title="@string/my_balance"
    app:showAsAction="never"
    app:actionViewClass="android.widget.ImageButton"
        />
</menu>

source code

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.my_menu, menu);
        ImageButton balanceMenu = (ImageButton) MenuItemCompat.getActionView(menu.findItem(R.id.action_my_balance));
        balanceMenu.setBackgroundColor(Color.RED);
        balanceMenu.invalidate();
        return true;
    }

But it's not working at all. Also is it possible to getView on MenuItem without set custom actionview?

shantanu
  • 2,408
  • 2
  • 26
  • 56
  • I tried without invalidate first but it didn't work, so I tested with invalidate but that didn't work either. – shantanu Jan 22 '16 at 07:31
  • http://stackoverflow.com/questions/2719173/change-background-color-of-android-menu follow this link – Vivek Mishra Jan 22 '16 at 07:33
  • I already tried that, it crash in 5.0.0. Says cannot set factory because it's already set (somewhere). BTW I am trying to figure out why my code(approach) is not working. – shantanu Jan 22 '16 at 07:35
  • I'm not sure why your approach is not working but I think it's somehow a contradiction to use *app:showAsAction="never"* together with *app:actionViewClass="whatever"* Are you trying to set an action bar ImageButton or to change an item in the overflow menu? – Bö macht Blau Jan 22 '16 at 07:39
  • I don't want to show this menu in actionbar directly. Instead I want to show it in a list. That's why I used showAsAction="never" – shantanu Jan 22 '16 at 07:42
  • 1
    Then I think you have to use a custom popup window instead of a menu – Bö macht Blau Jan 22 '16 at 08:10

1 Answers1

0

try this Style from AndroidManifest.xml

<style name="ActionBarTheme" parent="ChooseYourParentStyle">
    <item name="android:popupMenuStyle">@style/MyStyle.PopupMenu_Style</item>
</style>

<style name="MyStyle.PopupMenu_Style" parent="ChooseYourParentStyle.ListPopupWindow">
    <item name="android:popupBackground">@drawable/CreateCustomDrawable</item>
</style>
iSandeep
  • 597
  • 8
  • 24