How to use a SearchView with an empty query text submit
Tags: android, development, appdev, beginners
Published: 2018-01-16 20:58:54

---
title: How to use a SearchView with an empty query text submit
published: true
description: How to use a SearchView with an empty query text submit
tags: android, development, appdev, beginners
---

At the moment i’m working on an App with a search function. This alone is nothing special. Every second App has a search function. As a standard component the Android SDK offers the class [SearchView](https://developer.android.com/reference/android/widget/SearchView.html). You can add an [SearchView.OnQueryTextListener](https://developer.android.com/reference/android/widget/SearchView.OnQueryTextListener.html) to it.


![SearchView](https://cdn-images-1.medium.com/max/1067/1*IljK3KTqvAyChLe6MsX9Zg.jpeg)

{% gist https://gist.github.com/Foso/e308760b0f3169f6b584aca7e6aa0de3 %}

![SearchView.OnQueryTextListener()](https://cdn-images-1.medium.com/max/1067/1*1o6P7EWI1blhjMVVLa23aQ.png)


###Where is the problem?
The App has a function that gets called when the user presses the search button on the keyboard and there is no text in the SearchView. But this is not possible with the standard SearchView.

![Keyboard](https://cdn-images-1.medium.com/max/1067/1*4T6_bAFqTLefmpBZjQTf3g.jpeg)

###Why?
**onQueryTextSubmit()** gets not called when the query is empty. A look in the source code tells us why:

{% gist https://gist.github.com/Foso/a55e3882363267eb7984a99a5d78a5df %}

The listener gets called in **onSubmitQuery()** of SearchView, but the if-statement checks before if the query length is longer than zero.

{% gist https://gist.github.com/Foso/c4363792c27024c3112e5d4e32e4465e %}

###How does the SearchView detect when the search button was pressed?
When you take a look which methods call **onSubmitQuery()** you can see that it gets called inside an **OnEditorActionListener**.

{% gist https://gist.github.com/Foso/d000af975f7205c69f38e6dd312e76cf %}

And this listener is added to the SearchAutoComplete class:


{% gist https://gist.github.com/Foso/584d46823c66446a1b8e09451078a3c5 %}

SearchAutoComplete informs SearchView with this listener about the key press. Unfortunately SearchView doesn’t offer a possibility to replace SearchAutoComplete with an custom implementation. But let’s take a look in the constructor of SearchView to see how SearchAutoComplete gets created:

{% gist https://gist.github.com/Foso/fb85d55c7d90ba44ad6992e44f357156 %}

###What now?

{% gist https://gist.github.com/Foso/897bdd09db31f54afdce2eab6070036f %}

SearchView is nothing more than an extended LinearLayout. With findViewById the SearchAutoComplete inside the layout gets inflated. So it should be possible to also inflate the layout to get at the SearchAutoComplete.

I created a class that extends SearchView. Then i’ve overridden the **setOnQueryTextListener()** method. Since the listener already has the methods i need and I don’t have to make special changes to my existing code.

{% gist https://gist.github.com/Foso/9a92b2c8e050ea973710ec4fcad68074 %}

The listener gets passed to the original SearchView, but in this method I also set a listener to the SearchAutoComplete as explained above. Now every time the user clicks the search button I get the query from the SearchView and pass it to the listener. Except that **onQueryTextSubmit()** will also get called when the query is empty.




{% gist https://gist.github.com/Foso/d77e1851b01d7a6b853533d929fef088 %}



[Source Code](https://github.com/Foso/Notes/blob/master/Android/EmptySubmitSearchView.java)