0

I made a drop-down item on an Action bar just as said here How to add a Dropdown item on the action bar so I have a menu.xml with

<item
android:id="@+id/menuSpinner"
android:showAsAction="ifRoom"
android:actionLayout="@layout/options"  />

and options.xml

<?xml version="1.0" encoding="utf-8"?>
<Spinner xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="@array/my_array" />

and array in strings.xml

<string-array name="my_array">
    <item>ONE</item>
    <item>TWO</item>
    <item>THREE</item>
</string-array>

Everything looks fine in Activity.

Please, help, how should I get onClick message, or just how I may know what item is selected (visible) on Action Bar. May be something in this code..?

if (item.getItemId() == R.id.menuSpinner) {
                      ...
            }

UPD: by now I did this

  1. Put an id to my spinner in options.xml

  2. in code wrote this

    Spinner sp = (Spinner) findViewById(R.id.spinner_menu);

    if (sp.getSelectedItemId()==0) {

    ...

    }

this is suits to mi because I do not need to act immediatly to user spinner clicks.

Community
  • 1
  • 1
Foenix
  • 991
  • 3
  • 10
  • 23

3 Answers3

2

Try this.

  1. add items to spinner list
  2. implement onNavigationItemSelected() to get item position and item id respectively.

example:

@Override

public boolean onNavigationItemSelected(int itemPosition, long itemId) {
    switch (itemPosition) {
    case 0:
        Toast.makeText(getApplicationContext(), "location 1",
                Toast.LENGTH_SHORT).show();

    return true;
    case 1:
        Toast.makeText(getApplicationContext(), "location 2",
                Toast.LENGTH_SHORT).

    default:
        Toast.makeText(getApplicationContext(), "location 4",
                Toast.LENGTH_SHORT).show();
    }
    return false;
}
Silambarasan Poonguti
  • 9,386
  • 4
  • 45
  • 38
1

Adding dropdowns to the action bar

for detail explanation

enter image description here

private void InitializeActionBar()
{
    ActionBar.NavigationMode = ActionBarNavigationMode.List;

    ActionBar.SetListNavigationCallbacks(
        new NavigationSpinnerAdapter(this),
        new NavigationListener());
}

public class NavigationSpinnerAdapter : BaseAdapter
{
    private List<Java.Lang.Object> _spinnerItems;
    private LayoutInflater _layoutInflater;

    public NavigationSpinnerAdapter(Context context)
    {
        _spinnerItems = new List<Java.Lang.Object>();

        // Create java strings for this sample.
        // This saves a bit on JNI handles.
        _spinnerItems.Add(new Java.Lang.String("Sample item 1"));
        _spinnerItems.Add(new Java.Lang.String("Sample item 2"));
        _spinnerItems.Add(new Java.Lang.String("Sample item 3"));

        // Retrieve the layout inflater from the provided context
        _layoutInflater = LayoutInflater.FromContext(context);
    }

    public override Object GetItem(int position)
    {
        return _spinnerItems[position];
    }

    public override long GetItemId(int position)
    {
        return position;
    }

    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        var view = convertView;

        // Try to reuse views as much as possible.
        // It is alot faster than inflating new views all the time
        // and it saves quite a bit on memory usage aswell.
        if (view == null)
        {
            // inflate a new layout for the view.
            view = _layoutInflater.Inflate(Resource.Layout.SpinnerItem, parent, false);
        }

        var textView = view.FindViewById<TextView>(Resource.Id.DisplayTextLabel);
        textView.Text = _spinnerItems[position].ToString();

        return view;
    }

    public override int Count
    {
        get { return _spinnerItems.Count; }
    }
}

public class NavigationListener: Java.Lang.Object, ActionBar.IOnNavigationListener
{
    public bool OnNavigationItemSelected(int itemPosition, long itemId)
    {
        // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 
        // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        //catch clicked item
        // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

        return false;
    }
}
<menu xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:id="@+id/menu_search" android:title="Save" android:showAsAction="always"
        android:actionViewClass="android.widget.SearchView"/>
</menu>

public override bool OnCreateOptionsMenu(IMenu menu)
{
    MenuInflater.Inflate(Resource.Menu.ActionItems, menu);

    var searchView = (SearchView)menu.FindItem(Resource.Id.menu_search).ActionView;
    searchView.SearchClick += OnSearchClicked;

    return true;
}
Talha
  • 12,673
  • 5
  • 49
  • 68
  • Am I right that in this case I cannot manage in what position this navigation spinner will appear? In other way that decition http://stackoverflow.com/questions/8312344/how-to-add-a-dropdown-item-on-the-action-bar have its own place and somehow someone can manage its clicks.. – Foenix Nov 24 '12 at 09:35
  • Can you really add multiple of those dropdown menus to the actionbar? To me it seems only one can be added (as seen in the sample). – AgentKnopf Mar 12 '13 at 17:41
  • How to add the Navigation List below the Title (PhotoCrumbs). similar to gmail app. – madan V Nov 01 '13 at 07:19
  • Not sure if the gmail app version is a "Title" to begin with. I got around this one with setting the title to blank then then you can add a spinner (PhotoCrumbs) to sit where the "Title" would normally be. The alternative would be making a custom menu bar. – SatanEnglish Nov 06 '13 at 02:38
0

If you simply want to use this as a submenu, have you looked at the submenu APIs?

See this question for some more info on this. The original question has an example of how to do this from your menu xml right at the beginning and then goes into more detail about dynamically generating menu items from a data source. If you have only static submenu items to present (as seems to be the case since you have a string-array defining them in your question) you should be able to stop at the initial menu xml definition, and then handle submenu item clicks as if they were any other menu item.

Community
  • 1
  • 1
adamp
  • 28,862
  • 9
  • 81
  • 69
  • I have looked, but I cannot still manage to this problem.. and yes, this menu is static and suits to me veri well. I still cannot handle submenu items click. I tried several times but not one my solution is right, that is why I asked my Q. Everybody talk about other ways to create a submeny but if there is this case (I gave a link) there is a click handle I guess. – Foenix Nov 24 '12 at 09:49