1

I know how to set error on EditText like this:

 serverIp.setError("Server IP Is Required");
        serverIp.requestFocus();
        return;

But in SearchView there is no option to set Error.

Is there any way to get the result?

ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108
saurabh yadav
  • 567
  • 6
  • 14

1 Answers1

0

But in SearchView there is no option to set Error.

Indeed. There is no public method (I couldn't find any at least) related to setError() in SearchView since it extends from LinearLayoutCompat and not the EditText to use setError().

Instead, you can use an EditText inside a CardView to handle the setError().


Also, check this out: https://stackoverflow.com/a/47573025/4409113

private fun setErrorOnSearchView(searchView: SearchView, errorMessage: String) {
    val id = searchView.context
            .resources
            .getIdentifier("android:id/search_src_text", null, null)
    val editText = searchView.find<EditText>(id)
    editText.error = errorMessage
}

You might be able to declare-bypass the SearchView as an EditText to use setError().

ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108