Search code examples
androidkotlinandroid-edittextandroid-textinputedittext

Changing the text in addTextChangedListener listener of edit text in android gives error


  • Is it not possible to change the text of EditText in afterTextChanged
  • It gives the error below
  • Is there a specific way to change the text

code:

inputEmail.addTextChangedListener(object :TextWatcher{
            override fun afterTextChanged(s: Editable?) {
                inputEmail.setText("123456")
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }

        })

Error:

at com.caring2u.organizer.ui.fragments.screen.FrgBookingDetails$setEditTextListeners$4.afterTextChanged(FrgBookingDetails.kt:256)
        at android.widget.TextView.sendAfterTextChanged(TextView.java:9605)
        at android.widget.TextView.setText(TextView.java:5496)
        at android.widget.TextView.setText(TextView.java:5343)
        at android.widget.EditText.setText(EditText.java:113)
        at android.widget.TextView.setText(TextView.java:5300)

Solution

  • Changing EditText in TextWatcher is not good idea at all.

    When you setting text to editText your TextWatcher will call again .

    Note:- And it will goes in infinite loop.

    Solution for this:-

    Use string value as a result in TextWacher and change your ExitText outside text-watcher.

    Using TextView you can do like this .....

    Create TextView in you layout like below.

    <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
    
            <EditText
                android:id="@+id/edit_phone"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/border_edittext"
                android:gravity="center"
                android:inputType="number"
                android:padding="10dp"
                android:singleLine="true" />
    
    
    
                <TextView
                    android:id="@+id/text_phone"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@drawable/border_edittext"
                    android:gravity="center"
                    android:inputType="number"
                    android:padding="10dp"
                    android:singleLine="true" />
    
    
    
        </FrameLayout>
    

    and use textWatcher and Update your TextView text inside onTextChange() method

    Another Solution :- Create XMl like this

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_height="wrap_content">
    
        <EditText
            android:id="@+id/code"
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:background="@drawable/border_edittext"
            android:gravity="center"
            android:inputType="number"
            android:padding="10dp"
            android:text=""
            android:singleLine="true" />
    
        <EditText
            android:id="@+id/mobile"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/border_edittext"
            android:gravity="center"
            android:inputType="number"
            android:padding="10dp"
            android:singleLine="true" />
    
    
    </LinearLayout>
    

    and update your editText with id code in your textWatcher of your editText with id mobile.