Skip to content

Instantly share code, notes, and snippets.

@marczych
Last active April 12, 2023 12:54
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marczych/6908324 to your computer and use it in GitHub Desktop.
Save marczych/6908324 to your computer and use it in GitHub Desktop.
This is a workaround for `android:showAsAction="withText"` not displaying text on narrow layouts e.g. in portrait on phones. This uses styles found in ActionBarSherlock so it matches the native styling. I tested it with ABS v4.4.0 but earlier versions are probably compatible.Note: This should only be used if you absolutely have to display the te…
<?xml version="1.0" encoding="utf-8"?>
<!-- TODO: This could be genericized by removing android:text so the caller
can set it dynamically. -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
style="@style/Widget.Sherlock.ActionButton"
android:text="@string/text"
android:drawableLeft="@drawable/icon"
android:background="@drawable/abs__item_background_holo_dark"
android:clickable="true"
android:textSize="12sp"
android:textStyle="bold"
android:textColor="?attr/actionMenuTextColor"
android:textAllCaps="@bool/abs__config_actionMenuItemAllCaps" />
<!-- Note: The text attributes were copied from:
"@style/TextAppearance.Sherlock.Widget.ActionBar.Menu" -->
<item
android:id="@+id/button_id"
android:icon="@drawable/icon"
android:showAsAction="always"
android:actionLayout="@layout/action_button"
android:title="@string/text"/>
public class MyActivity extends Activity {
...
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Trigger onOptionsItemSelected for the custom menu item because it doesn't
// happen automatically.
final MenuItem item = menu.findItem(R.id.button_id);
barcodeItem.getActionView().setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
onOptionsItemSelected(item);
}
});
// This sets up the long press listener to make the tool tip.
// CheatSheet can be found here: https://gist.github.com/romannurik/3982005
CheatSheet.setup(item.getActionView(), R.string.text);
return super.onCreateOptionsMenu(menu);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment