3

I use OnCreateOptionsMenu for users to search a given list which then populates a spinner. The problem I am facing is one of two things. The spinner is visible before a user clicks on search and it is always populated with the first item in my list. My question is how can I disable/hide/remove the spinner untill a user starts searching and how can I stop the first item being populated?

Also is it possible when a user clicks the search box that the spinner fully expands instead of having to click on it to show the results?

Menu.main:

<?xml version="1.0" encoding="utf-8" ?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">
  <item android:id="@+id/action_search"
        android:title="Search"
        android:icon="@android:drawable/ic_menu_search"
        app:showAsAction="always|collapseActionView"
        app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>

Layout.Main:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:minWidth="25px"
    android:minHeight="25px">
    <Spinner
        android:layout_width="match_parent"
        android:layout_height="49.0dp"
        android:id="@+id/spinner1" />
    //etc...
</LinearLayout>

MainActivity:

   private Android.Support.V7.Widget.SearchView _searchView;
   private ArrayAdapter _adapter;
   protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        SetContentView(Resource.Layout.Main);
        //list omitted 
        spinner = FindViewById<Spinner>(Resource.Id.spinner1);
        _adapter = new ArrayAdapter<MyClass>(this, Android.Resource.Layout.SimpleSpinnerDropDownItem, list); 
        spinner.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>(spinner_ItemSelected);
        spinner.Adapter = _adapter;
    }
    private void spinner_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
    {
        Spinner spinner = (Spinner)sender;
        string toast = string.Format("Selected text is {0}", spinner.GetItemAtPosition(e.Position));
        Toast.MakeText(this, toast, ToastLength.Long).Show();
    }
    public override bool OnCreateOptionsMenu(IMenu menu)
    {
        MenuInflater.Inflate(Resource.Menu.main, menu);
        var item = menu.FindItem(Resource.Id.action_search);
        var searchView = MenuItemCompat.GetActionView(item);
        _searchView = searchView.JavaCast<Android.Support.V7.Widget.SearchView>();
        _searchView.QueryTextChange += (s, e) => _adapter.Filter.InvokeFilter(e.NewText);
        _searchView.QueryTextSubmit += (s, e) =>
        {
            Toast.MakeText(this, "Searched for" + e.Query, ToastLength.Short).Show();
            e.Handled = true;
        };
        return true;
    }
Kirsty White
  • 1,210
  • 3
  • 26
  • 54

1 Answers1

2

This is the best solution for your problem.

https://stackoverflow.com/a/15162795/5920347

I hope this help. Regards.

Try this

<LinearLayout
    android:id="@+id/contacts_type"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"

    //Change the visibility of the Layout. When you click the button change to visible  
    android:visibility="gone">

    <Spinner
    android:layout_width="match_parent"
    android:layout_height="49.0dp"
    android:id="@+id/spinner1" />

the XML Attributes Spinner I don't watch a property that hide directly you can read again here I paste the page. https://developer.android.com/reference/android/widget/Spinner.html

Community
  • 1
  • 1