Search code examples
androidandroid-actionbarmenuitem

How to change Background color of menuItem in android?


I am working on programmatically add a menu-Item into menu. I want to add background color on selection to particular Item. How can I add background to menuItem?

enter image description here

Your answer would be appreciated.


Solution

  • Firstly You need to create an Style for popmenu as you want refer below sample for that

    <style name="MyApp.PopupMenu" parent="android:Widget.Holo.Light.ListPopupWindow">
        <item name="android:textStyle">@style/commonEditTextTheme</item>
        <item name="android:popupBackground">@drawable/pop_up_menu_bg_with_shadow</item>
    </style>
    

    Place of drawable you can also replace with color as you needed or make XML Drawable into your drawable folder

    <style name="commonEditTextTheme" parent="@android:style/TextAppearance.Medium">
        <item name="android:fontFamily">sans-serif-light</item>
    </style>
    

    this pop menu theme add in your main application or activity theme like below

    <!--My Theme-->
    <style name="MyTheme" parent="@android:style/Theme.Holo.Light">
        ......
        <item name="android:popupMenuStyle">@style/MyApp.PopupMenu</item>
    </style>