Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to style the menu items on an Android action bar

There's been many questions on styling on action bars, but the ones I've found either are relating to styling the tabs, or have answers that don't work for me.

The question is really quite simple. I want to be able to change the text styling (even just colour) of the menu items in the action bar.

I've read this: http://android-developers.blogspot.com/2011/04/customizing-action-bar.html?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+blogspot%2FhsDu+%28Android+Developers+Blog%29

And this question: Style an Action Bar in Android Honeycomb

From which I have put together a test application that I am using to try and get the menu items to change. It uses all the default values for an app created in the eclipse android plugin, except for the following.

A styles file:

<?xml version="1.0" encoding="utf-8"?> <resources>  <style name="MyAppTheme" parent="@android:style/Theme.Holo.Light">     <item name="android:actionBarStyle">@style/MyActionBar</item> </style>  <style name="MyActionBar" parent="@android:style/Widget.Holo.ActionBar">     <item name="android:titleTextStyle">@style/MyActionBar.TitleTextStyle</item>     <item name="android:actionMenuTextAppearance">@style/MyActionBar.MenuTextStyle</item> </style>  <style name="MyActionBar.TitleTextStyle"     parent="android:style/TextAppearance.Holo.Widget.ActionBar.Title">     <item name="android:textColor">#F0F</item>     <item name="android:textStyle">bold</item>     <item name="android:textSize">24dip</item> </style>  <style name="MyActionBar.MenuTextStyle"     parent="android:style/TextAppearance.Holo.Widget.ActionBar.Title">     <item name="android:textColor">#F0F</item>     <item name="android:textStyle">bold</item>     <item name="android:textSize">24dip</item> </style> </resources> 

A menu for the action bar:

<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android">  <item android:showAsAction="always|withText" android:icon="@android:drawable/ic_menu_edit"     android:id="@+id/menu_item1" android:title="menu_item1"></item>  <item android:showAsAction="always|withText" android:icon="@android:drawable/ic_menu_edit"     android:id="@+id/menu_item2" android:title="menu_item2"></item>  </menu> 

The main activity:

public class Main extends Activity {  @Override public void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.main); }  /**  * Create the options menu that is shown on the action bar  */ @Override public boolean onCreateOptionsMenu(Menu menu) {     MenuInflater inflater = getMenuInflater();     inflater.inflate(R.menu.main_menu, menu);     return true; } } 

The application compiles and runs. The styling for the action bar title text works perfectly (is that lovely shade of pink #F0F I've defined). The menu items do not change appear but with default styling (holo light).

What am I doing wrong ?

like image 441
sksamuel Avatar asked May 20 '11 12:05

sksamuel


People also ask

How do I customize my action bar?

This example demonstrate about how to create a custom action bar in Android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How will you replace a menu with action bar in Android?

To replace an app's default action bar with a Toolbar : Create a new custom theme and modify the app's properties so that it uses this new theme. Disable the windowActionBar attribute in the custom theme and enable the windowNoTitle attribute. Define a layout for the Toolbar .

Which method is used to create menu items on Action Bar?

The Android tooling automatically creates a reference to menu item entries in the R file, so that the menu resource can be accessed. An activity adds entries to the action bar in its onCreateOptionsMenu() method. The showAsAction attribute allows you to define how the action is displayed.


1 Answers

Instead of having the android:actionMenuTextAppearance item under your action bar style, move it under your app theme.

like image 59
Chris Feist Avatar answered Oct 05 '22 00:10

Chris Feist