1
  • 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)
Prags
  • 2,457
  • 2
  • 21
  • 38
Devrath
  • 42,072
  • 54
  • 195
  • 297

1 Answers1

4

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.

sushildlh
  • 8,986
  • 4
  • 33
  • 77
  • This seems true to me, not sure why this answer is getting negative votes. – TFennis Jan 08 '19 at 06:43
  • Thats Exactly what is happening, But what is a workaround for this or is it not possible ? – Devrath Jan 08 '19 at 06:44
  • If you're always setting your text to the same value, you could check in your text watcher to not set it if it's already at the correct value – TFennis Jan 08 '19 at 06:44
  • @TFennis Some people are here to demotivate people. Who just down-vote and never mention why they did. We will not do anything in it. – sushildlh Jan 08 '19 at 06:45
  • @Devrath what you trying to achieve? Please share may we can help in it. – sushildlh Jan 08 '19 at 06:49
  • @SushilKumar .... I am changing a value in edit text, It is a phone no field .... Once someone modifies it, i need to append it with `+70` code along with the value user entered – Devrath Jan 08 '19 at 07:00
  • try this one https://stackoverflow.com/a/20824665/5305430 or i have another one using TextView – sushildlh Jan 08 '19 at 07:04