How to make a Submenu in Action Bar?

5

There was a need to create a menu with submenu in action bar , equal to this:

Couldyouhelpme?

MyXMLmenu:

<menuxmlns: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);
    }
}  
    
asked by anonymous 13.03.2014 / 13:30

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);
    }
}

Android Developer Reference

    
13.03.2014 / 13:41
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 menu.add () 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);
}
    
13.03.2014 / 13:41
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 can not occupy more than 50% of the width of the bar. Action buttons on background action bars can use full width.
  • The width of the screen in density-independent pixels (dp) determine the number of items that fit the main action:

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

As we can verify if you use a Nexus S and put 2 icons, the submenu will not appear, but if you enter 3 or more the submenu will appear automatically.

Source and more

    
13.03.2014 / 14:36