I select text syntax in Android Studio, for this I need a timer that will call the syntax check method every 5 seconds, in turn, this method gets the EditText itself and processes it. Tried through ScheduledExecutorService , but for some reason the program freezes during the execution of the method. Here is the code, as I did in a separate thread:

 class synLight implements Runnable { private EditText editText; public synLight(EditText editText){ this.editText = editText; } @Override public void run() throws RuntimeException{ MainActivity.editText.setTextColor(Color.RED); } } 

    1 answer 1

    Use Handler

     int interval = 5000; Handler handler = new Handler(); handler.post(new Runnable() { @Override public void run() { // Необходимые действия handler.postDelayed(this, interval); } }); 
    • I have a method that I need to perform after a while takes EditText as input, what then? - Alexandr Larin
    • I do not see the problem, anonymous classes can use variables in the ambient scope. - Sergey Gornostaev pm
    • The text for some reason does not change (only checked once, but does not want to continue. - Alexandr Larin
    • If there are no errors, but the program does not work as it should, then you have to use the debugger . - Sergey Gornostaev
    • thank you) should handler and interval be final? Just the compiler gave an error. - Alexandr Larin 5:03 pm