0

I'm writing an android app and want to create an action bar as follows:

enter image description here

I have tried this:

my menu

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

    <item android:id="@+id/actions_saved"
          android:title="@string/action_settings"
          android:icon="@android:drawable/btn_star"
          android:showAsAction="always" />

    <!-- Settings, should always be in the overflow -->
    <item android:id="@+id/action_settings"
          android:title="@string/action_settings"
          android:showAsAction="never" />

</menu>

in my class

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu items for use in the action bar
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_actions_menu, menu);
    return super.onCreateOptionsMenu(menu);
}
  1. Is there any way to create on all my activities beside 2 of them?

  2. How do I force the star icon be aligned to left? Can I use a regular LinearLayout?

  3. How do I force the settings action be in an overflow menu?

Paul Lammertsma
  • 37,593
  • 16
  • 136
  • 187
Elad Benda
  • 35,076
  • 87
  • 265
  • 471

1 Answers1

0

Your questions seem to be fairly straight-forward and could be better understood with a brief review of the Menu guide on d.android.com.

  1. You could inflate the same menu resource in all activities you desire the menu to appear in. If you want to keep the logic handling onOptionsItemSelected() in one place, you could consider introducing a super class or delegating the handling of some actions.

  2. As per the documentation:

    If necessary, you can re-order the menu items with the android:orderInCategory attribute in each you need to move.

  3. You have already done so with android:showAsAction="never". Note that some devices use a hardware menu key; you should observe the device's preferred behavior to conceal overflow menus on these devices for a consistent user experience.

Community
  • 1
  • 1
Paul Lammertsma
  • 37,593
  • 16
  • 136
  • 187
  • thanks. though i'm a bit confused. this xml defines both "settings" button (bottom right for galaxy devices) and also floating menu? – Elad Benda Feb 01 '14 at 20:47
  • regarding (3) I understand that should have been enough. But yet I see no 3 dots on my action bar. nothing. Just a star icon – Elad Benda Feb 01 '14 at 20:50
  • Does your device have a hardware menu key? If so, I believe this may be your confusion. Normally, the overflow menu appears in the action bar. On devices with a hardware menu key, the overflow menu pops up from the bottom of the screen. – Paul Lammertsma Feb 02 '14 at 00:11
  • so how can i force the 3 dots also to appear on my device? I don't see them with current "never" attribute – Elad Benda Feb 02 '14 at 06:14
  • 1
    *If your problem is realted to your device has a hardware menu button*, you may want to take a look at these two questions about forcing the overflow to appear: [changing device configuration for HC+ ActionBar](http://stackoverflow.com/questions/9286822/how-to-force-use-of-overflow-menu-on-devices-with-menu-button) or [ActionBarSherlock-specific solution using the `Theme.Sherlock.ForceOverflow` theme](http://stackoverflow.com/questions/13179620/force-overflow-menu-in-actionbarsherlock). I would nevertheless discourage you from doing this, as it gives an inconsistent user experience. – Paul Lammertsma Feb 02 '14 at 16:59
  • regarding `android:orderInCategory` how can I create a menu item left to the application's icon on the action bar? or how to have an action bar without an application icon for only one activity? – Elad Benda Feb 03 '14 at 22:11
  • I'm afraid that's not possible to place action items left of the application icon and would again discourage you from doing this. You will only be able to do this using a custom view in your action bar, or replacing the action bar altogether with something else. As for removing the action bar for an activity, you can simply provide a non-Sherlock or ActionBar theme. – Paul Lammertsma Feb 04 '14 at 00:13
  • `You will only be able to do this using a custom view in your action bar` i'll google this. but if you have a quick link, i'll be grateful. – Elad Benda Feb 04 '14 at 08:05
  • You'll have to make use of [`setCustomView()`](http://developer.android.com/reference/android/app/ActionBar.html#setCustomView(int)). – Paul Lammertsma Feb 04 '14 at 08:34