-1

I am setting the text in EditText but when getting the text from TextWatcher it is giving empty string.

Please check this code

for (int i = 0; i < 4; i++) {
   et = new EditText(this);
   et.setText("hai");
   final EditText finalEt = et;
   ans.addView(et);
   final int finalI = i;
   finalEt.addTextChangedListener(new TextWatcher() {
       public void afterTextChanged(Editable s) {
           Log.d("text is",""+finalEt.getText().toString());
       }

       public void beforeTextChanged(CharSequence s, int start,int count, int after) {}

       public void onTextChanged(CharSequence s, int start,int before, int count) {}
   }
Rohit5k2
  • 17,948
  • 8
  • 45
  • 57

2 Answers2

0

You will get it in onTextChanged as well as on afterTextChanged something like this.

for (int i = 0; i < 4; i++) {
et = new EditText(this);
et.setText("hai");

final EditText finalEt = et;

ans.addView(et);
final int finalI = i;
finalEt.addTextChangedListener(new TextWatcher() {
   public void afterTextChanged(Editable s) {
                   String str = s.toString();
           Log.d("text is",""+s);

   }

   public void beforeTextChanged(CharSequence s, int start,int count, int after) {}

   public void onTextChanged(CharSequence s, int start,int before, int count) {
           Log.d("text is",""+s.toString());
   }
}
Beena
  • 2,334
  • 24
  • 50
  • thanks for the answer but if i want to get only "hai" from the Log.d – Hari Babu Koduri Feb 20 '16 at 05:46
  • Sorry but not getting you. AT this moment what do you get? `s.toString()` will return you text you have entered in `edittext.` – Beena Feb 20 '16 at 05:52
  • it is displaying hai+data entered in edittext but i need only the "hai" which is set from et.setText("hai") from the log message – Hari Babu Koduri Feb 20 '16 at 07:16
  • If you just want to get the text you entered previously then what is the use of using `TextWatcher `? And If you want to get text before you started entering then you can get it on `beforeTextChanged ` method by ` String str = s.toString();` – Beena Feb 20 '16 at 07:27
  • actually in my app an activity has four edittexts to collect user info ,initially they are empty but after filled-in by user ,his info will be saved and he goes to next activity and if he wants to change the info. he come back to the activity since the info is already filled-in i will set those data in the edittext but if he changes only one out of four , the changed data in the edittext is saving but unchanged edittext is not saving because textWatcher is not being called – Hari Babu Koduri Feb 20 '16 at 08:25
0

Replace log with this

Log.d("text is","" + new String(s.toString()));
Rohit5k2
  • 17,948
  • 8
  • 45
  • 57