I want to show custom dialogs in order. Each dialog must shown for 5 second then it must be dismissed and the other one must be shown. I use for loop and my code looks like:
for(int i = 0 ; i < 10 ; i++){
popupView.show();
SystemClock.sleep(3000);
popupView.dismiss();
SystemClock.sleep(1000);
}
Try this code and adjust the time interval as your need.
showDialogs();
final Handler handler = new Handler();
Timer timer = new Timer();
TimerTask doShowDialog = new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
public void run() {
alertDialog.dismiss();
if (mDialogCounter < 5) {
showDialogs();
mDialogCounter++;
}
}
});
}
};
timer.schedule(doShowDialog, 5000, 5000);
Related
I want to print the current second using a handler. I record a video for exactly 10 seconds and want to set the text of a TextView every second.
Recording 10 seconds works like that:
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
stopRecordingVideo();
}
}, 11000); // don't know why 11000 but it only works this way
After the 10 seconds the method stopRecordingVideo() gets executed. So how can I change the text every second of the TextView?
Working answer:
int t = 0;
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
t++;
textView.setText(getString(R.string.formatted_time, t));
if(t<10) {
handler.postDelayed(this, 1000);
}
}
}, 1000);
Where formatted_time is something like that:
<string android:name="formatted_time">%d seconds</string>
To print text every second, you can use CountDownTimer. But if you want to achieve this with try below code:
void startTime(){
//Post handler after 1 second.
handler.postDelayed(runnable, 1000);
}
int totalDelay=0;
Handler handler = new Handler();
Runnable runnable = new Runnable() {
#Override
public void run() {
totalDelay++;
if(totalDelay<=10){
//If total time is less then 10 second execute handler again after 1 second
handler.postDelayed(runnable, 1000);
}
textView.setText(totalDelay+" Second");
}
};
Try this, basically do the increment in a worker thread, but updating the text view is done by main's thread handler.
Thread worker= new Thread(new Runnable() {
#Override
public void run() {
for (int i = 0; i < 10; i++) {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
// stop recording after 10 seconds
if (i == 9) {
handler.post(new Runnable() {
#Override
public void run() {
stopRecordingVideo();
}
});
}
else{
// set text for each second
handler.post(new Runnable() {
#Override
public void run() {
textView.setText(String.valueOf(i+1));
}
});
}
}//ends for()
worker.start()
I want to create 10 ImageViews with a delay of 5 seconds between, I created this code:
new Timer().scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
if (counter <= 10)
newimg();
else
// here I want to stop the timer so it will not try to create any more `ImageViews` (the array contains only 10).
}
}, 0, 5000);
private void newimg() {
ball[counter] = new ImageView(this);
ball[counter].setTag(counter);
ball[counter].setBackgroundResource(R.mipmap.ball);
int randomx = rand.nextInt(layoutwidth);
int randomy = rand.nextInt(layoutheight);
ball[counter].setX(randomx);
ball[counter].setY(randomy);
rlt.addView(ball[counter]);
counter++;
}
How can I stop the timer inside the else statement?
Call Timer cancel to stop the timer.
Call cancel() on timer
My Solutions is :
Handler handler = new Handler();
handler.postDelayed(createImageView, 5000);
Runnable createImageView=new Runnable() {
#Override
public void run() {
if (counter <= 10){
handler.postDelayed(this,5000);
newimg();
}
}
};
First, I used animation to hide and show TextView. I saw that using animation costing memory. So I used another way:
SetVisibility(VISIBLE) and SetVisibility(INVISIBLE) with TaskTimer
it works well and it performs better considering the memory.
The main issue is that after a restarting the timer for many times, the TextView disappear.
I need to restart the app to get it back again!
this is the code snippet:
myTimerForAnimation = new Timer();
myTimerForAnimation.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() // run on ui thread
{
public void run() {
counter++;
if (counter < 7) {
if (counter % 2 == 1) {
list_textView[x].setVisibility(View.INVISIBLE);
} else {
list_textView[x].setVisibility(View.VISIBLE);
}
} else {
myTimerForAnimation.cancel();
myTimerForAnimation.purge();
list_textView[x].setVisibility(View.VISIBLE);
}
}
});
}
}, 1000, 600);
Dont use Timer use handler,something like this:
// init the runnables
// the runnable should be members
Handler hanlder = new Handler();//If you arent on the UI thread pass a correct looper
for (int i=1; i<7 ; i++){
long delay = i * 1000;
if (i%2==0)
{
handler.postDelayed(mVisibleRunnable,delay);
}else{
handler.postDelayed(mInVisibleRunnable,delay);
}
}
Whereas the get runnablebs should be memebers because if u choose to cancel the callbacks then call
handler.removeCallbacks(runnable);
Play with it. It should fix your issue.
thanks to #EE66 for the loop idea, I used this code to solve my problem:
private void animateView(final View view){
for (int counter = 0; counter < 7; counter++) {
long delay = counter * 1000;
if (counter % 2 == 0) {
view.postDelayed(new Runnable() {
public void run() {
view.setVisibility(View.VISIBLE);
}
;
}, delay);
} else {
view.postDelayed(new Runnable() {
public void run() {
view.setVisibility(View.INVISIBLE);
}
;
}, delay);
}
}
}
I have an image view. So in the image view i want to change the images after a certain time period. Images are coming from an array list. Now when the no of images are 3 or more than 3, it is working perfect. But when it is 2, my logic is not working. Second image is visible for a moment and then again changed to first image here is my code:
r = new Runnable(){
int i = 0;
public void run(){
iv.setImageBitmap(alBmps.get(i));
i++;
if(i >= alBmps.size()){
i = 0;
}
iv.postDelayed(r, 5000); //set to go off again in 5 seconds.
}
};
iv.postDelayed(r, 1000);
Can any one help me what changes i need on the above code?
Thanks.
Try this
declare variables
static int i=0;
private Timer myTimer;
in your onCreate or on button click where you want to call and start the methods
myTimer = new Timer();
myTimer.schedule(new TimerTask()
{
#Override
public void run()
{
TimerMethod();
}
}, 500, 5000);
add these methods to your class
private void TimerMethod()
{
this.runOnUiThread(Timer_Tick);
}
private Runnable Timer_Tick = new Runnable()
{
public void run()
{
if(i<alBmps.size())
{
iv.setImageBitmap(alBmps.get(i));
}
else
{
i=0;
iv.setImageBitmap(alBmps.get(i));
}
i++;
}
};
I am using an ImageSwitcher with a TouchListener to change images from an array. Its working fine but i want it to switch images every x seconds or so, so that I can add imageSwitcher.setImageResource(imageList[curIndex]); to it.
Any suggestions?
Try this,
imageSwitcher.postDelayed(new Runnable() {
int i = 0;
public void run() {
imageSwitcher.setImageResource(
i++ % 2 == 0 ?
R.drawable.image1 :
R.drawable.mage2);
imageSwitcher.postDelayed(this, 1000);
}
}, 1000);
I think it is possible via TimerTask and Timer. please Try this code. I think It help you.
private Handler mHandler;
private Runnable mUpdateResults;
private Timer timerAnimate;
private TimerTask timerTask;
mHandler = new Handler();
mUpdateResults = new Runnable() {
public void run() {
AnimateandSlideShow();
}
};
int delay = 0;
int period = 15000;
timerAnimate = new Timer();
timerTask = new TimerTask() {
public void run() {
mHandler.post(mUpdateResults);
}
};
timerAnimate.scheduleAtFixedRate(timerTask, delay, period);
Public void AnimateandSlideShow()
{
imageSwitcher.setImageResource(imageList[curIndex]);
///Here You need To handle curIndex position.
}