Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change background popupMenu in Android

I tried to change background of popupmenu, but my implementation does not work.

This is my code:

<style name="MyHoloLight" parent="android:Theme.Holo.Light">
    <item name="android:popupMenuStyle">@style/popupMenuStyle</item>
</style>
<style name="popupMenuStyle" parent="@android:style/Widget.PopupMenu">
    <item name="android:popupBackground">@color/bgPopumMenu</item>
</style>

Apply in AndroidManifest.xml

<application
        android:hardwareAccelerated="true"
        android:label="@string/app_name"
        android:icon="@drawable/ic_launcher"
        android:theme="@style/MyHoloLight">
like image 346
user1854307 Avatar asked Mar 11 '14 08:03

user1854307


2 Answers

If you are using custom theme :

  1. In Manifest : @style/MyMaterialTheme is my customized theme :

      <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:supportsRtl="true"
            android:theme="@style/MyMaterialTheme">
    
  2. Use this code : R.style.popupMenuStyle for popup menu in java class :

Context wrapper = new ContextThemeWrapper(getContext(), R.style.popupMenuStyle);
PopupMenu popup = new PopupMenu(wrapper, v);

//Inflating the Popup using xml file
popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());

MenuPopupHelper menuHelper = new MenuPopupHelper(wrapper, (MenuBuilder) popup.getMenu(), v);
menuHelper.setForceShowIcon(true);
menuHelper.setGravity(Gravity.RIGHT);
  1. this is custom style of popup- menu for your own background drawable

<style name="popupMenuStyle" parent="@android:style/Widget.PopupMenu">
    <item name="android:popupBackground">@drawable/dropdown_bg</item>
</style>
  1. then change in your own theme "popupMenuStyle"

    <style name="MyMaterialTheme" parent="MyMaterialTheme.Base">
    
        </style>
    
        <style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
            <item name="windowNoTitle">true</item>
            <item name="windowActionBar">false</item>
            <item name="colorPrimary">@color/colorPrimary</item>
            <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
            <item name="colorAccent">@color/colorPrimary</item>
    
            <item name="popupMenuStyle">@style/popupMenuStyle</item>
            <item name="android:popupMenuStyle">@style/popupMenuStyle</item>
        </style>
    
like image 172
Heena Arora Avatar answered Oct 13 '22 05:10

Heena Arora


I made some changes of @Raju`s code and the following styles working for me,

Context wrapper = new ContextThemeWrapper(context,R.style.popupMenuStyle);
PopupMenu popup = new PopupMenu(wrapper, YourView);

and this is my style,

<style name="popupMenuStyle" parent="android:Theme.Holo.Light.DarkActionBar">
    <item name="android:popupMenuStyle">@style/MyApp.PopupMenu</item>
    <item name="android:textColor">@color/White</item>
</style>

<style name="MyApp.PopupMenu" parent="android:Widget.Holo.Light.ListPopupWindow">
<item name="android:popupBackground">@color/color_semi_transparent</item>       
</style>
like image 34
varotariya vajsi Avatar answered Oct 13 '22 05:10

varotariya vajsi