You can use a TextWatcher
to listen to text changes in an EditText
. This TextWatcher
subclass takes a TextView
and each time the String
in the EditText
changes it sets the new text to the TextView
.
public class EditTextWatcher implements TextWatcher {
private final TextView target;
private EditTextWatcher(TextView target) {
this.target = target;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
this.target.setText(s);
}
}
You would us it like this:
editText.addTextChangedListener(new EditTextWatcher(textView));
Of course you have to be careful with listeners like this. Don't forget to remove the TextWatcher
later if you have to!