Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top