Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Respond Keyboard while Updating UI

I am Using android.support.v7.widget.SearchView and I am performing Search on enter of every character in SearchView. following is the snippet.

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener()
            {

                @Override
                public boolean onQueryTextSubmit(String query)
                {
                    return false;
                }

                @Override
                public boolean onQueryTextChange(String newText)
                {

                    if (!newText.trim().equals(""))
                    {
                     displayResult(newText);
                    return false;
                   }
              }
            });

And I am updating the UI When I am Getting Result, So In My Case It will call Search API every time when user enter a single character. So Suppose user wants to Search "Australia" and try to enter whole word but My UI is Updating with Search and User has just type "Aus" after the Keyboard is not responding because it's updating the UI.

So I want Just want to respond Keyboard while Updating the UI.
Hope I'll get fast response.

like image 769
GreenROBO Avatar asked Jun 09 '16 08:06

GreenROBO


3 Answers

The scenario of your case is:

  1. Input text

  2. Perform search

  3. Display results

  4. Do again

The lagging happened because of (2) + (3) cost much time to do.

Try to send the search key from (1) to worker thread (AsyncTask, Thread, IntentService,...).

(2) will be execute on worker thread then push the results back to UI

(3) update the UI

To improve performance, I think you should perform search while user stop the input at period of time. In additional, stop unneccessary search if the input changed.

example: input1 > search1 > input2 > cancel old task1 > search2 > ...

Hope it help!

like image 91
Alex Hong Avatar answered Nov 07 '22 17:11

Alex Hong


The key to the problem is both updating UI operation with the result from Search API and responding to SearchView with Keyboard are do its job in Main(UI) Thread.

In a short, you let the main thread do too much thing in a short time.

So the solution is to try to LIMIT the frequency of updating UI operation. I meaning, you should let the displayResult(newText) method can CANCEL the previous call when the user continuously inputted word in a short time.

Just updating the UI with the result ONCE when the whole word Australia was inputted completely.

BTW, your search API call should do in the background thread with some wonderful library(Retrofit && OkHttp).

like image 28
JohnWatsonDev Avatar answered Nov 07 '22 19:11

JohnWatsonDev


There might be two things that you might be doing wrong. 1) Network request are performed from UI Thread 2) Parsing is done on UI Thread Judging from your stackoverflow points you might not be doing the first case ie; performing a network request on the UI Thread.

For Case 2: try to perform parsing on a background thread, try to shorten the json response (tell the author of the Search API to give a lightweight response). And for each query change cancel the previous task

like image 1
NIPHIN Avatar answered Nov 07 '22 17:11

NIPHIN