locked
SearchView QueryTextSubmit event with empty query (need help to port workaround to Xamarin) RRS feed

  • Question

  • User151039 posted

    Hi,

    I want the SearchView to fire the QueryTextSubmit Event when the query Text is empty to reset the search result.

    The problem is that the implementation of the SearchView doesn't fire the event when the text ist empty.

    I found a workaround by deriving the control and override the QueryTextListener but unfortunately its in Java and I was not able to port it to C# as some sub-classes of the SearchView don't seem to be available.

    http://jensklingenberg.de/how-to-use-a-searchview-with-an-empty-query-text-submit/

    Can anyone help me with implementing that in C#?

    Or has another good idea how to get notified when the Searchbutton on Keyboard is pressed while the Searchview Query is empty?

    Thanks and Kind regards

    Friday, August 24, 2018 10:45 AM

All replies

  • User367416 posted

    @Kornelis As we are in Xamarin, we can write custom renderer for Android search view and implement java code in C#. it would be simple, just to override the method of search view in Android class.

    Friday, August 24, 2018 11:39 AM
  • User99156 posted

    @Kornelis

    Please try this:

    ```csharp // Created by: Jens Klingenberg (jensklingenberg.de) // GPLv3 // This SearchView gets triggered even when the query submit is empty

    using Android.Content; using Android.Util; using SearchView = Android.Support.V7.Widget.SearchView;

    namespace Test { public class EmptySubmitSearchView : SearchView { private SearchView.SearchAutoComplete _searchSrcTextView; private IOnQueryTextListener _listener;

        public EmptySubmitSearchView(Context context)
            : base(context)
        {
        }
    
        public EmptySubmitSearchView(Context context, IAttributeSet attrs)
            : base(context, attrs)
        {
        }
    
        public EmptySubmitSearchView(Context context, IAttributeSet attrs, int defStyleAttr)
            : base(context, attrs, defStyleAttr)
        {
        }
    
        public override void SetOnQueryTextListener(IOnQueryTextListener listener)
        {
            base.SetOnQueryTextListener(listener);
            _listener = listener;
            _searchSrcTextView = FindViewById<SearchAutoComplete>(Resource.Id.search_src_text);
            _searchSrcTextView.EditorAction += (s, e) =>
            {
                listener?.OnQueryTextSubmit(Query);
                e.Handled = true;
            };
        }
    }
    

    } ```

    Tuesday, August 28, 2018 7:50 AM
  • User151039 posted

    @RofiqSetiawan

    Unfortunatetly this doesn't work

    using SearchView = Android.Support.V7.Widget.SearchView; causes an error.

    SearchView isn't available in Android.Support.V7. And there is no NuGet Package available for that.

    Tuesday, August 28, 2018 11:10 AM
  • User99156 posted

    It's available on Xamarin.Android.Support.v7.AppCompat nuget https://www.nuget.org/packages/Xamarin.Android.Support.v7.AppCompat/

    Tuesday, August 28, 2018 11:12 AM
  • User151039 posted

    Thanks for the Hint, but it still doesn't work.

    I get it compiling but at runtime i get

    Android.Views.InflateException: Binary XML file line #1: Binary XML file line #1: Error inflating class md5b89dc1b20ff86aa2d4a582d2d9e70a10.EmptySubmitSearchView occurred when trying to add via axml layout and

    Android.Views.InflateException: Binary XML file line #18: Binary XML file line #18: Error inflating class <unknown> when trying to add as Subview into a FrameLayout

    Tuesday, August 28, 2018 11:50 AM
  • User99156 posted

    Can you show me your current layout?

    Tuesday, August 28, 2018 12:14 PM
  • User376919 posted

    What is this



    Tuesday, August 28, 2018 1:48 PM