I have two edittext fields in a layout. When i type something in edittext1, the same text should get filled up in edittext2 dynamically and vice versa. How can i do that??
Asked
Active
Viewed 83 times
-4
-
Give a look to this http://developer.android.com/reference/android/text/TextWatcher.html – Jeremy D Oct 14 '13 at 07:33
-
Check this http://stackoverflow.com/questions/4310525/android-on-edittext-changed-listener, maybe is what you need. – maketest Oct 14 '13 at 07:34
2 Answers
3
You add TextWatcher to both the Edittext as follows
EditText myTextBox = (EditText) findViewById(R.id.myTextBox);
EditText myOutputBox = (EditText) findViewById(R.id.myOutputBox);
myTextBox.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
public void onTextChanged(CharSequence s, int start,
int before, int count) {
myOutputBox.setText(s);
}
});
Similarly for other Edittext.
myOutputBox.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
public void onTextChanged(CharSequence s, int start,
int before, int count) {
myTextBox.setText(s);
}
});

CRUSADER
- 5,486
- 3
- 28
- 64
1
Take a look at EditText#addTextChangedListener. All you need to do is update the desired EditText
inside afterTextChanged
method.

ssantos
- 16,001
- 7
- 50
- 70