hi , i need one help can anyone tell how to restri...
# android
s
hi , i need one help can anyone tell how to restrict specific characters in searchView
stackoverflow 11
s
Add textwatch listener and in afterTextChange method iterate over the Editable to find if the input contains your specific character and if found use replace method like(input.replace("specific_character","") and set that filtered input as text again
Copy code
editText.addTextChangedListener { e  ->
    If(e?.any{it.contains("specific_character"){
     val filteredInput = it.toString().replace("specific_character","")
    editText?.setText(filteredInput)
    editText?.setSelection(filteredInput.length)
}
}
s
i don't want edit text
s
But logic is same just use this with searchview
Though i guess for searchview you have to reference its internal edittext
s
in search view you can't set filters
you can only replace the string on query text change method by matching the patterns
s
You can access searchviews internal textview that shows query using searchView?.findViewById(androidx.appcompat.R.id.search_src_text)
👍 1
s
ok but there is any listeners for textView?
s
val tvQuery = searchView?.findViewById(androidx.appcompat.R.id.search_src_text)
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener() {
fun onQueryTextSubmit(query: String?): Boolean {
return false
}
fun onQueryTextChange(newText: String?): Boolean {
if(newText?.any{it.contains("specific_character")}){
val filteredQuery = newText?.replace("specific_character","")
tvQuery.text = filteredQuery
}
return false
}
})
And use androidx.appcompat version of SearchView in import
s
🙂 👍🏻
s
Do let me know if works or not
s
sure