1

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.

aurelianr
  • 538
  • 2
  • 12
  • 35
  • 3
    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. – Vucko Jun 29 '16 at 15:12
  • 1
    maybe you should try calling the thread in `onCreateView()` or some other method after onCreateView(), if I'm not wrong the views will get inflated in onCreateView() method. – SripadRaj Jun 29 '16 at 15:14
  • i cannot try without thread. I will update the code : if (isAdded()) { getActivity.runonUiThread(){ ..... rightEditText.addTextChangedListener(FragmentTest.this);} } – aurelianr Jun 29 '16 at 15:21
  • 1
    @aurelianr always better to update code in main post.it makes much readable and sense. – Navoneel Talukdar Jun 29 '16 at 15:48
  • @Vucko can you post the your answer as reply to the post. because I want to set your anwer as correct answer. The code works properly after i updated it and made the addTextChangedListener call on the Ui Thread. – aurelianr Jun 29 '16 at 16:08
  • @aurelianr Yes, Thanks a lot for minding that. People rarely do! Answer coming! :) – Vucko Jun 29 '16 at 16:31

2 Answers2

1
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) {

}

}

Syed
  • 320
  • 3
  • 11
  • i cannot add the edittext listentener directly in onCreate method, because it depends on other operations that must be executed outside on the UI thread. – aurelianr Jun 30 '16 at 07:12
  • initialized it in onCreate method and add the listener any where you want – Syed Jun 30 '16 at 09:12
1

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.

Vucko
  • 7,371
  • 2
  • 27
  • 45