4

I make custom menu item which have color background I want when button press that item button menu selected become to another color like below image:

enter image description here

and its my custom style :

 <style name="Menu">
        <item name="android:colorBackground">@color/white</item>
        <item name="android:textColor">@color/colorAccent</item>
        <item name="android:background">@drawable/item_bg_menu</item>
        </style>

item_bg_menu:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item
    android:state_focused="true"
    android:state_pressed="true"
        android:drawable="@color/white" />

    <item
        android:state_pressed="true"
        android:drawable="@color/gray" />

</selector>

and it is way i use menu:

app:popupTheme="@style/Menu"
  • post your menu and xml file. Look for padding in textview – W4R10CK Jan 26 '17 at 10:12
  • I think this question is repeated. See this link http://stackoverflow.com/questions/26585045/android-background-color-when-menu-item-is-pressed – aslamhossin Jan 26 '17 at 10:17
  • Possible duplicate of [How to change the background color of Action Bar's Option Menu in Android 4.2?](http://stackoverflow.com/questions/19659637/how-to-change-the-background-color-of-action-bars-option-menu-in-android-4-2) – jakubbialkowski Jan 26 '17 at 11:04

1 Answers1

2

The best and easiest way is to make your own style

In this case, style name is Menu

<style name="Menu">
    <item name="android:colorBackground">@color/gray</item>   //color of background menu
    <item name="android:textColor">@color/white</item>       //text color
    <item name="android:listSelector">@drawable/item_bg_menu</item>     //this is for when button press color changed
    </style>

item_bg_menu

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true" android:drawable="@color/black" />  //color when button press

</selector>

and where you want use that menu in your toll bar just add this app:popupTheme="@style/Menu"

Erfan
  • 3,059
  • 3
  • 22
  • 49