I have to restrict users from typing Special Characters in the Search view.
I have already defined the input type as textPersonName, but the users should not be able to type any Special Characters in the first place.
When I tried to cast it to an EditText. It throws an exception, as below:
val filter: InputFilter = object : InputFilter {
override fun filter(
source: CharSequence,
start: Int,
end: Int,
dest: Spanned?,
dstart: Int,
dend: Int
): CharSequence? {
for (i in start until end) {
if (!Character.isLetterOrDigit(source[i])) {
return ""
}
}
return null
}
}
var etSearch = searchview_filter as EditText
etSearch.filters = arrayOf(filter)
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.sbi.investtap, PID: 12227
java.lang.ClassCastException: androidx.appcompat.widget.SearchView cannot be cast to android.widget.EditText