Mohsen Bahman
Mohsen Bahman

Reputation: 1092

How to refresh a view content?

I wanna create a program has two Buttons named button1 and button2.This is block of button1 onClick method :

    public void click1(View v){
        Button b = (Button)findViewById(R.id.button2);
        b.setText("TEXT 1");
        SystemClock.sleep(500);
        b.setText("TEXT 2");

    }

but the problem is after first change of object 'b' text to "TEXT 1" , it occurs nothing and after 500 ms text of 'b' changes to "TEXT 2".

What's the problem?

How to refresh Layout views contents?

Upvotes: 0

Views: 90

Answers (1)

user2652394
user2652394

Reputation: 1686

Instead of using SystemClock.sleep(500);, please you Handler, you can try this code:

public void click1(View v){
                Button b = (Button)findViewById(R.id.button2);
                b.setText("TEXT 1");
                new Handler().postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        b.setText("TEXT 2");    
                    }
                }, 500;)
                //b.forceLayout();
            }

Hope this helps.

Upvotes: 1

Related Questions