I'd like to change the background color of the option (overflow) menu in Android 4.2. I have tried all the methods but it is still showing the default color set by the theme. I used the following code & XML configs.
MainActivity.java
public class MainActivity extends Activity {
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getActionBar().setIcon(R.drawable.ic_launcher);
getActionBar().setTitle("Sample Menu");
getActionBar().setBackgroundDrawable(new
ColorDrawable(Color.parseColor("#33B5E5")));
int titleId = Resources.getSystem().getIdentifier("action_bar_title", "id", "android");
TextView titleText = (TextView)findViewById(titleId);
titleText.setTextColor(Color.parseColor("#ffffff"));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
setMenuBackground();
return true;
}
protected void setMenuBackground(){
// Log.d(TAG, "Enterting setMenuBackGround");
getLayoutInflater().setFactory( new Factory() {
@Override
public View onCreateView(String name, Context context,
AttributeSet attrs) {
if ( name.equalsIgnoreCase( "com.android.internal.view.menu.IconMenuItemView" ) ) {
try { // Ask our inflater to create the view
LayoutInflater f = getLayoutInflater();
final View view = f.createView( name, null, attrs );
/* The background gets refreshed each time a new item is added the options menu.
* So each time Android applies the default background we need to set our own
* background. This is done using a thread giving the background change as runnable
* object */
new Handler().post( new Runnable() {
public void run () {
// sets the background color
view.setBackgroundResource( R.color.menubg);
// sets the text color
((TextView) view).setTextColor(Color.WHITE);
// sets the text size
((TextView) view).setTextSize(18);
}
} );
return view;
}
catch ( InflateException e ) {}
catch ( ClassNotFoundException e ) {}
}
return null;
}});
}
}
Menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_settings"
android:icon="@drawable/menu"
android:showAsAction="always"
android:title="@string/action_settings">
<menu>
<item
android:id="@+id/item1"
android:showAsAction="always"
android:title="@string/item1" />
<item
android:id="@+id/item2"
android:showAsAction="always"
android:title="@string/item2" />
<item
android:id="@+id/item3"
android:showAsAction="always"
android:title="@string/item3" />
<item
android:id="@+id/item4"
android:showAsAction="always"
android:title="@string/item4" />
</menu>
</item>
</menu>
color.xml
<color name="menubg">#33B5E5</color>
The above setMenuBackground is not taking any effect:
In the above picture, I want to change the menu background from black to the blue color in the Action Bar. How can I achieve this, and what I did do wrong?
The Action Bar Style Generator, suggested by Sunny, is very useful, but it generates a lot of files, most of which are irrelevant if you only want to change the background colour.
So, I dug deeper into the zip it generates, and tried to narrow down what are the parts that matter, so I can make the minimum amount of changes to my app. Below is what I found out.
In the style generator, the relevant setting is Popup color, which affects "Overflow menu, submenu and spinner panel background".
Go on and generate the zip, but out of all the files generated, you only really need one image,
menu_dropdown_panel_example.9.png
, which looks something like this:So, add the different resolution versions of it to
res/drawable-*
. (And perhaps rename them tomenu_dropdown_panel.9.png
.)Then, as an example, in
res/values/themes.xml
you would have the following, withandroid:popupMenuStyle
andandroid:popupBackground
being the key settings.And, of course, in
AndroidManifest.xml
:What you get with this setup:
Note that I'm using
Theme.Holo.Light
as the base theme. If you useTheme.Holo
(Holo Dark), there's an additional step needed as this answer describes!Also, if you (like me) wanted to style the whole Action Bar, not just the menu, put something like this in
res/drawable/blue_action_bar_background.xml
:Works great at least on Android 4.0+ (API level 14+).
Within your app theme you can set the android:itemBackground property to change the color of the action menu.
For example: