Search code examples
xamarin.androidclicksearchview

When I click SearchView, click event doesn't fire (Xamarin.Android)


I have the following SearchView

<SearchView
    android:minWidth="25px"
    android:minHeight="25px"
    android:background="@drawable/rounded_border"
    android:clickable="true"
    android:iconifiedByDefault="false"
    android:focusable="false"
    android:layout_weight="1"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:id="@+id/searchViewCustomers" />

When I click on it I want one of the widgets of the activity to become visible and the other one to become gone like that:

searchView.Click += delegate
{
    customersRecyclerView.Visibility = ViewStates.Visible;
    customerDataContainer.Visibility = ViewStates.Gone;
};

But when I run the application and tap on the searchView the widgets doesn't become visible/gone and when I put break point on serachView.Click, program execution never stops there. How to make the widgets visible/gone when I tap on the searchView widget?


Solution

  • The way I would do it is, add a focus change event to the searchview :

     searchView.FocusChange += SearchView_FocusChange;
     private void SearchView_FocusChange(object sender, View.FocusChangeEventArgs e)
        {
                  if(searchView.HasFocus)
                  {
                   //Visibility code
                  }
        }
    

    UPDATE : First and foremost I would use the Appcompat Searchview for compatibility purpose, Something like this:

    <android.support.v7.widget.SearchView
    android:minWidth="25px"
    android:minHeight="25px"
    android:background="@drawable/rounded_border"
    android:clickable="true"
    android:iconifiedByDefault="false"
    android:focusable="false"
    android:layout_weight="1"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:id="@+id/searchViewCustomers" />
    

    Then I would try using the focus change listener(Same as Above).

    If that does not work I would use on query focus change listener something like this:

    searchView.SetOnQueryTextFocusChangeListener(new FocusChangeListenerClass());
    

    And add the listener class something like this :

    public class FocusChangeListenerClass : Java.Lang.Object, IOnFocusChangeListener
    {
        public void OnFocusChange(Android.Views.View v, bool hasFocus)
        {
    
        }
    }
    

    Update:

    Activity:

    public class ClientsActivity : Activity
    {
        .......
        .......
        Android.Widget.SearchView searchView;
        RecyclerView customersRecyclerView;
        ScrollView customerDataContainer;
        .......
        protected override void OnCreate(Bundle savedInstanceState)
        {
             ........
             ........
             searchView.SetOnQueryTextFocusChangeListener(new FocusChangeListenerClass(ref customersRecyclerView, ref customerDataContainer));
        }
    }
    

    FocusChangeListenerClass.cs:

    public class FocusChangeListenerClass : Java.Lang.Object, IOnFocusChangeListener
    {
        RecyclerView customersRecyclerView;
        ScrollView customerDataContainer;
        public FocusChangeListenerClass(ref RecyclerView recyclerView, ref ScrollView dataContainer)
        {
            customersRecyclerView = recyclerView;
            customerDataContainer = dataContainer;
        }
    
        public void OnFocusChange(Android.Views.View v, bool hasFocus)
        {
            if(hasFocus == true)
            {
                customersRecyclerView.Visibility = ViewStates.Visible;
                customerDataContainer.Visibility = ViewStates.Gone;
            }
            else
            {
                customersRecyclerView.Visibility = ViewStates.Gone;
                customerDataContainer.Visibility = ViewStates.Visible;
            }
        }
    
    }
    

    There is no need to change <SearchView> to <android.support.v7.widget.SearchView> in the .axml file.