Wednesday, March 26, 2014

How to change background color and text color of sub menu of action bar.

When we use action bar in old android version such as Android 2.2(under Android 3.0), we need to use sub menu sometimes. Also, if menu overflow is not allowed in some devices we have to use sub menu as below.

  • sub menu example
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.helloworld.MainActivity" >
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
android:icon="@drawable/car_tow"
app:showAsAction="ifRoom"/>
<item
android:id="@+id/action_exit"
android:orderInCategory="101"
app:showAsAction="ifRoom"
android:title="more"
android:icon="@drawable/action_more">
<menu android:background="@color/action_bar">
    <item android:id="@+id/action_law"
    android:background="@color/action_bar"
    android:showAsAction="ifRoom"
    android:icon="@drawable/car_lawyer"
    android:title="test"/>
    <item android:id="@+id/action_buysell"
   android:showAsAction="ifRoom"
   android:icon="@drawable/car_buy_sell"
   android:title="test2"/>
   </menu>
</item>   
</menu>

This is how to change the background color and text color of sub menu.
It seems that the sub menu is not working as a part of action bar but as popup menu.
These three styles can be used in AppCompat and there probably are similar style items in other themes.

Widget.AppCompat.Base.PopupMenu
TextAppearance.AppCompat.Widget.PopupMenu.Large
TextAppearance.AppCompat.Widget.PopupMenu.Small

  • Style example
<resources>
<color name="white_opaque">#FFFFFFFF</color>
<style name="CustomActionBarTheme"
parent="@style/Theme.AppCompat.Light">
<item name="android:actionBarStyle">@style/MyActionBar</item>
<item name="android:popupMenuStyle">@style/PopupMenu</item>
<item name="android:textAppearanceLargePopupMenu">@style/PopupTextStyleLarge</item>
<item name="android:textAppearanceSmallPopupMenu">@style/PopupTextStyleSmall</item>
</style>
<style name="MyActionBar"
parent="@style/Widget.AppCompat.ActionBar">
 
<!-- action bar style is here -->
 
</style>
<style name="PopupMenu"
parent="@style/Widget.AppCompat.Base.PopupMenu">
<item name="android:textColor">@color/pitch_black</item>
<item name="android:popupBackground">@drawable/MyPopupMenuBackgroundImg</item>
</style>
<style name="PopupTextStyleLarge"
parent="@style/TextAppearance.AppCompat.Widget.PopupMenu.Large">
<item name="android:textColor">@color/white_opaque</item>
</style>   
<style name="PopupTextStyleSmall"
parent="@style/TextAppearance.AppCompat.Widget.PopupMenu.Small">
<item name="android:textColor">@color/white_opaque</item>
</style>   
</resources>

 

No comments:

Post a Comment