Change Android background color of popup menu
I don't understand why it is so painful to change the color for an Android app. I have tried several ways to change the background color of the popup menu in my action bar without any success.
I am using AppTheme.NoActionBar to style the obviously action bar.
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="navigationIcon">@drawable/ic_return_dark</item>
<item name="overlapAnchor">false</item>
<item name="popupMenuStyle">@style/PopupMenu</item>
</style>
<style name="PopupMenu" parent="Widget.AppCompat.PopupMenu">
<item name="android:popupBackground">@color/color4</item>
</style>
Following the examples below, it was explained that you need to insert your own style (in my case AppTheme.NoActionBar ) custom popupMenuStyle to arrange for the popupBackground setting that changes the background color of the popup menu. This does not work.
What can I do to change the background color of the popup menu?
+3
source to share
1 answer
To change the color of the options menu in Android, changing themes and styles will not help. You must initialize the LayoutInflater Factory Class.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my_menu, menu);
getLayoutInflater().setFactory(new Factory() {
@Override
public View onCreateView(String name, Context context,
AttributeSet attrs) {
if (name .equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView")) {
try {
LayoutInflater f = getLayoutInflater();
final View view = f.createView(name, null, attrs);
new Handler().post(new Runnable() {
public void run() {
view.setBackgroundResource(R.drawable.your_color);
((TextView) view).setTextColor(Color.your_color);
}
});
return view;
} catch (InflateException e) {
} catch (ClassNotFoundException e) {
}
}
return null;
}
});
return super.onCreateOptionsMenu(menu);
}
+1
source to share