How to handle search API call on text change
<p>In this article, we will learn how we can implement search efficiently in Android. Whenever user search for something in your app, we show a list of suggestions they might be searching for on each txt change.</p>
<p>Let’s take an example:</p>
<p><img alt="" src="https://miro.medium.com/v2/resize:fit:270/1*LcOoRKGEFieKpC_NUHJ4JA.gif" style="height:600px; width:270px" /></p>
<p>In the medium app when we are searching for people, on each text change we see a respective result.</p>
<p>The code looks something like this:</p>
<pre>
searchEditText.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(editable: Editable) {
// api call to send search text , and get respective result
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
})</pre>
<p>Here in this code whenever the user types something in the search bar, we get a callback in text watcher and we make api call.</p>
<p><a href="https://levelup.gitconnected.com/how-to-handle-search-api-call-on-text-change-42f4fffe0baa">Visit Now</a></p>