Mueyiwa Moses Ikomi Mueyiwa Moses Ikomi - 3 months ago 8
Java Question

What is the equivalent of OnInputListener in Android

I have an editText in android and i'ld like to listen for when an input or text has been added.

i am setting the text dynamically and i want a situation where once that is done, a new event is triggered.

code.setText(savedcode);

code.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
Toast.makeText(Verification.this, "New Input" , Toast.LENGTH_SHORT ).show();
}

@Override
public void afterTextChanged(Editable editable) {

}
});


i've used the text watcher but it doesn't trigger event when input is set dynamically unless i edit the field then it does. i know in javascript, on input listener can do that so i'm looking for its android equivalent. Thank you

Answer

Well, the thing is that your code seems to be correct, the one thing that is done wrong is the order.

Practically,you want to know when the textView's text has changed,but you change it before adding a text to it.

So the logical way to go is this:

TextView TV;

TV = (TextView) rootView.findViewById(R.id.theID);

TV.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                Toast.makeText(Verification.this, "New Input" , Toast.LENGTH_SHORT ).show();
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

TV.setText(savedcode);

This way, you change the text once you actually have a listener there, not before.That should fix your problem.

P.S: I am not sure how you declare your textView, don't mind the variables name, no inspiration at this moment.

Comments