PKAP
PKAP

Reputation: 735

Program should wait after Textfield changed its text

My Testprogram should change a TextViews text and after it is done, it should wait on second before the next text change. However my program runs the text changes instant behind each other:

    t.setText("Test!"); 
    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        t.setText("Test - after 1 second!");    
        }
    },1000);

The first text is not even there close to a second.

Upvotes: 0

Views: 79

Answers (2)

Jawascript
Jawascript

Reputation: 703

Hard to say why that wouldn't work from that limited amount of code but you can just add the post delayed to your View. You don't need a handler.

t.setText("Test!");
t.postDelayed(new Runnable() {
    @Override
    public void run() {
       t.setText("Test - after 1 second!");
    }
}, 1000);

All Views in Android have a built in handler class.

Upvotes: 2

Nabin
Nabin

Reputation: 11776

You are missing the following:

handler.postDelayed(this, 1000);

inside the run method. Here this will refer to the handler object

Upvotes: 1

Related Questions