Andy
Andy

Reputation: 10830

How to create the Action Overflow part of the ActionBar?

I have been looking online for a while, and there are very little tutorials on how to do this. Even the google docs have very vague info on how to put this stuff up there! I am sure its subtle, but I can't understand the terminology because I am still fairly new to Android. I would like to be able to get the Action Overflow icon to the right side of the ActionBar. Putting the view control was pretty understandable using the docs and example code when creating a project, but it does not have one for the Action Overflow. Thanks in advance!

Edit: I should probably elaborate. I would like the menus to default being under the action overflow. I found an eye opener answer to a similar question, but it only tells you how to put the menus at the top. How can I force them to go under the list? Is it even possible to force that? Thanks!

Upvotes: 1

Views: 3503

Answers (1)

Ilya Demidov
Ilya Demidov

Reputation: 3305

If you use API-11, it's not problem. If lower I am inviting you to this topic : The best way to create drop down menu in android 2.x like in ICS

In the case when API-11 and higher you must :

  1. Create menu xml like this :

         <?xml version="1.0" encoding="utf-8"?>
         <menu xmlns:android="http://schemas.android.com/apk/res/android" >
    
            <item
                android:id="@+id/item_refresh"
                android:icon="@drawable/ic_menu_refresh"
                android:title="Refresh"
                android:showAsAction="ifRoom|withText" />
    
            <item
                android:id="@+id/item_save"
                android:icon="@drawable/ic_menu_save"
                android:title="Save"
                android:showAsAction="ifRoom|withText" />
        </menu>
    

And create code like this :

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // gets the activity's default ActionBar
    ActionBar actionBar = getActionBar();
    actionBar.show();
    actionBar.setDisplayHomeAsUpEnabled(true);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu); //inflate our menu
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item){
    switch(item.getItemId()) {
    case R.id.item_refresh:
        //click on refresh item
        break;
    case R.id.item_save:
        //click on save item
        break;
    }
    return true;
}

Good Luck!

Upvotes: 4

Related Questions