Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android:popupBackground not working for context menus

Why does my styles.xml code successfully change the background colour of my actionbar overflow menu, but fail to change the background colour of the context menu in my app?

<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
    <!--<item name="android:actionBarStyle">@style/DarkActionBar</item> -->

</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">

    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    <item name="android:popupMenuStyle">@style/MyPopupMenu</item>
    <item name="android:itemTextAppearance">@style/MyCustomMenuTextAppearance</item>

</style>

<!-- Popup Menu Background Color styles -->
<!-- <style name="MyPopupMenu"  parent="@android:style/Widget.Holo.ListPopupWindow"> -->
<!-- <style name="MyPopupMenu"  parent="@android:style/Widget.PopupMenu"> -->
<style name="MyPopupMenu"  parent="@style/Widget.AppCompat.PopupMenu">
    <item name="android:popupBackground">@color/dark_gray</item> 
</style>
<!-- Popup Menu Text Color styles -->
<style name="MyCustomMenuTextAppearance">
    <item name="android:textColor">@color/white</item>
</style>

I've been stuck on this for a couple of hours and none of the solutions on SO for similar questions have worked for me.

If it helps, here is my Java code where the context menu is created:

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);

    AdapterView.AdapterContextMenuInfo info =
            (AdapterView.AdapterContextMenuInfo) menuInfo;
    String selectedWord = ((TextView) info.targetView).getText().toString();
    menu.setHeaderTitle(selectedWord);

    MenuInflater inflater = getActivity().getMenuInflater();
    inflater.inflate(R.menu.shopping_list_name_context, menu);
}

And, for completeness, here is my context menu xml, shopping_list_name_context.xml:

<item android:id="@+id/rename_shopping_list"
      android:icon="@drawable/ic_action_edit"
      android:title="@string/rename_shopping_list" />

<item android:id="@+id/empty_shopping_list"
      android:icon="@drawable/ic_action_discard"
      android:title="@string/empty_shopping_list" />

<item android:id="@+id/delete_shopping_list"
      android:icon="@drawable/ic_action_discard"
      android:title="@string/delete_shopping_list" />

And, as requested, here is an excerpt of my AndroidManifest.xml:

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="21" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />    
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

<supports-screens       
    android:smallScreens="true" 
    android:normalScreens="true" 
    android:largeScreens="true" 
    android:xlargeScreens="true" />

<permission
    android:name="com.example.myapp.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />

<uses-permission android:name="com.example.myapp.permission.C2D_MESSAGE" />
<uses-permission android:name="com.android.vending.BILLING" />

<application
    android:name=".MyApplication"
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name_short"
    android:theme="@style/AppTheme"
    android:largeHeap="true" >
like image 507
ban-geoengineering Avatar asked Oct 31 '22 21:10

ban-geoengineering


1 Answers

Below is code for set background in context menu and it works like charm.

In styles.xml file put below code:

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">

  <item name="android:itemBackground">@android:color/holo_green_dark</item>

</style>
like image 197
Darshan Mistry Avatar answered Nov 11 '22 12:11

Darshan Mistry