0

I have a menu and I would like to apply the following background style:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >    
    <corners android:radius="10dip" />
    <stroke android:color="@color/pink" 
            android:width="6dip"></stroke>
    <solid android:color="@color/white"></solid>
</shape>

I have a Java class just to treat the operations related with the menu. This class extends ActionBarActivity.

The code of my menu is the following:

<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.android.ExtendedActivity">

 <item android:id="@+id/action_share"
     android:title="Share"
     android:orderInCategory="100"/>

 <item android:id="@+id/action_about"
     android:title="About"
     android:orderInCategory="100"/>
 </menu>

Does anyone know how can I set that background style to my menu?

porthfind
  • 1,581
  • 3
  • 17
  • 30
  • Take a look at the answer with the most upvotes in this [question](http://stackoverflow.com/questions/19659637/how-to-change-the-background-color-of-action-bars-option-menu-in-android-4-2). I think it is a similar problem. – siriuseteor77 Sep 14 '15 at 15:20
  • You have to change the style in res/values/styles/style.xml, but if you are trying to change the drawer menu is different! First of all you have to use AppCompatActivity because ActionBarActivity is deprecated, then you have to create the drawer_layout.xml in which you can set the style of the menu. =) – michoprogrammer Sep 14 '15 at 15:45
  • I found for you some similar question http://stackoverflow.com/questions/28591447/style-appcompat-v7-toolbar-menu-background and this one http://stackoverflow.com/questions/28352963/change-toolbar-menu-item-color-non-hidden-action and this is a good starting point http://developer.android.com/guide/topics/ui/menus.html . I hope u can find a solution that fits for you! ^^ – michoprogrammer Sep 14 '15 at 15:48
  • @MachoProgrammer thanks, but it didn't resolve my problem since themes only can be applied to menu items and not to the whole menu. Nevertheless I've applied AppCompatActivity instead of ActionBarActivity. – porthfind Sep 15 '15 at 14:57

1 Answers1

0

In your style you will have something like this

<style name="YourTheme.Main" parent="Theme.AppCompat.Light.NoActionBar">
... <item name="android:activatedBackgroundIndicator">@drawable/selectable_background</item> ... </style> 

and in your drawable something like

<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@color/your_color" android:state_pressed="true"/> <item android:drawable="@drawable/selected_background" android:state_selected="true"/> <item android:drawable="@drawable/selected_background" android:state_checked="true"/> </selector> 

and finally you can use your backgroud as a selected_background like this

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <corners android:radius="10dip" /> <stroke android:color="@color/pink" android:width="6dip"></stroke> <solid android:color="@color/white"></solid> </shape>.

I'm so sorry for the very simplified explanation but without a full code demo app is very tricky! I hope this help :-)

michoprogrammer
  • 1,159
  • 2
  • 18
  • 45
  • Maybe this http://stackoverflow.com/questions/30594025/how-to-customize-item-background-and-item-text-color-inside-navigationview is a more complete example! – michoprogrammer Sep 16 '15 at 10:28