'when setText on editText TextWatcher.onTextChanged not called
Whenever EditText string is changed, onTextChanged is called.
Now when I start a new Activity and send data through Bundle, onTextChanged is not called.
if( getIntent().getExtras() != null) {
Bundle b = getIntent().getExtras();
int value = -1;
if(b != null)
value = b.getInt("key");
edit1.setText("Mywords:");
}
How can I call it ?
Solution 1:[1]
Make sure you are subscribing with TextWatcher before setText called.
Solution 2:[2]
For some reason, the method addTextChangedListener is not called for me.
However the method doBeforeTextChanged, doOnTextChanged and doAfterTextChanged worked perfectly :
amountTextField.doBeforeTextChanged { text, start, count, after ->
Log.d(TAG, "beforeTextChange.") // OK, called
}
amountTextField.doOnTextChanged { text, start, before, count ->
Log.d(TAG, "onTextChanged.") // OK, called
}
amountTextField.doAfterTextChanged {
Log.d(TAG, "afterTextChanged.") // OK, called
}
amountTextField.addTextChangedListener {
object: TextWatcher {
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
Log.d(TAG, "beforeTextChange.") // NOK, not called
}
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
Log.d(TAG, "onTextChanged.") // NOK, not called
}
override fun afterTextChanged(p0: Editable?) {
Log.d(TAG, "onTextChanged.") // NOK, not called
}
} }
By the way, the result was always the same : whether I set any text to my EidtText or not, whether after or before I set any text.
Solution 3:[3]
Try the following:
edit1.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) {
}
});
Reference: android on Text Change Listener
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Reaz Murshed |
| Solution 2 | Jérémy Valensi |
| Solution 3 | Community |
