2

Can it possible to make a button on screen which automatically visible and gone every 5sec interval? By using this

b.setVisibility(View.VISIBLE);

we can visible and

b.setVisibility(View.GONE); 

we can hide it.But I can't manage to make it by usin the time interval. Any idea?please share.

MBMJ
  • 5,323
  • 8
  • 32
  • 51
  • 3
    Use a `Handler.postDelayed()` call, such as is described in [this question](http://stackoverflow.com/questions/10845172/android-running-a-method-periodically-using-postdelayed-call). – Rob I Aug 30 '12 at 04:26
  • Better create Service and run your stuff there. – Praveenkumar Aug 30 '12 at 04:28

2 Answers2

4

There are a few different ways, one is a Handler and Runnable:

public class Example extends Activity {
    private Handler mHandler = new Handler();
    private Runnable alternate = new Runnable() {
        public void run() {
            // Alternate visible and not
            mHandler.postDelayed(alternate, 5000);
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mHandler.postDelayed(alternate, 5000);
    }
}
Sam
  • 86,580
  • 20
  • 181
  • 179
2

Use this

new CountDownTimer(9000000, 5000) {

 public void onTick(long millisUntilFinished) {
     if(b.getVisibility() == View.GONE)
      b.setVisibility(View.VISIBLE);
     else
      b.setVisibility(View.GONE);
 }

 public void onFinish() {
   //Restart timer if you want.
 }
}.start();
nandeesh
  • 24,740
  • 6
  • 69
  • 79