1

I mean is like stopwach, when button clicked the Timer is on until the stop button pressed

startbutton= (Button)findViewById(R.id.black1);
  startbutton.setOnClickListener(new View.OnClickListener() {

              public void onClick(View v){
              //Start the timer
                                         }                
            });

 stopbutton= (Button)findViewById(R.id.black1);
 stopbutton.setOnClickListener(new View.OnClickListener() {

              public void onClick(View v){
              //Stop the timer
                                         }                
            });

and second question,

if timer show 90 second how to make it show imageview or button in screen? like some if statement to make button visible each timer count to 90 second (90, 180, 270, etc..) he will set button visibility to visible.

Thanks before.

RichFounders
  • 169
  • 21
  • please check out the following link:http://examples.javacodegeeks.com/android/core/os/handler/android-timer-example/ – Rajan Bhavsar Aug 31 '15 at 05:49
  • http://developer.android.com/reference/java/util/Timer.html – Zarwan Aug 31 '15 at 05:50
  • Thanks for give some link guy's it helped but still not answer my second question in there – RichFounders Aug 31 '15 at 05:52
  • Check out : http://developer.android.com/intl/zh-tw/reference/android/widget/Chronometer.OnChronometerTickListener.html or http://stackoverflow.com/questions/526524/android-get-time-of-chronometer-widget – Haresh Chhelana Aug 31 '15 at 07:20

2 Answers2

5

Use chronometer in your xml

<Chronometer
        android:id="@+id/chronometer1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Chronometer" />

in your java

Chronometer focus = (Chronometer) findViewById(R.id.chronometer1);

startButton.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {

        focus.start();
        setVisibilityTimerOn(); //Second Question Solution
    }
});

stopButton.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {

        focus.stop();
        setVisibilityTimerOff();
    }
});

Second Question If you want to turn VISIBILITY on/off of some button/ImageView set up a handler

//Declare these variable 
private Handler handler;
private Runnable updateView;


private void setVisibilityTimerOn(){
     timeHandler = new Handler(); //it's better if you declare this line in onCreate (becuase if user press stopButton first before pressing startButton error will occur as handler was never initialized and you try calling removeCallback function)
     updateView = new Runnable() {
         public void run() {
              someImageView.setVisibility(View.VISIBLE);
         }
     };
     handler.postDelayed(updateView ,90000);//this will be on after 90 second
}

private void setVisibilityTimerOff(){
    handler.removeCallbacks(updateView);
}
Sanjeev
  • 4,255
  • 3
  • 28
  • 37
0

This works like stop watch timer with the interval of 1ms.

    Handler handler= new Handler();

1. click to start   (code)


  Runnable runnable = new Runnable() {
                       @Override
                       public void run() {
        //put your code to be executed on within every interval
                           handler.postDelayed(this, 1); 
                       }
                    };

        handler.postDelayed(runnable, 5); //start after 5 seconds 

 2. click to stop (code)
     handler.removeCallbacksAndMessages(null); 

It should work. Thanks

Androider
  • 3,833
  • 2
  • 14
  • 24