15

enter image description here

I'm having menu items being inflated from res/menu/menu.xml on to ActionBar, how do I add padding between menu items using android?

<item 
    android:id="@+id/home"
    android:showAsAction="always"
    android:title="Home"
    android:icon="@drawable/homeb"/>
<item 
    android:id="@+id/location"
    android:showAsAction="always"
    android:title="Locations"
    android:icon="@drawable/locationb"/>
<item
    android:id="@+id/preQualify"
    android:showAsAction="always"
    android:title="Pre-Qualify"
    android:icon="@drawable/prequalityb"/>
<item 
    android:id="@+id/products"
    android:showAsAction="always"
    android:title="Products"
    android:icon="@drawable/productb"/>

Java file:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_store_locator, menu);
    return true;
}
Anil M
  • 1,297
  • 2
  • 19
  • 42

3 Answers3

38

Found solution, added following lines in styles.xml file and it worked!!

<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:actionButtonStyle">@style/MyActionButtonStyle</item>
</style>

<style name="MyActionButtonStyle" parent="AppBaseTheme">
    <item name="android:minWidth">20dip</item>
    <item name="android:padding">0dip</item>
</style>
Anil M
  • 1,297
  • 2
  • 19
  • 42
  • 1
    The important thing is "minWidth" it's 80dp by default. Which means even without padding there can be some space. That was the problem in my case. – Evren Ozturk Oct 02 '14 at 13:50
  • 2
    How can i use this style in my menu item ?? If you share simple code , it will be helpful Thanks – aslamhossin Jan 21 '17 at 08:46
  • I noticed that our theme style and "MyActionButtonStyle" should have the same parent name – arniotaki Jun 27 '18 at 08:33
9

create drawable xml . like res/drawable/shape_green_rect.xml

<?xml version="1.0" encoding="utf-8"?>
<shape
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="rectangle">

  <!-- Specify a semi-transparent solid green background color -->
  <solid android:color="#5500FF66" />

  <!-- Specify a dark green border -->
  <stroke 
    android:width="5dp"
    android:color="#009933" />

  <!-- Specify the margins that all content inside the drawable must adhere to -->
  <padding
    android:left="30dp"
    android:right="30dp"
    android:top="30dp"
    android:bottom="30dp" />
</shape>

Add this drawable in

android:icon="@drawable/shape_green_rect"

This way we can able to add padding in menu.

Thanks.

Md Abdul Gafur
  • 6,213
  • 2
  • 27
  • 37
  • Thanks for the reply!! If I add `android:icon="@drawable/shape_green_rect"` where do I add my icons in the xml file? – Anil M Mar 28 '13 at 09:57
-2

Padding I don't know, but you can add margin by the following way:

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(left, top, right, bottom);
yourItem.setLayoutParams(lp);

Set your values instead of "left, top, right, bottom". In this example I'm adding margin on an item that you would get with its ID.

Hope this helps.

Community
  • 1
  • 1
Damien R.
  • 3,383
  • 1
  • 21
  • 32