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

Multi tool use
When I click SearchView, click event doesn't fire (Xamarin.Android)
I have the following SearchView
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:
visible
gone
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?
searchView
visible/gone
serachView.Click
searchView
1 Answer
1
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:
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:
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.
<SearchView>
<android.support.v7.widget.SearchView>
.axml
When I tap on
searchView
, SearchView_FocusChange
method doesn't fire up.– ProfileForStack4
Jul 2 at 13:46
searchView
SearchView_FocusChange
I have updated my answer kindly take a look
– G.hakim
Jul 3 at 5:54
still doesn't work
– ProfileForStack4
Jul 3 at 6:02
check again please
– G.hakim
Jul 3 at 6:05
where do I put:
customersRecyclerView.Visibility = ViewStates.Visible; customerDataContainer.Visibility = ViewStates.Gone;
using FocusChangeListenerClass
– ProfileForStack4
Jul 3 at 10:05
customersRecyclerView.Visibility = ViewStates.Visible; customerDataContainer.Visibility = ViewStates.Gone;
FocusChangeListenerClass
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
i would rather ask you to use the focus event
– G.hakim
Jun 29 at 11:43