Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show button with delay Android Java

I want create some simple animation for my program. There is 4 invisible button, and that I want is when program is started, those button will visible with delay.

Show button 1 -> button 2 -> and so on.

I tried this, but when program running, all button will appear in same time.

try {
((Button) findViewById(R.id.f1)).setVisibility(View.VISIBLE);
    Thread.sleep(1200);
((Button) findViewById(R.id.f2)).setVisibility(View.VISIBLE);
    Thread.sleep(1200);
((Button) findViewById(R.id.f3)).setVisibility(View.VISIBLE);
    Thread.sleep(1200);
((Button) findViewById(R.id.f4)).setVisibility(View.VISIBLE);
    Thread.sleep(1200);
 } catch (Exception e) {
 }

anyone can help me ?

like image 293
felangga Avatar asked Jul 01 '12 12:07

felangga


1 Answers

Use a Handler:

private Handler handler;

private void showButtons(){
    handler = new Handler();

handler.postDelayed(new Runnable(){
    @Override
    public void run(){
        ((Button) findViewById(R.id.f1)).setVisibility(View.VISIBLE);
    }
}, 1200);

handler.postDelayed(new Runnable(){
    @Override
    public void run(){
        ((Button) findViewById(R.id.f2)).setVisibility(View.VISIBLE);
    }
}, 2400);

//etc
}
like image 63
nhaarman Avatar answered Oct 06 '22 00:10

nhaarman