Hi I have a button and a timer
I want to run a timer but when I click a button the timer should stop and the method test() should be called.
This is my code:
public class MainActivity extends AppCompatActivity {
Button button;
int a=1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
a=6;
}
});
new CountDownTimer(30000, 4000) {
#Override
public void onTick(long millisUntilFinished) {
if (a<5)
{
Toast.makeText(MainActivity.this, ""+a, Toast.LENGTH_SHORT).show();
}
}
#Override
public void onFinish() {
test();
}
}.start();
}
private void test ()
{
Toast.makeText(this, "test is run", Toast.LENGTH_SHORT).show();
}
}
You have to define it first:
CountDownTimer yourCountDownTimer = new CountDownTimer(30000, 4000) {
public void onTick(long millisUntilFinished) {
if (a<5)
{
Toast.makeText(MainActivity.this, ""+a, Toast.LENGTH_SHORT).show();
}
}
public void onFinish() {
test();
}
}.start();
yourCountDownTimer.cancel(); //To Stop Timer
using cancel key word
timer.cancel();
timer.purge();
private CountDownTimer timer;
timer=new CountDownTimer(30000, 4000) {
#Override
public void onTick(long millisUntilFinished) {
if (a<5)
{
Toast.makeText(MainActivity.this, ""+a, Toast.LENGTH_SHORT).show();
}
}
#Override
public void onFinish() {
test();
}
}.start();
call timer.cancel() on onClick() metod of your button Listener.
Related
Hi I am making sport countdown timer and I have pause button. To pause timer I am using :(code bellow)
but I don't know hot to resume it. And I don't want to put my countdown timer function in public void method.
mButtonStartPause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mTimerRunning) {
pauseTimer();
} else {
startTimer();
}
}
});
mButtonReset.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
resetTimer();
}
});
mCountDownTimer = new CountDownTimer(mTimeLeftInMillis, 1000) {
#Override
public void onTick(long millisUntilFinished) {
mTimeLeftInMillis = millisUntilFinished;
updateCountDownText();
}
#Override
public void onFinish() {
mTimerRunning = false;
mButtonStartPause.setText("Start");
mButtonStartPause.setVisibility(View.INVISIBLE);
mButtonReset.setVisibility(View.VISIBLE);
}
}.start();
updateCountDownText();
}
private void startTimer() {
mTimerRunning = true;
mButtonStartPause.setText("pause");
mButtonReset.setVisibility(View.INVISIBLE);
}
private void pauseTimer() {
mCountDownTimer.cancel();
mTimerRunning = false;
mButtonStartPause.setText("Start");
mButtonReset.setVisibility(View.VISIBLE);
}
private void resetTimer() {
mTimeLeftInMillis = START_TIME_IN_MILLIS;
updateCountDownText();
mButtonReset.setVisibility(View.INVISIBLE);
mButtonStartPause.setVisibility(View.VISIBLE);
}
}
I checked your restart function. And I think solution to restart is to cancel it and start it again. Try this out:
private void resetTimer() {
mCountDownTimer.cancel();
mCountDownTimer.start();
}
StartTimer() function Should call after button click event completed, but it is get executed before onclick event. ro how to stop timer before onclick event and start after onclick event.
How do I fix this?
btn1=(Button)findViewById(R.id.button);
btn2=(Button)findViewById(R.id.button2);
btn3=(Button)findViewById(R.id.button3);
timer = new Timer();
btn1.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
timer.cancel();
Toast.makeText(getApplicationContext(), "Button1", Toast.LENGTH_SHORT).show();
}
});
StartTimer();
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
timer.cancel();
Toast.makeText(getApplicationContext(), "Button2", Toast.LENGTH_SHORT).show();
}
});
StartTimer();
btn3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
timer.cancel();
Toast.makeText(getApplicationContext(), "Button3", Toast.LENGTH_SHORT).show();
}
});
}
public void StartTimer()
{
timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
Log.v("timer", "Timer running");
}
}, 0, 5000);
Toast.makeText(getApplicationContext(), "Timer", Toast.LENGTH_LONG).show();
}
protected void onResume() {
super.onResume();
StartTimer();
}
}
There is already built in Timer class in android. So you can do like that:
import java.util.Timer;
Timer timer = new Timer();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timer.schedule(new TimerTask() {
#Override
public void run() {
Log.v("timer", "Timer running");
Toast.makeText(getApplicationContext(), "TIMER HAS FINISHED", Toast.LENGTH_SHORT).show();
}
}, 0, 5000);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
timer.cancel();
Toast.makeText(getApplicationContext(), "Button1", Toast.LENGTH_SHORT).show();
}
});
StartTimer();
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
timer.cancel();
Toast.makeText(getApplicationContext(), "Button2", Toast.LENGTH_SHORT).show();
}
});
btn3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
timer.cancel();
Toast.makeText(getApplicationContext(), "Button3", Toast.LENGTH_SHORT).show();
}
});
So just start timer in onCreate, and stop it when button clicked.
I want to do app to show 3 elements (each 2 seconds) and each start showing after the before element. Here is code:
public class Remember extends Activity
{
TextView text;
ImageView element1, element2, element3, element4 ,element5, element6;
Button button1, button2, button3, button4, button5, button6;
int data, id;
Random random;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_remember);
random = new Random();
id=1;
text = (TextView)findViewById(R.id.text);
element1 = (ImageView)findViewById(R.id.element1);
element2 = (ImageView)findViewById(R.id.element2);
element3 = (ImageView)findViewById(R.id.element3);
element4 = (ImageView)findViewById(R.id.element4);
element5 = (ImageView)findViewById(R.id.element5);
element6 = (ImageView)findViewById(R.id.element6);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button4 = (Button) findViewById(R.id.button4);
button5 = (Button) findViewById(R.id.button5);
button6 = (Button) findViewById(R.id.button6);
element1.setVisibility(View.GONE);
element2.setVisibility(View.GONE);
element3.setVisibility(View.GONE);
element4.setVisibility(View.GONE);
element5.setVisibility(View.GONE);
element6.setVisibility(View.GONE);
data = random.nextInt(6)+1;
}
public void button1(View v)
{
gameStart();
}
public void gameStart()
{
do{
if (data==1)
{
CountDownTimer cdt = new CountDownTimer(2000, 1000)
{
#Override
public void onTick(long millisUntilFinished)
{
element1.setVisibility(View.VISIBLE);
}
#Override
public void onFinish()
{
element1.setVisibility(View.GONE);
}
}.start();
}
else if (data==2)
{
CountDownTimer cdt = new CountDownTimer(2000, 1000)
{
#Override
public void onTick(long millisUntilFinished)
{
element2.setVisibility(View.VISIBLE);
}
#Override
public void onFinish()
{
element2.setVisibility(View.GONE);
}
}.start();
}
else if (data==3)
{
CountDownTimer cdt = new CountDownTimer(2000, 1000)
{
#Override
public void onTick(long millisUntilFinished)
{
element3.setVisibility(View.VISIBLE);
}
#Override
public void onFinish()
{
element3.setVisibility(View.GONE);
}
}.start();
}
else if (data==4)
{
CountDownTimer cdt = new CountDownTimer(2000, 1000)
{
#Override
public void onTick(long millisUntilFinished)
{
element4.setVisibility(View.VISIBLE);
}
#Override
public void onFinish()
{
element4.setVisibility(View.GONE);
}
}.start();
}
else if (data==5)
{
CountDownTimer cdt = new CountDownTimer(2000, 1000)
{
#Override
public void onTick(long millisUntilFinished)
{
element5.setVisibility(View.VISIBLE);
}
#Override
public void onFinish()
{
element5.setVisibility(View.GONE);
}
}.start();
}
else if (data==6)
{
CountDownTimer cdt = new CountDownTimer(2000, 1000)
{
#Override
public void onTick(long millisUntilFinished)
{
element6.setVisibility(View.VISIBLE);
}
#Override
public void onFinish()
{
element6.setVisibility(View.GONE);
}
}.start();
}
id=id+1;
text.setText("cos " + id);
data = random.nextInt(6)+1;
try
{
Thread.sleep(2000);
} catch (InterruptedException e)
{
e.printStackTrace();
}
}while(id<=3);
}
}
But if I clicked start application start lagging and everything go to norm after time which all elements should be hide. If I delete Thread.sleep all elements show in the same time. What should I do?
Thread.sleep(2000); on the ui thread blocks the ui thread. You should never block the ui thread. Remove the sleep.
To create a delay use a Handler.
Read
http://developer.android.com/guide/components/processes-and-threads.html
I try to do app which show elements. Each element should start showing when the before element was hidden. Each element is showing 2 seconds.
Code:
public void gameStart()
{
do
{
data = random.nextInt(6) + 1;
if (data == 1)
{
CountDownTimer cdt = new CountDownTimer(2000, 1000)
{
#Override
public void onTick(long millisUntilFinished)
{
element1.setVisibility(View.VISIBLE);
}
#Override
public void onFinish()
{
element1.setVisibility(View.GONE);
}
}.start();
{
CountDownTimer cdt = new CountDownTimer(2000, 1000)
{
#Override
public void onTick(long millisUntilFinished)
{
element2.setVisibility(View.VISIBLE);
}
#Override
public void onFinish()
{
element2.setVisibility(View.GONE);
}
}.start();
} else if (data == 3)
{
CountDownTimer cdt = new CountDownTimer(2000, 1000)
{
#Override
public void onTick(long millisUntilFinished)
{
element3.setVisibility(View.VISIBLE);
}
#Override
public void onFinish()
{
element3.setVisibility(View.GONE);
}
}.start();
} else if (data == 4)
{
CountDownTimer cdt = new CountDownTimer(2000, 1000)
{
#Override
public void onTick(long millisUntilFinished)
{
element4.setVisibility(View.VISIBLE);
}
#Override
public void onFinish()
{
element4.setVisibility(View.GONE);
}
}.start();
} else if (data == 5)
{
CountDownTimer cdt = new CountDownTimer(2000, 1000)
{
#Override
public void onTick(long millisUntilFinished)
{
element5.setVisibility(View.VISIBLE);
}
#Override
public void onFinish()
{
element5.setVisibility(View.GONE);
}
}.start();
} else if (data == 6)
{
CountDownTimer cdt = new CountDownTimer(2000, 1000)
{
#Override
public void onTick(long millisUntilFinished)
{
element6.setVisibility(View.VISIBLE);
}
#Override
public void onFinish()
{
element6.setVisibility(View.GONE);
}
}.start();
}
id = id + 1;
text.setText("cos " + id);
} while (id < 3);
All elements are being shown in the same time. And I try add
Thread.sleep(2500);
But this stoped the action in window. I try add this after code:
Timer timer = new Timer();
timer.schedule( new TimerTask() {
public void run() {
}
}, 0, 60*1000);
But loop wasn't stay. I try do it with notify() and wait(), but it also don't work.
Don't suggest me to do do next action in onFinish() because this must be repeat a lot.
Anybody have another idea?
EDIT
I also use Handler but it don't work
I would recommend you not to implement this code in the main thread.
Create an async task, which hides a button and on postExequte calls itself.
Basically my aim is to have this timer reset whenever the user presses button b. I've tried a few methods such as if( i==true && bIsPressed()) but no luck, any ideas?
//2 buttons
Button =b;
TextView = time;
//countdown code
CountDownTimer Count = new CountDownTimer(11000, 1000) {
public void onTick(long millisUntilFinished) {
time.setText(""+millisUntilFinished / 1000);
}
public void onFinish() {
time.setText("Finished");
}
}; Count.start();
Haven't tested it, but I would do something along the lines of:
private void setupTimerResetButton()
{
mTimerResetButton.setOnClickListener(new OnClickListener(){
public void onClick(){
resetTimer();
}
});
}
private void resetTimer()
{
if(mTimer != null){
mTimer.cancel();
mTimer = null;
}
mTimer = new CountDownTimer(11000, 1000) {
public void onTick(long millisUntilFinished) {
mTimerTextView.setText(""+millisUntilFinished / 1000);
}
public void onFinish() {
mTimerTextView.setText("Finished");
}
};
mTimer.start();
}