I have a problem with a button to be invisible when I want. In my if statement I have declared the exerciseButtonDone to first to be invisible but then after the final countdown I want it to become visible.
As my code is right now the button is visible all the time.
package org.example.anders.eazy;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.os.Vibrator;
import java.util.concurrent.TimeUnit;
public class ExerciseActivity extends Activity {
String[] exerciseList;
TextView textTimer,workoutTextview,exerciseTextView;
int setsChosen;
int counter = 0;
int timeChosen;
boolean togglebuttonpress;
Button exerciseButtonDone;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
exerciseList = getResources().getStringArray(R.array.heavy_chest_arms);
setContentView(R.layout.activity_exercise);
exerciseButtonDone=(Button)findViewById(R.id.buttonActiveExcercise);
Intent intent = getIntent();
String selectedWorkout = intent.getExtras().getString("workoutName");
togglebuttonpress=intent.getExtras().getBoolean("togglebuttonPress");
textTimer = (TextView) findViewById(R.id.timeUntilFinished);
//workoutTextview = (TextView) findViewById(R.id.workoutChosen);
//workoutTextview.setText(selectedWorkout);
if(togglebuttonpress){
exerciseButtonDone.setVisibility(View.INVISIBLE);
setsChosen =intent.getExtras().getInt("setsChosen");
int timePass = intent.getExtras().getInt("timeChosen");
timeChosen=timePass;
counter = 0;
runCountDownTimer();
exerciseButtonDone.setVisibility(View.VISIBLE);
}
else {
textTimer.setText("");
exerciseButtonDone.setVisibility(View.VISIBLE);
}
//exerciseTextView= (TextView) findViewById(R.id.exerciseNow);
//exerciseTextView.setText(exerciseList[i]);
}
public void runCountDownTimer() {
if (counter < setsChosen) {
new CountDownTimer(timeChosen * 60000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
long millis = millisUntilFinished;
String hms = String.format("%02d:%02d",
TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
System.out.println(hms);
textTimer.setText(hms);
}
#Override
public void onFinish() {
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
counter++;
v.vibrate(500);
runCountDownTimer();
}
}.start();
}
textTimer.setText("Complete");
}
}
You are setting the button invisible and then immediately visible.
if(togglebuttonpress){
exerciseButtonDone.setVisibility(View.INVISIBLE);
...
exerciseButtonDone.setVisibility(View.VISIBLE);
}
move exerciseButtonDone.setVisibility(View.VISIBLE); from its current location to where the countdown timer actually finishes, which I now see is runCountDownTimer()
if (counter < setsChosen) {
new CountDownTimer(timeChosen * 60000, 1000) {
...
}
} else {
exerciseButtonDone.setVisibility(View.VISIBLE);
}
you'll still need to make execerciseButtonDone final... and move it to the right scope, thank you alex. It needs to be a class variable, over by say boolean togglebuttonpress;
you COULD do findViewById again, but that's redundant and unnecessary.
I added an if statement that checked if loop was done. It worked.
public void onFinish() {
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
counter++;
v.vibrate(500);
runCountDownTimer();
if (counter == setsChosen)
{
exerciseButtonDone.setVisibility(View.VISIBLE);
}
}
Related
My app is a timed math game. Answer as many questions as you can before the timer runs out. When the time runs out, The GameOverActivity is now the current activity. I've realized that if I give no answer, the app will crash. If I give at least 1 answer, the app doesn't crash and everything is normal. I'm not sure where the flaw in my code exista.
This is the Main Activity
package stormy.incremental.randomtest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Random;
public class FastMathActivity extends AppCompatActivity {
int rand1, rand2, randDecider, correctAnswer, falseAnswer, problemsSolved;
String response,sumStr;
MyCountDownTimer myCountDownTimer;
int score;
Random r;
TextView randTV1, randTV2, scoreTV, sumTV, problemsSolvedTV, timerTV;
Button choice1, choice2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_random_test);
problemsSolved =0;
falseAnswer = 1;
//Initializing TextViews
timerTV = ((TextView) findViewById(R.id.timer));
randTV1 = ((TextView) findViewById(R.id.rand1));
randTV2 = ((TextView) findViewById(R.id.rand2));
sumTV = ((TextView) findViewById(R.id.sum));
scoreTV = ((TextView) findViewById(R.id.score));
problemsSolvedTV = ((TextView) findViewById(R.id.problemsSolved));
choice1 = ((Button) findViewById(R.id.choice1));
choice2 = ((Button) findViewById(R.id.choice2));
//Initializing a Random
r = new Random();
//Set the first question
setRandomProblem();
//Starting the timer
myCountDownTimer = new MyCountDownTimer(timerTV, 5000, 1000);
myCountDownTimer.start();
// Button Listeners
choice1.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
checkResponse((Button)v);
setRandomProblem();
}
});
choice2.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
checkResponse((Button)v);
setRandomProblem();
}
});
}
public void checkResponse(Button v) {
//Convert the response and correctAnswer to String in order to compare values
response = v.getText().toString();
sumStr = Integer.toString(correctAnswer);
//If the user clicks the correct answer, increment score
if ((response.equals(sumStr))) {
score++;
scoreTV.setText(score+"");
}
//Increment the total amount of problems solved
problemsSolved++;
problemsSolvedTV.setText(problemsSolved+"");
//Keep track of the score within the timer
myCountDownTimer.recordScore(score,problemsSolved);
}
private void setRandomProblem() {
//Assigning random values to ints
rand1 = r.nextInt(5 - 1) + 1;
rand2 = r.nextInt(5 - 1) + 1;
randDecider = r.nextInt(2) + 1;
//The correctAnswer of the randoms
correctAnswer = rand1 + rand2;
//Setting the texts of the random values
randTV1.setText(rand1 + "");
randTV2.setText(rand2 + "");
//If the random deciding number is 1, set answer on choice1
if (randDecider == 1) {
choice1.setText(correctAnswer + "");
choice2.setText(correctAnswer + falseAnswer + "");
}
//If the random deciding number is 2, set answer on choice2
else {
choice1.setText(correctAnswer + falseAnswer + "");
choice2.setText(correctAnswer + "");
}
}
#Override
public void onStop(){
super.onStop();
//Stop the timer
myCountDownTimer.cancel();
}
}
This is the GameOverActivity
package stormy.incremental.randomtest;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
/**
* Created by kamalu on 12/25/2017.
*/
public class GameOverActivity extends AppCompatActivity {
TextView scoreTV, problemsSolvedTV, percentageTV;
int score, problemsSolved, percentage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gameover);
//Initializing TextViews
scoreTV = ((TextView)findViewById(R.id.score));
problemsSolvedTV = ((TextView)findViewById(R.id.problemsSolved));
percentageTV = ((TextView)findViewById(R.id.percentage));
//Opening Bundle and assigning values
Bundle extras = getIntent().getExtras();
score = extras.getInt("score");
problemsSolved = extras.getInt("problemsSolved");
//calculating the accuracy
percentage = (score/problemsSolved)*100;
//Displaying the score
percentageTV.setText(percentage+"");
scoreTV.setText(score+"");
problemsSolvedTV.setText(problemsSolved+"");
}
//Start the game over
public void retry(View v){
Intent retryIntent = new Intent(GameOverActivity.this, FastMathActivity.class);
startActivity(retryIntent);
}
public void onBackPressed()
{
}
}
This is the Timer. I believe it to be important to note that the onFinish() method in this class starts the GameOverActivity.
package stormy.incremental.randomtest;
import android.content.Intent;
import android.os.CountDownTimer;
import android.widget.TextView;
public class MyCountDownTimer extends CountDownTimer {
TextView textCounter;
int score,problemsSolved;
public MyCountDownTimer(TextView textCounter, long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
this.textCounter = textCounter;
}
#Override
public void onTick (long millisUntilFinished){
textCounter.setText(String.valueOf(millisUntilFinished / 1000));
}
#Override
public void onFinish () {
Intent gameOverIntent = new Intent(textCounter.getContext(), GameOverActivity.class);
gameOverIntent.putExtra("score", score);
gameOverIntent.putExtra("problemsSolved", problemsSolved);
textCounter.getContext().startActivity(gameOverIntent);
}
//Keep track of the scores
public void recordScore(int score,int problemsSolved){
this.problemsSolved = problemsSolved;
this.score = score;
}
}
You should check:
//calculating the accuracy
percentage = (score/problemsSolved)*100;
if problemsSolved = 0, your app will crash with exeptions: java.lang.ArithmeticException
You can refer:
if (problemSolved != 0){
//calculating the accuracy
percentage = (score/problemsSolved)*100;
} else {
// handle with problemSolved = 0;
}
I hope it can help your problem!
I Have countDownTimer() method and want to print a certain toast if the user presses the button when the timer is at 10 or 9 seconds left. I am trying to read the time by reading the textview of the timer in the gameButton() method but it only prints the fail toast and not the pass toast. So why can I not do this? And also how can I get it to work? Thanks.
import android.app.Activity;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class GameScreen extends Activity {
private TextView time;
private Button start;
private Button cancel;
private Button gameButton;
private CountDownTimer countDownTimer;
private View.OnClickListener btnClickListener = new View.OnClickListener(){
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.start_ID :
start();
break;
case R.id.cancel :
cancel();
break;
case R.id.gameButton_ID :
gameButton();
break;
}
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_screen);
start = (Button) findViewById(R.id.start_ID);
start.setOnClickListener(btnClickListener);
cancel = (Button) findViewById(R.id.cancel);
cancel.setOnClickListener(btnClickListener);
time = (TextView) findViewById(R.id.time);
gameButton = (Button) findViewById(R.id.gameButton_ID);
gameButton.setOnClickListener(btnClickListener);
}
public void start(){
time.setText("15");
countDownTimer = new CountDownTimer(15 * 1000, 1000) {
#Override
public void onTick(long millsUntilFinished){
time.setText("" + millsUntilFinished / 1000);
/* gameButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
//if(time.getText() == "10" || time.getText() == "9" ){
Toast.makeText(new GameScreen(), "This is my Toast message!", Toast.LENGTH_LONG).show();
//}
}
}); */
}
public void onFinish(){
time.setText("Done !");
}
};
countDownTimer.start();
}
private void cancel(){
if(countDownTimer != null){
countDownTimer.cancel();
countDownTimer = null;
}
}
private void gameButton(){
if(time.getText() == "10" || time.getText() == "9" ){
Toast.makeText(getApplicationContext(), "PASS", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getApplicationContext(), "FAIL", Toast.LENGTH_SHORT).show();
}
}
}
Use equals method not == operator
time.getText().equals("10") || time.getText().equals("9")
See What is the difference between == vs equals() in Java?
In my app I am using a CountdownTimer. After each time the countdown timer has been executed I´m using a counter. When the counter meet a certain requirement I want the countdown timer to behave in another way onFinish and change the textview of my textTimer. Like it acts now my counter has already looped before starting the countdowntimer. How can I change that?
package org.example.anders.eazy;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.os.CountDownTimer;
import android.widget.TextView;
import android.os.Vibrator;
import java.util.concurrent.TimeUnit;
public class ExerciseActivity extends Activity {
String[] exerciseList;
TextView textTimer,workoutTextview,exerciseTextView;
int setsChosen;
int counter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
exerciseList = getResources().getStringArray(R.array.heavy_chest_arms);
Intent intent = getIntent();
setsChosen =intent.getExtras().getInt("setsChosen");
String selectedWorkout = intent.getExtras().getString("workoutName");
int timeChosen = intent.getExtras().getInt("timeChosen");
setContentView(R.layout.activity_exercise);
for(counter=0;counter<setsChosen;counter++) {
final CounterClass timer = new CounterClass(timeChosen * 60000, 1000);
timer.start();
textTimer = (TextView) findViewById(R.id.timeUntilFinished);
workoutTextview = (TextView) findViewById(R.id.workoutChosen);
//exerciseTextView= (TextView) findViewById(R.id.exerciseNow);
//textTimer.setText("00:01:00");
//exerciseTextView.setText(exerciseList[i]);
workoutTextview.setText(selectedWorkout);
}
}
public class CounterClass extends CountDownTimer {
/**
* #param millisInFuture The number of millis in the future from the call
* to {#link #start()} until the countdown is done and {#link #onFinish()}
* is called.
* #param countDownInterval The interval along the way to receive
* {#link #onTick(long)} callbacks.
*/
public CounterClass(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onTick(long millisUntilFinished) {
long millis= millisUntilFinished;
String hms=String.format("%02d:%02d",
TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
System.out.println(hms);
textTimer.setText(hms);
}
#Override
public void onFinish() {
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(500);
if(counter<setsChosen){
this.start();
}
else
textTimer.setText("Complete");
}
}
}
You can remove your loop and update counter at onFinish()
So, your code should be like this.
package org.example.anders.eazy;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.os.CountDownTimer;
import android.widget.TextView;
import android.os.Vibrator;
import java.util.concurrent.TimeUnit;
public class ExerciseActivity extends Activity {
String[] exerciseList;
TextView textTimer,workoutTextview,exerciseTextView;
int setsChosen;
int counter = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
exerciseList = getResources().getStringArray(R.array.heavy_chest_arms);
Intent intent = getIntent();
setsChosen =intent.getExtras().getInt("setsChosen");
String selectedWorkout = intent.getExtras().getString("workoutName");
int timeChosen = intent.getExtras().getInt("timeChosen");
setContentView(R.layout.activity_exercise);
if (counter < setsChosen) {
final CounterClass timer = new CounterClass(timeChosen * 60000, 1000);
timer.start();
textTimer = (TextView) findViewById(R.id.timeUntilFinished);
workoutTextview = (TextView) findViewById(R.id.workoutChosen);
//exerciseTextView= (TextView) findViewById(R.id.exerciseNow);
//textTimer.setText("00:01:00");
//exerciseTextView.setText(exerciseList[i]);
workoutTextview.setText(selectedWorkout);
}
}
public class CounterClass extends CountDownTimer {
/**
* #param millisInFuture The number of millis in the future from the call
* to {#link #start()} until the countdown is done and {#link #onFinish()}
* is called.
* #param countDownInterval The interval along the way to receive
* {#link #onTick(long)} callbacks.
*/
public CounterClass(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onTick(long millisUntilFinished) {
long millis= millisUntilFinished;
String hms=String.format("%02d:%02d",
TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
System.out.println(hms);
textTimer.setText(hms);
}
#Override
public void onFinish() {
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(500);
counter ++;
if(counter<setsChosen){
this.start();
}
else
textTimer.setText("Complete");
}
}
}
And for simply (doesn't need to create CountDownTimer's subclass). You can create a function runCountDownTimer() for looping like this.
package org.example.anders.eazy;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.os.CountDownTimer;
import android.widget.TextView;
import android.os.Vibrator;
import java.util.concurrent.TimeUnit;
public class ExerciseActivity extends Activity {
String[] exerciseList;
TextView textTimer,workoutTextview,exerciseTextView;
int setsChosen;
int counter = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
exerciseList = getResources().getStringArray(R.array.heavy_chest_arms);
Intent intent = getIntent();
setsChosen =intent.getExtras().getInt("setsChosen");
String selectedWorkout = intent.getExtras().getString("workoutName");
int timeChosen = intent.getExtras().getInt("timeChosen");
setContentView(R.layout.activity_exercise);
textTimer = (TextView) findViewById(R.id.timeUntilFinished);
workoutTextview = (TextView) findViewById(R.id.workoutChosen);
//exerciseTextView= (TextView) findViewById(R.id.exerciseNow);
//textTimer.setText("00:01:00");
//exerciseTextView.setText(exerciseList[i]);
workoutTextview.setText(selectedWorkout);
counter = 0;
runCountDownTimer();
}
public void runCountDownTimer() {
if (counter < setsChosen) {
new CountDownTimer(timeChosen * 60000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
long millis = millisUntilFinished;
String hms = String.format("%02d:%02d",
TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
System.out.println(hms);
textTimer.setText(hms);
}
#Override
public void onFinish() {
counter++;
runCountDownTimer();
}
}.start();
}
}
}
I try to code a CountDownTimer which can be repeated several times. My code works fine... But just one time. I don't understand why the repeated action is not done :-(
Here is my code :
package com.bigmat.MyTimer;
import android.app.Activity;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.View;
import android.widget.*;
public class MainActivity extends Activity {
private TextView inputDuration;
private TextView tvCdTimer;
private Button start;
CountDownTimer myCdTimer;
NumberPicker npDuration;
CheckBox isRepeat;
TextView tvRepeat;
int nbRepeat;
int count;
/**
* Called when the activity is first created.
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
inputDuration = (TextView) findViewById(R.id.tvInputDuration);
tvCdTimer = (TextView) findViewById(R.id.tvCdTimer);
start = (Button) findViewById(R.id.btStart);
npDuration = (NumberPicker) findViewById(R.id.npDuration);
npDuration.setMaxValue(60);
npDuration.setMinValue(0);
isRepeat = (CheckBox) findViewById(R.id.cbRepeat);
start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
count=0;
tvRepeat = (TextView) findViewById(R.id.nbRepeat);
if (tvRepeat.getText().toString().length() > 0) {
nbRepeat = Integer.parseInt(tvRepeat.getText().toString());
} else {
nbRepeat = 1;
}
runTimer();
}
});
}
public void runTimer(){
Log.i("CDT", "value of npDuration is : "+npDuration.getValue());
if (npDuration.getValue() > 0 && nbRepeat > 0) {
// create a timer
myCdTimer = new CountDownTimer(npDuration.getValue()*1000, 1000) {
public void onTick(long millisUntilFinished) {
tvCdTimer.setText("" + millisUntilFinished / 1000);
}
public void onFinish() {
tvCdTimer.setText("Done ! ");
try {
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();
} catch (Exception e) {
e.printStackTrace();
}
count++;
//condition
if (count < nbRepeat) {
Log.i("MyTimer", "value of nbRepeat :" + nbRepeat + " value of count : " + count);
myCdTimer.cancel();
myCdTimer.start();
}
}
}.start();
} else {
Toast.makeText(getApplicationContext(), "Timer is set on 0 seconds !!!", Toast.LENGTH_LONG).show();
}
}
}
Thanks in advance!
I am having problem in stopping a CountDownTimer. I have searched a lot but couldn't understand how to do that.
Below is my MainActivity. How can I stop the timer if RadioButton b is pressed?
import java.util.Timer;
import java.util.TimerTask;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
TextView mTextField;
RadioButton a, b, c, d, e;
final static long interval = 1000;
long timeout = 15000;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextField = (TextView) findViewById(R.id.tv);
a = (RadioButton) findViewById(R.id.rB1a);
b = (RadioButton) findViewById(R.id.rB2a);
c = (RadioButton) findViewById(R.id.rB3a);
d = (RadioButton) findViewById(R.id.rB4a);
e = (RadioButton) findViewById(R.id.rB5a);
//Timer timer = new Timer();
//timer.scheduleAtFixedRate(task, interval, interval);
TimerTask task = new TimerTask() {
#Override
public void run() {
timeout = timeout - interval;
if (timeout == 0) {
this.cancel();
displayText("finished");
return;
}
if (timeout >= 0) {
displayText("time remaining: " + String.valueOf(timeout / 1000));
a.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(), "Wrong Answer!!! ", Toast.LENGTH_LONG).show();
Intent openWordlist = new Intent("com.example.the_vocab_master.AA");
startActivity(openWordlist);
}
});
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(), "GREAT...!!! ", Toast.LENGTH_LONG).show();
}
});
}
//displayText("time remaining: " + String.valueOf(timeout / 1000));
}
};
Timer timer = new Timer();
timer.scheduleAtFixedRate(task, interval, interval);
}
private void displayText(final String text) {
this.runOnUiThread(new Runnable() {
#Override
public void run() {
mTextField.setText(text);
}
});
}
}
To cancel the timer use
timer.cancel();
in your onClick listener. It's very difficult to see in your code, because the indentation is very bad, but I think you need to declare your timer variable in the class, instead of in onCreate();
Additionally, I'd suggest you to take both setOnClickListener calls OUTSIDE of the timer run() method. What you're doing doesn't make sense.