2

I had been developing Android applications, but I don't know much about 4+ versions of Android well. Therefore, please help me - I have made Android application with tabs for navigation:

final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

But I also need to add dropdown menu to ActionBar for other goals. Can I do it? Please, if it's possible, give me an example. Thank you in advance.

2 Answers 2

15

You can use something called an android spinner. It looks like this:

enter image description here

You can customize it in many ways to suit your apps design too.

Here's a great tutorial by Google on how to use these:

http://developer.android.com/guide/topics/ui/controls/spinner.html

If you want to add this to an action bar, you can do it via a spinner adapter as detailed here: http://developer.android.com/guide/topics/ui/actionbar.html#Dropdown

If you want to add icons to do certain actions, then you can see this: http://developer.android.com/guide/topics/ui/actionbar.html#ActionItems

If you want to do certain actions in the bar itself (like search in the google search app) then see this: http://developer.android.com/guide/topics/ui/actionbar.html#ActionView

If you want to add navigation tabs, then see this: http://developer.android.com/guide/topics/ui/actionbar.html#Tabs

Sign up to request clarification or add additional context in comments.

6 Comments

I know about existing of this element, but I don't know how to add spinner to Actionbar
It doesn't match my task. Please, tell me, how can I add TextView or icons to ActionBar?
added some more resources to help you out there. You can't add normal textViews and icons to action bar since it's a special UI element and not a normal layout. But you can add certain things to it which may help you accomplish your task.
I need to have ActionBar with buttons/icons, for example icon "Search" in order to helping user to do common things
add action items for each task. see the resource I've mentioned above. You can add any icons you want to the action items and you can set action listeners that will do whatever you want when the user clicks them.
|
3

Absolutely the best and and the simplest answer I found so far is here.

Basically, no need for custom layout in this case. Just set the actonViewClass:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >

  <item android:id="@+id/spinner"
    yourapp:showAsAction="ifRoom"
    yourapp:actionViewClass="android.widget.Spinner" />
</menu>

And then handle it in onCreateOptionsMenu, as usual:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_layout, menu);
    MenuItem item = menu.findItem(R.id.spinner);
    Spinner spinner = (Spinner) MenuItemCompat.getActionView(item);
    spinner.setAdapter(adapter); // set the adapter to provide layout of rows and content
    s.setOnItemSelectedListener(onItemSelectedListener); // set the listener, to perform actions based on item selection

This is by far the simplest and cleanest solution. Credits to François POYER, the original author.

5 Comments

Why not just link to the answer then?
if I would have given the answer, you would have said links are not reliable :)
Then why not link it in comment?
Links are not reliable, it should have been a comment.
this is a solution to nothing. more complete examples of spinners have been provided

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.