Boycott A.I.
Boycott A.I.

Reputation: 18921

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" >

Upvotes: 7

Views: 4455

Answers (7)

3c71
3c71

Reputation: 4531

None of the solutions above worked, ended-up doing the following, inspired by an answer here: How to change the background color of the options menu?

Note that for context menu, setting transparent has no effect, need to set a background on the parent.

This is working on Android 13, not tested yet on any other platforms.

@Override public View onCreateView(View parent, String name, Context context, AttributeSet attrs)
{
    //  Option menu!
    if (name.equals("androidx.appcompat.view.menu.ListMenuItemView") && parent.getParent() instanceof FrameLayout)
    {
        ((View) parent.getParent()).setBackgroundColor(0x00000000);
    }

    //  Context menu!
    if (name.equals("com.android.internal.view.menu.ListMenuItemView") && parent.getParent() instanceof FrameLayout)
    {
        parent.setBackgroundResource(R.drawable.special_background);
    }
    return super.onCreateView(parent, name, context, attrs);
}

Upvotes: 0

Suresh
Suresh

Reputation: 835

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

Upvotes: 0

Rajesh Jadav
Rajesh Jadav

Reputation: 12861

Try adding this:

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

I hope it helps!

Upvotes: 0

Darshan Mistry
Darshan Mistry

Reputation: 3372

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>

Upvotes: 2

koni
koni

Reputation: 1795

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>

Upvotes: 0

Ivan
Ivan

Reputation: 3062

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>

Upvotes: 0

Niki van Stein
Niki van Stein

Reputation: 10744

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

Upvotes: 0

Related Questions