I am using two imageview win which I want to use two countdowntimer. 1 timer shows 1 image at 1000ms ,after that another countdowntimer start and shows 2 image and after second image go on another screen but I have used this with single countdowntimer. How can I use 2 countdowntimer in my class? Here is my code:
public class MainActivity extends Activity {
private ImageView image1,image2;
CountDownTimer countdown1,countdown2;
int a = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.aaa);
image1=(ImageView)findViewById(R.id.image1);
image2=(ImageView)findViewById(R.id.image2);
image1=(ImageView)findViewById(R.id.image1);
image2=(ImageView)findViewById(R.id.image2);
image1.setVisibility(View.VISIBLE);
image2.setVisibility(View.INVISIBLE);
new CountDownTimer(3000, 1000) {
public void onTick(long millisUntilFinished) {
if(a == 0)
{
image1.setVisibility(View.VISIBLE);
image2.setVisibility(View.INVISIBLE);
}
else {
} a++;
}
public void onFinish() {
image2.setVisibility(View.VISIBLE);
image1.setVisibility(View.VISIBLE);
Intent i=new Intent(MainActivity.this,NextActivity.class);
startActivity(i);
}
}.start();
}
}
I am not sure if I understood the request but i would try something like this:
public class MainActivity extends Activity {
private ImageView image1,image2;
int a = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.aaa);
image1=(ImageView)findViewById(R.id.image1);
image2=(ImageView)findViewById(R.id.image2);
image1.setVisibility(View.VISIBLE);
image2.setVisibility(View.INVISIBLE);
new CountDownTimer(4000, 1000) {
public void onTick(long millisUntilFinished) {
if(a == 0) {
image1.setVisibility(View.VISIBLE);
} else if(a == 1) {
image1.setVisibility(View.INVISIBLE);
image2.setVisibility(View.VISIBLE);
}
a++;
}
public void onFinish() {
Intent i=new Intent(MainActivity.this,NextActivity.class);
startActivity(i);
}
}.start();
}
}
Related
I have been trying to create the onbackpressed() function using countdown but i don't know what to do. Kindly help me to create the onbackpressed function using countdown
private static boolean onbackpressonce = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
// using countdown timer
#Override
public void onBackPressed() {
if (!onbackpressonce) {
Toast.makeText(this, "Press back again to exit", Toast.LENGTH_SHORT).show();
onbackpressonce = true;
} else {
super.onBackPressed();
}
new CountDownTimer(3000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
}
#Override
public void onFinish() {
onbackpressonce = false;
}
}.start();
}
This will exit your app on the second onBackpressed within 1100 millis
private long mLastBackClick = 0;
#Override
public void onBackPressed() {
if (System.currentTimeMillis() - mLastBackClick < 1100) {
super.onBackPressed();
} else {
Toast.makeText(MainActivity.this, "Press back again to Exit", Toast.LENGTH_SHORT).show();
mLastBackClick = System.currentTimeMillis();
}
}
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.
my code is given below in that for Each Question there is i.e. when count down ends its jump to next question and count down starts again, i want that when the countdown ends it should jump to the result page..and even after jumping to next question countdown should not stop.
#Override
protected void onResume()
{
super.onResume();
questionPlay = db.getRuleQuestionMode(mode);
totalQuestion = questionPlay.size();
mCountDown = new CountDownTimer(TIMEOUT, INTERVAL) {
#Override
public void onTick(long millisUntilFinished) {
progressBar.setProgress(progressValue);
progressValue++;
}
#Override
public void onFinish() {
mCountDown.cancel();
}
};
showQuestion(index);
db.close();
}
private void showQuestion(final int index) {
if (index < totalQuestion) {
thisQuestion++;
txtQuestion.setText(String.format("%d/%d", thisQuestion, totalQuestion));
progressBar.setProgress(0);
progressValue = 0;
txtView1.setText(questionPlay.get(index).getQus());
btnA.setText(questionPlay.get(index).getAnswerA());
btnB.setText(questionPlay.get(index).getAnswerB());
btnC.setText(questionPlay.get(index).getAnswerC());
btnD.setText(questionPlay.get(index).getAnswerD());
mCountDown.start();
Button btnca =(Button) findViewById(R.id.btnca);
btnca.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
StyleableToast st= new StyleableToast(getApplicationContext(),questionPlay.get(index).getCorrectAnswer(), Toast.LENGTH_SHORT);
st.setBackgroundColor(Color.RED);
st.setTextColor(Color.WHITE);
st.setCornerRadius(3);
st.show();
score-=10;
}
});
} else {
Intent intent = new Intent(this, Done.class);
Bundle dataSend = new Bundle();
dataSend.putInt("SCORE", score);
dataSend.putInt("TOTAL", totalQuestion);
dataSend.putInt("CORRECT", correctAnswer);
intent.putExtras(dataSend);
startActivity(intent);
finish();
}
}
#Override
public void onClick(View v) {
mCountDown.cancel();
if (index < totalQuestion) {
Button clickedButton = (Button) v;
if (clickedButton.getText().equals(questionPlay.get(index).getCorrectAnswer())) {
score += 10; // increase score
correctAnswer++; //increase correct answer
showQuestion(++index);
} else {
vibrator.vibrate(50);
showQuestion(++index); // If choose right , just go to next question
}
txtScore.setText(String.format("%d", score));
//clickedButton.setBackgroundColor(Color.YELLOW);;
}
}
#Override
public void onStop() {
if(mCountDown != null)
mCountDown.cancel();
super.onStop();
}
remove mCountDown.start(); in your showQuestion method.
in your onFinish method, call your results page from there
I have a game where if a user touches the wrong button he goes to the highscores page and if he clicks the right one he goes to the next level. What I would like to do is make it so if the user does absolutely nothing for 1.5 seconds (fast-paced game) then it automatically intents him back to the scores.class activity. I am new to programming so anything helps!!! Thanks.
This will give you an idea:
private MainActivity context;
private CountDownTimer countDownTimer;
public boolean timerStopped;
/** Called when the activity is first created. */
#Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context = this;
startTimer();
// method looks at users choice, example
/* if (answer == true){
stopTimer();
// go to next question and start timer again..
}
else{
// do something
}
*/
}
/** Starts the timer **/
public void startTimer() {
setTimerStartListener();
timerStopped = false;
}
/** Stop the timer **/
public void stopTimer() {
countDownTimer.cancel();
timerStopped = true;
}
/** Timer method: CountDownTimer **/
private void setTimerStartListener() {
// will be called at every 1500 milliseconds i.e. every 1.5 second.
countDownTimer = new CountDownTimer(1500, 1500) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
// Here do what you like...
Intent intent = new Intent(context, Scores.class);
startActivity(intent);
}
}.start();
}
have you tried with CountDownTimer?
Here is an example:
new CountDownTimer(1500, 1500) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
// Here do what you like...
}
}.start();
To fix the error mentioned:
MainActivity context;
#Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context = this;
new CountDownTimer(1500, 1500) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
// Here do what you like...
Intent intent = new Intent(context, Score.class);
startActivity(intent);
}
}.start();
here is the code im using...my countdown timer doesnt display 0(zero) after 1...how to do it...I have tried replacing 1000 with 0 in countdown ...but it brought error while debugging...plss hellppppppp
public class play extends Activity {
TextView mTextField;
TextView score;
ToggleButton pause;
CountDownTimer countdown;
private long a;
private long b;
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.game);
mTextField=(TextView)findViewById(R.id.tme);
score=(TextView)findViewById(R.id.score);
score.setText("0" );
final ImageButton menu=(ImageButton)findViewById(R.id.menu);
menu.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent(play.this , GoodwordActivity.class);
startActivity(intent);
}
});
Bundle bundle = getIntent().getExtras();
b= bundle.getInt("a");
a=b;
startCountDownTimer();
pause=(ToggleButton)findViewById(R.id.pause);
pause.setText("");
pause.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (pause.isChecked()) {
pause.setText("");
pause.setBackgroundResource(R.drawable.refresh);
countdown.cancel();
} else {
pause.setText("");
pause.setBackgroundResource(R.drawable.pause);
startCountDownTimer();
}
}
});
}
private void startCountDownTimer( )
{
countdown=new CountDownTimer(a, 1000) {
public void onTick(long millisUntilFinished ) {
a=millisUntilFinished;
mTextField.setText("" + millisUntilFinished/1000);
}
public void onFinish() {
a=b;
startCountDownTimer();
}
}.start();
}
}
I think you have to put the
mTextField.setText("0");
into the onFinish() function.