How to Make a Submenu in Action Bar?

Asked

Viewed 4,187 times

5

A need to create a menu with submenu in action bar, like this:

Menu e submenu

Could you help me?

Man XML menu:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:apk="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/menu_atualizar"
    android:title="@string/menu_atualizar"
    apk:showAsAction="ifRoom"
 />
</menu>

My code:

@Override
public boolean onCreateOptionsMenu(Menu menu) {     
    getMenuInflater().inflate(R.menu.activity_main, menu);      
    return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.menu_atualizar:
        //código aqui...
    default:
        return super.onOptionsItemSelected(item);
    }
}  

3 answers

6


To have a submenu, you must have a menu within a menu. Using XML This way:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/file"
          android:icon="@drawable/file"
           android:title="@string/file"
           apk:showAsAction="ifRoom" 
     >
        <!-- submenu -->
        <menu>
            <item android:id="@+id/menu_atualizar"
                  android:title="Form 1"
                  />
            <item android:id="@+id/form_2"
                  android:title="Form 2" />
        </menu>
    </item>
</menu>

In your example you only have a menu, not a menu within a menu(submenu).

Java:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.activity_main, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handler dos cliques em cada menu
    switch (item.getItemId()) {
        case R.id.menu_atualizar:
            //codigo
        case R.id.form_2:
            //codigo
        default:
            return super.onOptionsItemSelected(item);
    }
}

Reference of Android Developers

1

Actually the name of this is not submenu, but only menu. To create it in your current code is just a matter of adding one or more lines add menu.() as below:

@Override
public boolean onCreateOptionsMenu(Menu menu) {     
    getMenuInflater().inflate(R.menu.activity_main, menu);      
    menu.add(Menu.NONE, Menu.NONE, 0, R.string.form1);
    menu.add(Menu.NONE, Menu.NONE, 1, R.string.form2);
    return super.onCreateOptionsMenu(menu);
}

1

Only complementing what has already been answered.

This "sub-menu" is created automatically by Actionbar and what will determine whether or not the sub-menu will appear are the following rules;

  • Action buttons in the main action bar cannot occupy more than 50% of the bar width. Action buttons on background action bars can use full width.
  • Screen width in density independent pixels ( dp ) determine the number of items that fit the main bar of action:

    1. less than 360 dp = 2 icons
    2. 360-499 dp = 3 icons
    3. 500-599 dp = 4 icons
    4. 600 dp e = 5 larger icons

As we can check if Voce uses a Nexus S and puts 2 icons, the sub-menu will not appear, but if you put 3 or more the sub-menu will appear automatically.

Source and more information

  • I got it, baby (:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.