5

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" >
ban-geoengineering
  • 18,324
  • 27
  • 171
  • 253
  • Can you post your AppManifest.xml please? – Hussein El Feky Aug 20 '15 at 22:21
  • OK, have now added the relevant part of it. – ban-geoengineering Aug 20 '15 at 22:39
  • Check this stackoverflow [Post](http://stackoverflow.com/questions/30526667/appcompat-toolbar-popuptheme-not-used-when-multi-selection-active/30633124#30633124) . – Elltz Aug 28 '15 at 05:09
  • I had to resort to changing the foreground colour instead, but I've had a lot of new answers since then, so I'll check them out now... – ban-geoengineering Aug 29 '15 at 13:28
  • @Heyyou I've tried all the suggestions, but none of them worked. I think a few of the answerers thought it is the overflow menu I am trying to change, when I'm actually trying to change the contextual menu that shows after long-pressing on a view (in this case, a custom text view). – ban-geoengineering Aug 31 '15 at 00:33

6 Answers6

2

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>
Darshan Mistry
  • 3,294
  • 1
  • 19
  • 29
0

You need to overwrite the actionModeBackground like this:

<item name="android:actionModeBackground">@drawable/context_menu</item>

possible duplicate question: Override context menu colors in Android

Community
  • 1
  • 1
Niki van Stein
  • 10,564
  • 3
  • 29
  • 62
0

Try adding this:

<item name="android:popupBackground">@android:color/white</item>

I hope it helps!

Rajesh Jadav
  • 12,801
  • 5
  • 53
  • 78
0

In your app bar XML:

app:popupTheme="@style/MyCustomOverflowTheme"

Then in your styles.xml:

<style name="MyCustomOverflowTheme" parent="Theme.AppCompat.Light">
    <item name="android:textColorPrimary">@color/primaryColor</item>
    <item name="android:textColorSecondary">@color/accentColor</item>
    <item name="android:background">@color/accentColor</item>
</style>

EDIT

app:popupTheme="@style/MyCustomOverflowTheme"

Should go as your Toolbar or ActionBar property, e.g:

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/primaryColor"
    app:theme="@style/MyCustomToolBarTheme"
    app:popupTheme="@style/MyCustomOverflowTheme"
    >
</android.support.v7.widget.Toolbar>
Ivan V
  • 3,024
  • 4
  • 26
  • 36
  • Thanks for this. Can you clarify where the `app:popupTheme="@style/MyCustomOverflowTheme"` line should go? – ban-geoengineering Aug 29 '15 at 13:46
  • Just noticed your edit. The context menu is not being shown from a Toolbar or ActionBar, but by long-pressing on a custom TextView. I have tried adding `xmlns:app="http://schemas.android.com/apk/res-auto"` and `app:popupTheme="@style/MyCustomOverflowTheme"` to the custom TextView in the xml, but I just get `error: No resource identifier found for attribute 'popupTheme' in package 'com.example.myapp'`. – ban-geoengineering Aug 31 '15 at 00:23
  • You got an error because TextView can't use this attribute. – Ivan V Aug 31 '15 at 08:35
  • Thank you, yes. Result is that it is not a solution for me. – ban-geoengineering Aug 31 '15 at 09:44
0

Did you try to put "android:actionModeBackground" in your ActionBar theme ?

<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- main theme -->
    .....
    <item name="actionBarStyle">@style/AppTheme.ActionBar</item>

    <!-- if you're using Toolbar you have to put this -->
    <item name="windowActionModeOverlay">true</item>

</style>

<style name="AppTheme.ActionBar" parent="Theme.AppCompat.Light">
         .....
    <item name="android:actionModeBackground">@color/theme_ambiance_dark</item>
        .....
</style>
koni
  • 1,805
  • 1
  • 14
  • 13
0

I got it working by using "contextPopupMenuStyle" instead of "popupMenuStyle".

Suresh
  • 627
  • 6
  • 16