'how to set one textwatcher for dynamic more than Edittext in android

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



Solution 1:[1]

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

Solution 2:[2]

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);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sumit Ojha
Solution 2 Rajen Raiyarela