Pop up count down alert with images (android) - android

I'm trying to create a countdown timer that will pop up and show a different image every second so it goes "3, 2, 1, Start" then starts a different activity. I've tried this numerous ways but can't get any to work.. if anyone could point me in the right direction that'd be great !
package com.practice;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.widget.ImageView;
public class Practice2 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final long delayCount = 1000;
final long delayIncrement = 1000;
final long delayCount1 = 1000;
final long delayIncrement1 = 1000;
final long delayCount2 = 1000;
final long delayIncrement2 = 1000;
final Dialog dialog = new Dialog (Practice2.this,
android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
dialog.setTitle("Get ready to play DrawTastic!");
final CountDownTimer timer = new CountDownTimer(delayCount, delayIncrement) {
#Override
public void onTick(long millisUntilFinished) {
// TODO Auto-generated method stub
ImageView three = (ImageView) dialog.findViewById(R.id.iv3);
three.setImageResource(R.drawable.three);
}
#Override
public void onFinish() {
// TODO Auto-generated method stub
ImageView two = (ImageView)dialog.findViewById(R.id.iv2);
two.setImageResource(R.drawable.two);
timer1.start();
}
final CountDownTimer timer1 = new CountDownTimer(delayCount1, delayIncrement1) {
#Override
public void onTick(long millisUntilFinished) {
// TODO Auto-generated method stub
ImageView one = (ImageView) dialog.findViewById(R.id.iv1);
one.setImageResource(R.drawable.one);
}
#Override
public void onFinish() {
// TODO Auto-generated method stub
ImageView play = (ImageView) dialog.findViewById(R.id.ivPlay);
play.setImageResource(R.drawable.play);
timer2.start();
}
final CountDownTimer timer2 = new CountDownTimer(delayCount2, delayIncrement2) {
#Override
public void onTick(long millisUntilFinished) {
// TODO Auto-generated method stub
ImageView play = (ImageView) dialog.findViewById(R.id.ivPlay);
play.setImageResource(R.drawable.play);
}
#Override
public void onFinish() {
// TODO Auto-generated method stub
ImageView play = (ImageView) dialog.findViewById(R.id.ivPlay);
play.setImageResource(R.drawable.play);
dialog.dismiss();
}
};
};
};
timer.start();
dialog.show();
}
}

Make use of Handler. You'll need to show 4 different images at a time interval of 1 second each. Something like -
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
imageView.setBackgroundResource(R.id.nextImage);
}
}, 1000);
But, I'd suggest you to use TextView as shown in API demos.
Go to Views->TextSwitcher in API demos.

Related

CountDownTimer Is Not Cancelling --- Continues to Run After Cancel()

I've implemented a CountDownTimer in my code as follows: At the top of the class, I create
CountDownTimer myTimer;
Then when a user presses button Start, the following method is called:
private void countme()
{
final int tick = 500;
final int countTime = 10000;
myTimer = new CountDownTimer(countTime, tick) {
#Override
public void onTick(final long millisUntilFinished) { }
#Override
public void onFinish() {
myPicture.setVisibility(View.GONE);
}
};
myTimer.start();
}
I have button Stop all myTimer.cancel(). As you can see, if the timer is not cancelled, myPicture will disappear.
Even if I click the stop button so that myTimer.cancel() is called (I checked this with log statements), the counter still continues to count down and to make the picture disappear when it's done.
Why isn't it stopping? How do I get it to actually cancel?
To clarify, I do know how to implement Runnable timers, but they are not as accurate for my needs as CountDownTimers are, which is why I'm not using them in this case.
After a lot of tries, trick is to declare the timer in onCreate but start and cancel it in some other method. The onFinish() will not call after cancelling the timer.
myTimer = new CountDownTimer(COUNT_DOWN_TIME, TICK) {
#Override
public void onTick(final long millisUntilFinished) {
((TextView) findViewById(R.id.textView3)).setText(""
+ millisUntilFinished);
}
#Override
public void onFinish() {
findViewById(R.id.timer_imageBiew).setVisibility(View.GONE);
}
};
private fun startTimer() {
myTimer .start()
}
private fun stopTimer() {
myTimer .cancel()
}
Here in your method countme() you are initializing myTimer, so outside this method myTimer has no value.
Use this
Declare at the top
CountDownTimer myTimer;
final int tick = 500;
final int countTime = 10000;
In the onCreate method of Activity or Fragment
myTimer = new CountDownTimer(countTime, tick) {
#Override
public void onTick(final long millisUntilFinished) { }
#Override
public void onFinish() {
myPicture.setVisibility(View.GONE);
}
};
Now use myTimer.start() to start and myTimer.cancel() to stop it.
Hope you understood.
Your post is very odd. I just tried doing a sample activity:
public class MainActivity extends AppCompatActivity {
CountDownTimer myTimer;
Button btnStart;
Button btnCancel;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sample2);
btnStart = (Button) findViewById(R.id.start);
btnStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
countme();
Toast.makeText(MainActivity.this, "Count Started!", Toast.LENGTH_SHORT).show();
}
});
btnCancel = (Button) findViewById(R.id.cancel_timer);
btnCancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myTimer.cancel();
Toast.makeText(MainActivity.this, "Clicked Stop Timer!", Toast.LENGTH_SHORT).show();
}
});
}
private void countme() {
final int tick = 500;
final int countTime = 10000;
myTimer = new CountDownTimer(countTime, tick) {
#Override
public void onTick(final long millisUntilFinished) {
Log.d(MainActivity.class.getSimpleName(), "onTick()");
}
#Override
public void onFinish() {
// myPicture.setVisibility(View.GONE);
Toast.makeText(MainActivity.this, "In onFinish()", Toast.LENGTH_SHORT).show();
}
};
myTimer.start();
}
}
It works perfectly fine. It stops the timer. But I went and looked around and found this answer where it mentions that CountDownTimer doesn't seem to work, so he suggested to use a Timer instead. Do check it out. Cheers!
This is working example , I have implemented both handler and timer you can pick one .
import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class MainActivity extends Activity {
private CountDownTimer myTimer;
final int TICK = 500;
final int COUNT_DOWN_TIME = 2000;
// Option 2 using handler
private Handler myhandler = new Handler();
private Runnable runnable;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Option 1 using timer
myTimer = new CountDownTimer(COUNT_DOWN_TIME, TICK) {
#Override
public void onTick(final long millisUntilFinished) {
((TextView) findViewById(R.id.textView3)).setText(""
+ millisUntilFinished);
}
#Override
public void onFinish() {
findViewById(R.id.timer_imageBiew).setVisibility(View.GONE);
}
};
// Option 2 using handler
runnable = new Runnable() {
#Override
public void run() {
findViewById(R.id.handlerImageView).setVisibility(View.GONE);
}
};
findViewById(R.id.start_timer).setOnClickListener(
new OnClickListener() {
#Override
public void onClick(View v) {
// Option 1 using timer
myTimer.start();
// Option 2 using handler
myhandler.postDelayed(runnable, COUNT_DOWN_TIME);
}
});
findViewById(R.id.stop_timer).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Option 1 stop timer
myTimer.cancel();
// Option 2 stop handler
myhandler.removeCallbacks(runnable);
}
});
}
}

How to use Lap Button using timer program in android(lap button code for timmer application in android)

I have one button start and three editText. If I press the start button then timer starts and the button text changes to Lap and hour minutes and seconds are displayed in edittext1. If I press Lap button then editText1's values are stop. All three values of editText1 are displayed in editText2 at stop position of editText1.
Please help me for Lap button source code. I have the following code so far:
package com.example.timmerproject;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class LapActivity extends Activity
{
EditText ed1,ed2,ed3;
Button btntimer;
final Handler handler = new Handler();
long startTime=0;
Handler h2 = new Handler();
Runnable run = new Runnable()
{
#Override
public void run()
{
// TODO Auto-generated method stub
long millis = System.currentTimeMillis() - startTime;
int seconds = (int)(millis/1000);
int hours =seconds/3600;
int minutes = (seconds % 3600)/60;
seconds = seconds % 60;
ed1.setText(String.format("%02d:%02d:%02d", hours,minutes,seconds));
h2.postDelayed(this, 500);
}
};
class firstTask extends TimerTask
{
#Override
public void run()
{
handler.sendEmptyMessage(0);
}
};
class secondTask extends TimerTask
{
#Override
public void run()
{
// TODO Auto-generated method stub
LapActivity.this.runOnUiThread(new Runnable()
{
#Override
public void run()
{
// TODO Auto-generated method stub
long millis = System.currentTimeMillis() - startTime;
int seconds = (int)(millis/1000);
int hours =seconds/3600;
int minutes = (seconds % 3600)/60;
seconds = seconds % 60;
ed2.setText(String.format("%02d:%02d:%02d", hours,minutes,seconds));
}
});
}
}
class thirdTask extends TimerTask
{
#Override
public void run()
{
// TODO Auto-generated method stub
LapActivity.this.runOnUiThread(new Runnable()
{
#Override
public void run()
{
// TODO Auto-generated method stub
long millis = System.currentTimeMillis() - startTime;
int seconds = (int)(millis/1000);
int hours =seconds/3600;
int minutes = (seconds % 3600)/60;
seconds = seconds % 60;
ed3.setText(String.format("%02d:%02d:%02d", hours,minutes,seconds));
}
});
}
}
Timer timer = new Timer();
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.lap);
ed1 = (EditText)findViewById(R.id.ed11);
ed2 = (EditText)findViewById(R.id.ed12);
ed3 = (EditText)findViewById(R.id.ed13);
btntimer = (Button)findViewById(R.id.lapbutton);
btntimer.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
Button btn = (Button)v;
if(btn.getText().equals("Stop"))
{
timer.cancel();
timer.purge();
h2.removeCallbacks(run);
btn.setText("Start");
}
else
{
startTime = System.currentTimeMillis();
timer = new Timer();
timer.schedule(new firstTask(), 0,500);
timer.schedule(new secondTask(), 0,500);
timer.schedule(new thirdTask(), 0,500);
h2.postDelayed(run, 0);
btn.setText("Stop");
}
}
});
}
}

Android Automatic Refresh Button

I'm having trouble with something that seems like it should be very simple. I have a frame layout with 2 Buttons (one on top of the other naturally). When I click on the top button, it automatically takes me to a website, and the button beneath it replaces it to be the visible one. I want to set up an automatic refresh so that a few seconds later, the button that was originally on top becomes on top again. Thank you for any help you can give! Here is the Java, and with my attempt at creating an automatic refresh:
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ImageButton bJava1= (ImageButton)findViewById(R.id.button1);
final ImageButton bJava2 = (ImageButton)findViewById(R.id.button2);
final WebView webview1= (WebView)this.findViewById(R.id.webView1);
final MediaPlayer sound= MediaPlayer.create(Youtube.this, R.raw.soundclip1);
final Handler handler = new Handler();
Refresh = new Runnable() {
public void run() {
bJava1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
webview1.loadUrl("http://www.google.com");
if(sound.isPlaying()){
bJava1.setVisibility(ImageButton.VISIBLE);
bJava2.setVisibility(ImageButton.GONE);
}else {
sound.start();
bJava1.setVisibility(ImageButton.GONE);
bJava2.setVisibility(ImageButton.VISIBLE);
}
}
});
handler.postDelayed(Refresh, 10000);
}
};
handler.post(Refresh);
Problem is, that your Refresh runnable only registers a onClickListener on the first button and calls itself every 10 seconds, you should register the onClickListener only once outside the Runnable and only call the if block within your Refresh.run() Method:
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ImageButton bJava1= (ImageButton)findViewById(R.id.button1);
final ImageButton bJava2 = (ImageButton)findViewById(R.id.button2);
final WebView webview1= (WebView)this.findViewById(R.id.webView1);
final MediaPlayer sound= MediaPlayer.create(Youtube.this, R.raw.soundclip1);
final Handler handler = new Handler();
Refresh = new Runnable() {
public void run() {
if(sound.isPlaying()){
bJava1.setVisibility(ImageButton.VISIBLE);
bJava2.setVisibility(ImageButton.GONE);
}else {
sound.start();
bJava1.setVisibility(ImageButton.GONE);
bJava2.setVisibility(ImageButton.VISIBLE);
}
}
};
bJava1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
webview1.loadUrl("http://www.google.com");
handler.postDelayed(Refresh, 10000);
}
});

how to stop the timer in android

This is my code following,I am changing some iamges dynamically in my image view.
public class LoadingScreen extends Activity{
public static Integer[] imageList={R.drawable.food_pics1,R.drawable.food_pics2,R.drawable.food_pics3,
R.drawable.food_pics4,R.drawable.food_pics5,R.drawable.food_pics6,R.drawable.food_pics7,
R.drawable.food_pics8,R.drawable.food_pics9};
Thread thread;
ImageView foodImageView;
final Handler myHandler=new Handler();
public int currentImageIndex=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.load_xml);
foodImageView=(ImageView)findViewById(R.id.imageView_food);
// final int i=0;
final Runnable runnable = new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
animateImages();
}
};
final int delay=500;
final long period=1000;
Timer timer=new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
// TODO Auto-generated method stub
myHandler.post(runnable);
}
}, delay, period);
}
private void animateImages() {
// TODO Auto-generated method stub
foodImageView.setImageResource(imageList[currentImageIndex%imageList.length]);
currentImageIndex++;
foodImageView.getAnimation();
}
I want to stop the timer and finish this activity after 20 secs.how can I do that.
Try using this,
View v = new View(this);
v.postDelayed(new Runnable(){
public void run() {
// TODO Auto-generated method stub
//cancel your animation and finish the Activity here.
finish();
}
}, (long) 20000.0);
You can say something like this :
timer.cancel();
timer = null;
Save the TimerTask instance in the class and invoke cancel on it.
Here's a tip I found very useful without using Java's Timer: Try synchronizing things in the UI thread, especially if you want to do UI tasks. Example:
... onCreate() {
getUiThreadHandler().post(new Runnable(){
if (canceled()) {
return;
}
// Actual work
...
// Invoke again after delay
getUiThreadHandler().postDelayed(this, 500);
});
Good luck

how to make the countdown timer to count till zero?

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.

Categories

Resources