aurelianr
aurelianr

Reputation: 549

android textchangelistener is not called

I have the following issue:

Inside a fragment let say FragmentTest, i am having the following thread:

new Thread(new Runnable() {
    public void run() {
    ...
        if (condition) {
             if (isAdded()) {
        getActivity.runOnUiThread(new Runnable () {
    public void run() {           
             rightEditText.addTextChangedListener(FragmentTest.this);} } 
          }
        }
       }
    ...
    }
}).start;

inside of onCreate method.

The FragmentTest implements the TextWatcher interface but none of the interface method is called.

Upvotes: 1

Views: 257

Answers (2)

Syed
Syed

Reputation: 340

public class MainActivity extends AppCompatActivity implements TextWatcher {
EditText edttext;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
   
    edttext = (EditText) findViewById(R.id.editText);
    edttext.addTextChangedListener(this);
}


@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
  runOnUiThread(new Runnable() {
      @Override
      public void run() {
         Toast.makeText(MainActivity.this,"change",Toast.LENGTH_SHORT).show();
      }
  });
}

@Override
public void afterTextChanged(Editable editable) {

}

}

Upvotes: 1

Vucko
Vucko

Reputation: 7479

Migrating the comment to the answer:

Why do you need to do this inside a separate thread? I think the problem is that all the UI components run on the main UI thread. Try it without the thread.

I guess no further explanation is needed here. OP migrated his code to the UI thread and it worked.

Upvotes: 1

Related Questions