Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android create custom overflow menu item

I want to create a custom overflow menu item in my ActionBar in addition at the Setting item like described in the image below:

Action bar

But if there is few space in the ActionBar I don't want that the Item1 and Item2 go into the Setting item as overflow, but into "my overflow item".

this is my menu xml code:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:icon="@android:drawable/ic_menu_agenda"  
    android:title="Item1"
    android:showAsAction="ifRoom|withText" />

<item
    android:icon="@android:drawable/ic_menu_add"  
    android:title="Item2"
    android:showAsAction="ifRoom|withText" />

<item android:id="@+id/pick_action_provider"
    android:icon="@android:drawable/ic_menu_sort_by_size" 
    android:showAsAction="always"
    android:title="Overflow" >
     <menu>  
        <item android:id="@+id/action_sort_size"  
              android:icon="@android:drawable/ic_menu_camera"  
              android:title="Item3" />  
        <item android:id="@+id/action_sort_alpha"  
              android:icon="@android:drawable/ic_menu_sort_alphabetically"  
              android:title="Item4" />  
    </menu>  
</item>

<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:showAsAction="never"
    android:title="@string/action_settings"/></menu>
like image 650
lory105 Avatar asked Jun 05 '13 10:06

lory105


People also ask

How do I create an overflow menu?

Within the Project tool window, locate the project's app -> res -> menu -> menu_menu_example. xml file and double-click on it to load it into the Layout Editor tool. Switch to Design mode if necessary and select and delete the default Settings menu item added by Android Studio so that the menu currently has no items.

What is contextual menu in Android?

A context menu is a floating menu that appears when the user performs a long-click on an element. It provides actions that affect the selected content or context frame.

What is Android system menu?

The Android System Settings menu allows you to control most aspects of your device—everything from establishing a new Wi-Fi or Bluetooth connection, to installing a third-party onscreen keyboard, to adjusting system sounds and screen brightness.


1 Answers

This will help you. I had coded this for overflow menu

In menu/main.xml file

<item
    android:id="@+id/overflow"
    android:orderInCategory="100"
    android:showAsAction="always"
    android:icon="@drawable/ic_overflow"
    android:title="">
    <menu>
        <item
            android:id="@+id/facebook"
            android:title="Facebook"/>

        <item
            android:id="@+id/Twitter"
            android:title="Twitter"/>

        <item
            android:id="@+id/Youtube"
            android:title="Youtube"/>
    </menu>
</item>

And here is your java code:

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.main, menu);

    return true;

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    // Handle action bar actions click

    super.onOptionsItemSelected(item);
    if(item.getItemId() == R.id.facebook){
        Toast.makeText(Getstarted.this, "Option pressed= facebook",Toast.LENGTH_LONG).show();
    }
    else if(item.getItemId() == R.id.Youtube){
        Toast.makeText(Getstarted.this, "Option pressed= youtube",Toast.LENGTH_LONG).show();
    }
    else if(item.getItemId() == R.id.Twitter){
        Toast.makeText(Getstarted.this, "Option pressed= twitter",Toast.LENGTH_LONG).show();
    }
    return true;    

}
like image 92
Prathamesh Gujar Avatar answered Nov 06 '22 11:11

Prathamesh Gujar