I Want to set one textwatcher for dynamic more than Edittext .if I am Creating Dynamic Edittext I want to use One textWatcher for all Dynamic Edittext.if anybody know tell me
Asked
Active
Viewed 1,184 times
-1
-
1Did you try searching for it before posting the question? Check [this question](http://stackoverflow.com/questions/5702771/how-to-use-single-textwatcher-for-multiple-edittexts) or [this](http://stackoverflow.com/questions/4283062/textwatcher-for-more-than-one-edittext) – Rehan May 30 '16 at 08:09
-
I am Asking For Dynamic Edittext not Static – Selva Meena May 30 '16 at 09:15
-
You need to read [this](http://stackoverflow.com/help/how-to-ask) – Rehan May 30 '16 at 11:23
2 Answers
3
You can create custom Text Watcher class for Edit Text
class DynamicTextWatcher(private val editText: EditText, private val type: String) :
TextWatcher {
var beforeText : String = ""
override fun beforeTextChanged(p0: CharSequence?, start: Int, before: Int, count: Int) {
beforeText = p0.toString()
}
override fun onTextChanged(p0: CharSequence?, start: Int, before: Int, count: Int) {
if (p0 == null)
return
// 1. get cursor position : p0 = start + before
val initialCursorPosition = start + before
//2. get digit count after cursor position : c0
val numOfDigitsToRightOfCursor = getNumberOfDigits(beforeText.substring(initialCursorPosition,
beforeText.length))
try {
editText.removeTextChangedListener(this)
val value = editText.text.toString().trimStart(' ')
if (value != "") {
when(type){
Constants.FORM_INPUT_NAME -> {
//editText.setText(value.replaceFirstChar { it.uppercase() })
val fc = value[0]
if (fc == ' '){
editText.setText(value.drop(1))
}else{
if (value.contains(" ")){
editText.setText(value.replace(" ", " "))
}
else {
editText.setText(value)
}
}
}
Constants.FORM_INPUT_NO_SPACE -> editText.setText(value.removeWhitespaces())
Constants.FORM_INPUT_MOBILE -> {
val cs = value.removeWhitespaces()
val cleanString = cs.replace("[^0-9]".toRegex(), "")
editText.setText(cleanString)
}
//Constants.FORM_INPUT_PASSWORD -> editText.setText(value.removeWhitespaces())
}
}else{
editText.setText(value)
}
editText.setSelection(getNewCursorPosition(numOfDigitsToRightOfCursor, value,type))
editText.addTextChangedListener(this)
} catch (ex: Exception) {
ex.printStackTrace()
editText.addTextChangedListener(this)
}
}
override fun afterTextChanged(s: Editable) {
}
private fun getNewCursorPosition(digitCountToRightOfCursor : Int, numberString : String, type : String) : Int{
Log.v("getNewCursorPosition","hello"+numberString+"hello")
var position = 0
var c = digitCountToRightOfCursor
var value = numberString
when(type){
Constants.FORM_INPUT_NAME -> {
if (numberString.contains(" ")){
value = numberString.replace(" ", " ")
}
}
Constants.FORM_INPUT_NO_SPACE -> value = numberString.removeWhitespaces()
Constants.FORM_INPUT_MOBILE -> { }
}
for (i in value.reversed()) {
if (c == 0)
break
else
c --
position ++
}
return value.length - position
}
private fun getNumberOfDigits(@NonNull text : String) : Int{
var count = 0
for (i in text)
count++
return count
}
}
This code in your activity
registerSheetBinding.etPassword.addTextChangedListener(
DynamicTextWatcher(
registerSheetBinding.etPassword,
Constants.FORM_INPUT_NO_SPACE
)
)
registerSheetBinding.etEmail.addTextChangedListener(
DynamicTextWatcher(
registerSheetBinding.etEmail,
Constants.FORM_INPUT_NO_SPACE
)
)
registerSheetBinding.etFullName.addTextChangedListener(
DynamicTextWatcher(
registerSheetBinding.etFullName,
Constants.FORM_INPUT_NAME
)
)
I hope this code helpful for you. #Android developer

Sumit Ojha
- 283
- 2
- 8
1
You can create custom TextWatcher class as below.
private class CustomTextWatcher implements TextWatcher{
private EditText et;
private CustomTextWatcher (EditText et){
this.et = et;
}
@Override
public void afterTextChanged(Editable s) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
}
For setting up textwatcher you can do as below
yourdynamicedittext.addTextChangedListener(new CustomTextWatcher(yourdynamicedittext));
Inside CustomTextWatcher class you can handle the relative case for each edittext you assign TextChangedListener. For identifying between different edittext you can set tag and retrieve it inside CustomTextWatcher class.
yourdynamicedittext.setTag(1, 2, ... n);

Rajen Raiyarela
- 5,526
- 4
- 21
- 41
-
ok fine i put this all finally.Now i am running why the error is coming on this line? et.setText(s); The error is : 05-30 16:20:55.623: E/AndroidRuntime(28054): at com.example.samptextwatcher.CustomTextWatcher.afterTextChanged(CustomTextW – Selva Meena May 30 '16 at 10:49
-