Override Context Menu Colors in Android

Override context menu colors in Android

If by context menu you mean the menu from the long press, then I have done this with the following code. My menu has my theme's background, and a green highlight.

context menu layout:

<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/resetConfirm" android:title="@string/actual_reset"></item>
</menu>

styles.xml, where I'm using a custom theme (which I think is the key)

 <style name="GradientLight" parent="@android:style/Theme.Light">
<item name="android:windowBackground">@drawable/background</item>
<item name="android:progressBarStyle">@style/progressBar</item>
<item name="android:buttonStyle">@style/greenButton</item>
<item name="android:buttonStyleSmall">@style/greenButton</item>
<item name="android:listViewStyle">@style/listView</item>
<item name="android:itemBackground">@drawable/menu_selector</item>
<item name="android:spinnerStyle">@style/spinner</item>
</style>
<style name="listView" parent="@android:style/Widget.ListView.White">
<item name="android:background">@drawable/background</item>
<item name="android:listSelector">@drawable/list_selector_background_green</item>
</style>

How to change a context menu items color

You can try to change theme style on your toolbar inside ur XML layout file:

Try that or maybe it can help to do ur similar code:

 <android.support.v7.widget.Toolbar 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_my_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

To change Tittle color you can do it programatically:

toolbar.setTitleTextColor(0xFFFFFFFF);

UPDATE1

To change Popup background color you can assign color directly on new theme only for that popup, and assign that on AndroidManifest.xml, try something like that:

 <style name="AppTheme" parent="android:Theme.Light">
<item name="android:popupMenuStyle">@style/PopupMenu</item>
</style>

<style name="PopupMenu" parent="@android:style/Widget.PopupMenu">
<item name="android:popupBackground">@android:color/white</item>
</style>

AndroidManifest.xml

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
.............
</application>

UPDATE2

You can change statusBar color again programatically when context menu it's enabled:

Try that:

getWindow().setStatusBarColor(0xFFFFFFFF);

If it reports an error on getWindow() method, try that:

Window window = activity.getWindow();

window.setStatusBarColor(0xFFFFFFFF);

NOTE

You only can change StatusBarColor on Android Lollipop, remember that,
on Devices with pre-Lollipop version the statusBarColor it's allways
Black or soemtimesTransparent

Android - material design - how to set the background color for a context menu

Although I am thankful for the previous answer, I found the perfect and very simple solution.

In my project I used this parent:

<style name="Base.Theme.Deholtmans" parent="Theme.AppCompat.NoActionBar">

I got very dark context menu's, etc. Before AppCompat I used the Light version of themes.

The solution is using the right pre-defined parent:

<style name="Base.Theme.Deholtmans" parent="Theme.AppCompat.Light.NoActionBar">

So, the light version. Easy does it!

How to change the text color of context menu item

As you have created a custom list , you are calling method for adding item in list like addItem() or setText(). So whatever you do just apply below code while adding the item.

Spannable wordtoSpan = new SpannableString("Your Item Name");        

wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 0, wordtoSpan.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //Your color code instead Color.BLUE

How to change background color popup menu android

Add popupMenu style to ur AppTheme:

<style name="AppTheme" parent="android:Theme.Light">
<item name="android:popupMenuStyle">@style/PopupMenu</item>
</style>

<style name="PopupMenu" parent="@android:style/Widget.PopupMenu">
<item name="android:popupBackground">@android:color/white</item>
</style>

manifest.xml:

 <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
.............
</application>

Change Popup menu background color

I wasn't satisfied with the accepted answer since it doesn't really explain why the OPs custom popup style isn't being applied--not just the background, but also things like the text color--so I did my own experimentation.

It's important to note there is a difference between the popup created by the Toolbar (when it has menu items) and showing one yourself with PopupMenu. These are governed by different theme attributes. Also, be aware there are two PopupMenu classes: android.widget.PopupMenu, and android.support.v7.widget.PopupMenu.

The theme attribute you need to style PopupMenus you show explicitly is android:popupMenuStyle or popupMenuStyle. You have a few options to achieve proper application of your custom style:

(1) Use android:popupMenuStyle in the theme of the activity (or app)

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- if using android.widget.PopupMenu -->
<item name="android:popupMenuStyle">@style/PopupMenu</item>
<!-- if using android.support.v7.widget.PopupMenu -->
<item name="popupMenuStyle">@style/PopupMenu</item>
</style/>

<style name="PopupMenu" parent="ThemeOverlay.AppCompat.Dark">
<item name="android:popupBackground">@color/popupBackground</item>
</style>

PopupMenu popup = new PopupMenu(this, anchorView);

Note this requires nothing extra in your layout file.

(2) Use a ContextThemeWrapper

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- nothing special -->
</style/>

<style name="CustomPopupTheme" parent="ThemeOverlay.AppCompat.Dark">
<!-- if using android.widget.PopupMenu -->
<item name="android:popupMenuStyle">@style/PopupMenu</item>
<!-- if using android.support.v7.widget.PopupMenu -->
<item name="popupMenuStyle">@style/PopupMenu</item>
</style>

<style name="PopupMenu" parent="ThemeOverlay.AppCompat.Dark">
<item name="android:popupBackground">@color/popupBackground</item>
</style>

ContextThemeWrapper ctw = new ContextThemeWrapper(this, R.style.CustomPopupTheme);
PopupMenu popup = new PopupMenu(ctw, anchorView);

Note how this doesn't use R.style.PopupMenu directly when constructing the ContextThemeWrapper. This seems a bit roundabout, but it's useful if you want to keep the popup theme separated from activity or app themes (perhaps only some popups need your special theme, for example).

(3) Use your AppBarLayout's Context

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- nothing special -->
</style/>

<style name="AppBarOverlay" parent="ThemeOverlay.AppCompat.Light">
<!-- if using android.widget.PopupMenu -->
<item name="android:popupMenuStyle">@style/PopupMenu</item>
<!-- if using android.support.v7.widget.PopupMenu -->
<item name="popupMenuStyle">@style/PopupMenu</item>
</style>

<style name="PopupMenu" parent="ThemeOverlay.AppCompat.Dark">
<item name="android:popupBackground">@color/popupBackground</item>
</style>

<style name="PopupOverlay" parent="ThemeOverlay.AppCompat.Dark">
<!-- changes the background of the Toolbar's popup -->
<item name="android:colorBackground">@color/popupBackground</item>
</style>

<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppBarOverlay">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/PopupOverlay"/>

</android.support.design.widget.AppBarLayout>

AppBarLayout appBar = (AppBarLayout) findViewById(R.id.app_bar);
PopupMenu popup = new PopupMenu(appBar.getContext(), anchorView);

Since you already have a theme overlay for the AppBar, you can use it to hold your popup theme references. This would also work with the Toolbar's context, at least given the current layout, although note that app:popupTheme is not actually relevant here since it affects the Toolbar's popup and not your PopupMenu. Also note how similar this is to option 2 above, which should clue you in to how the android:theme attribute works under the hood ;)

In my experiments, android:itemBackground only worked when I used it in place of android:colorBackground in the PopupOverlay style. However, it's better to use android:colorBackground because that will change the popup's window color, leaving the rounded corners and the selectable item highlight/ripple of the items intact.

How to change the background of context menu for EditText?

try to use standard Activity, with Theme.AppCompat or

in your theme add this:

android:panelBackground=@android:color/transparent


Related Topics



Leave a reply



Submit