Santiago
Santiago

Reputation: 982

Android Option Menu showing as list

I have created a menu which is activated on clicking the menu button on the device however it is showing as a list

Menu

My in res/menu/menu.xml code is as follows:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menuRefresh"
     android:icon="@drawable/ic_menu_refresh"
     android:title="Refresh"/>
<item android:id="@+id/menuAbout"
     android:icon="@drawable/ic_menu_info_details"
     android:title="About"/>
</menu>

and in my main activity i have:

//Initiate Menu XMl file
    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.menu, menu);
        return true;
    }

    /**
     * Even handling for individual menu items selected
     * Identity single menu item by its id
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        switch(item.getItemId()){
        case R.id.menuRefresh:
            Toast.makeText(MainActivity.this, "Refresh Selected", Toast.LENGTH_SHORT).show();
            return true;

        case R.id.menuAbout:
            Toast.makeText(MainActivity.this, "About Selected", Toast.LENGTH_SHORT).show();
            return true;

        default:
            return super.onOptionsItemSelected(item);
        }
    }

If i select an item from the list, i get the notification as expected.

How do i make the menu look like the Options menu on the android dev site?

Upvotes: 0

Views: 5542

Answers (2)

cprcrack
cprcrack

Reputation: 19129

Even in new devices, you can set the theme to an old one and the menu will display in the old 6-item table layout:

<style name="AppBaseTheme" parent="android:Theme">

Upvotes: 1

A--C
A--C

Reputation: 36449

That is what the options menu looks like on new devices, a list. If you want your options to be part of the top bar (called the Action Bar), add android:showAsAction to your xml.

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menuRefresh"
     android:icon="@drawable/ic_menu_refresh"
     android:title="Refresh"
      android:showAsAction = "always"/>
<item android:id="@+id/menuAbout"
     android:icon="@drawable/ic_menu_info_details"
     android:title="About"
     android:showAsAction = "always"/>
</menu>

For demo purposes, I chose the attribute to be "always", but you have more options:

  • "ifRoom"

  • "never"

  • "withText"

  • "always"

  • "collapseActionView"

Upvotes: 2

Related Questions