I'm using ActionBarSherlock in my project, I'm displaying 3 action buttons with a custom minWidth. Here is my code:
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_item1" android:showAsAction="always"
android:title="@string/empty" android:icon="@drawable/item1_selector" />
<item android:id="@+id/menu_item2" android:showAsAction="always"
android:title="@string/empty" android:icon="@drawable/item2_selector" />
<item android:id="@+id/menu_item3" android:showAsAction="always"
android:title="@string/empty" android:icon="@drawable/item3_selector" />
</menu>
And my styles
values/styles.xml
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="AppTheme" parent="Theme.Sherlock.Light">
<item name="android:windowBackground">@android:color/white</item>
<item name="actionBarItemBackground">@color/White</item>
<item name="actionButtonStyle">@style/CustomActionButtonStyle</item>
</style>
<style name="CustomActionButtonStyle" parent="Widget.Sherlock.ActionButton">
<item name="android:minWidth">32dip</item>
<item name="android:padding">0dip</item>
</style>
</resources>
values-v14/styles.xml
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="AppTheme" parent="Theme.Sherlock.Light">
<item name="android:windowBackground">@android:color/white</item>
<item name="android:actionBarItemBackground">@color/White</item>
<item name="actionBarItemBackground">@color/White</item>
<item name="android:actionButtonStyle">@style/CustomActionButtonStyle</item>
<item name="actionButtonStyle">@style/CustomActionButtonStyle</item>
</style>
</resources>
This code is not working with Galaxy S2 with android 4.1.2 (HDPI) and Galaxy S3 mini android 4.1.2 (HDPI). In those devices my action buttons seem to have the default width(56dip) so the space between them looks bigger. However in a Galaxy S3 with the same android version everything works OK, the style is applied. My code also works on many devices with different android versions and screen densities.
How can achieve to set minWidth or reducing the space consistently for many different devices, is there any way to do this programatically?
Finally I ended up setting a custom action view (ImageButton) for every item in the action bar. First I changed my menu.xml by removing the icon field.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_item1" android:showAsAction="always"
android:title="@string/empty" />
<item android:id="@+id/menu_item2" android:showAsAction="always"
android:title="@string/empty" />
<item android:id="@+id/menu_item3" android:showAsAction="always"
android:title="@string/empty" />
</menu>
Then I created a custom layout for my action button applying my existent button style (custom_action_button.xml)
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/padding_small"
android:clickable="true">
<ImageButton
android:id="@+id/custom_action_bar_button"
style="@style/CustomActionButtonStyle"
android:src="@drawable/some_drawable"
android:clickable="false"
android:focusable="true"/>
</FrameLayout>
Finally in my activity (onCreateOptionsMenu method) I did:
getSupportMenuInflater().inflate(R.menu.main_menu, menu);
final MenuItem item = menu.findItem(R.id.menu_item1);
LayoutInflater li = LayoutInflater.from(this);
View actionView = li.inflate(R.layout.custom_action_button, null);
ImageButton menuItemButton = (ImageButton) actionView.findViewById(R.id.custom_action_bar_button);
item.setActionView(actionView);
//...and so on for my other 2 buttons
//Setting a listener for the button
item.getActionView().setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
//do stuff
}
});
In this way I was able to set the spacing between buttons consistently in all my devices using my CustomActionButtonStyle.