I have small question.
new CountDownTimer(12000, 2000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
}
}.start();
Here I would like setText to 6 different TextView(in one xml) with each onTick(). Any ideas how i can make this? Thanks
//edit
I would like have something like this
First tick
tv.setText("one")
Second Tick
tv2.setText("two")
try using Array of TextViews like
TextView[] textViews = new TextView[6];
String[] value = new String[]{"one","two","three","four","five","six"};
int count = 0;
new CountDownTimer(12000, 2000) {
public void onTick(long millisUntilFinished) {
textViews[count].setText(value[count]);// you can set dynamic string variable also
count++;
}
public void onFinish() {
}
}.start();
You could do something like this:
new CountDownTimer(12000, 2000) {
int counter = 0;
public void onTick(long millisUntilFinished) {
switch (counter) {
case 0:
tv1.setText("");
break;
case 1:
tv2.setText("");
break;
// ...
}
counter++;
}
public void onFinish() {
}
}.start();
Check Below Answer,
new CountDownTimer(12000, 2000) {
public void onTick(long millisUntilFinished) {
Activity_Name.this.runOnUiThread(new Runnable() {
#Override
public void run() {
tv1.setText("");
tv2.setText("");
tv3.setText("");
tv4.setText("");
}
});
}
public void onFinish() {
}
}.start();
Related
I have four balls on the screen red , blue , green , yellow which has two states ON , OFF. Initially all of them are in OFF state.My requirement is that I have a string suppose "RBYGRBYG" and for every letter i want to change the state of the corresponding ball one by one (R->RED,B->BLUE,Y->YELLOW,G->GREEN) for 0.5 seconds. But my code for playString("RBYGRBYG"); changes the state of all balls simultaneously at once for 0.5 seconds instead of changing it one by one.
private void playString(String str) {
for(int i=0;i<str.length();i++)
{
if(str.charAt(i)=='R')
{
red.setImageResource(R.drawable.red_on);
new CountDownTimer(500,100){
#Override
public void onTick(long millisUntilFinished) {
}
#Override
public void onFinish() {
red.setImageResource(R.drawable.red_off);
}
}.start();
}
if(str.charAt(i)=='B')
{
blue.setImageResource(R.drawable.blue_on);
new CountDownTimer(500,100){
#Override
public void onTick(long millisUntilFinished) {
}
#Override
public void onFinish() {
blue.setImageResource(R.drawable.blue_off);
}
}.start();
}
if(str.charAt(i)=='G')
{
green.setImageResource(R.drawable.green_on);
new CountDownTimer(500,100){
#Override
public void onTick(long millisUntilFinished) {
}
#Override
public void onFinish() {
green.setImageResource(R.drawable.green_off);
}
}.start();
}
if(str.charAt(i)=='Y')
{
yellow.setImageResource(R.drawable.yellow_on);
new CountDownTimer(500,100){
#Override
public void onTick(long millisUntilFinished) {
}
#Override
public void onFinish() {
yellow.setImageResource(R.drawable.yellow_off);
}
}.start();
}
}
}
Try this :
private int i= 0; // class field
private void playString(String str) {
i++;
if (i == str.length())
i = 0;
if(str.charAt(i)=='R')
// the rest of the code the same removing one "}" at the end
Let's see if I got it right:
-for each letter on your string, you want to change the state of the ball(coresponding with the char in string).
Maybe try with a while instead of for, and you will increment i++ only in the function onFinish() this way it will get to the next color only if previous one has stopped.
EDIT Hope this helps :
private void playString(String str){
while (i < str.length()){
if(str.charAt(i)=='R'){
red.setImageResource(R.drawable.red_on);
new CountDownTimer(500,100){
#Override
public void onTick(long millisUntilFinished) {
}
#Override
public void onFinish() {
red.setImageResource(R.drawable.red_off);
i+=1;
}
}.start();
}
}
}
I have implemented one counter and onFinish() of first counter,I started second counter but the first counter not able to finish.Text "Bye Guyz" remain for some time so how to finish the text.
Please help me.
Thanks in advance.!!!
Code :-
counter= new CountDownTimer(10000, 1000) {
public void onTick(long millisUntilFinished) {
if (count == 0) {
tv.setText("First counter");
tv2.setVisibility(View.VISIBLE);
tv2.setText("Hello Guyz");
}
}
public void onFinish() {
if(!flag) {
tv2.setText("Bye Guyz");
count = 0;
try {
counter.cancel();
}catch (Exception e){}
}
else if(flag) {
counter1 = new CountDownTimer(9000, 1000) {
public void onTick(long millisUntilFinished) {
flag = false;
tv.setText("Second counter");
tv2.setVisibility(View.VISIBLE);
tv2.setText("Hello Girls");
count = 0;
}
public void onFinish() {
tv2.setVisibility(View.VISIBLE);
tv2.setText("Bye Girls");
count = 0;
}
}.start();
Did you "debug" the code to be sure the code is arriving to counter1 = new CountDownTimer(9000, 1000)?
Are you sure when the first counter arrives to onFinish() the flag variable is true?
Why do you call counter.cancel() in onFinish() when obviously the counter is already over?
public void onFinish() {
if(!flag) {
tv2.setText("Bye Guyz");
count = 0;
try {
counter.cancel();
}catch (Exception e){}
}
If you say your tv2 displays "Bye Guyz" it means that your flag is set to false, so the "else if" part is not being executed. onFinish() is only executed once, so you need to make sure the flag is set for true to start the second counter.
Also you shouldn't cancel your counter in onFinish() because it's already finished.
Here is my alternative is as follows
Create the custom Counterextending Thread
class Counter extends Thread {
private long timeOne, timeTwo;
private OnCounterFinishedListener mCounterFinishedListener;
private Thread t;
Activity activity = null;
Counter(Context context){
t = new Thread(this);
activity = (Activity)context;
}
#Override
public void run() {
try {
sleep(timeOne);
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
mCounterFinishedListener.firstCounterFinished();
}
});
sleep(timeTwo);
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
mCounterFinishedListener.secondCounterFinished();
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
void setTimes(long timeOne, long timeTwo){
this.timeOne = timeOne;
this.timeTwo = timeTwo;
}
public void start(OnCounterFinishedListener listener){
mCounterFinishedListener = listener;
t.start();
}
interface OnCounterFinishedListener{
void firstCounterFinished();
void secondCounterFinished();
}
}
Then inside your main thread you can start this counter as
final Counter counter = new Counter(this);
counter.setTimes(5000, 5000);
counter.start(new Counter.OnCounterFinishedListener() {
#Override
public void firstCounterFinished() {
// Update your first TextView
}
#Override
public void secondCounterFinished() {
// Update your second TextView
}
});
I have the user inputting the time in the countdown timer.
Right now, its fixed to 10sec. How can i change this to user input ?
CountDownTimer counter = new CountDownTimer(10000,1000) {
#Override
public void onTick(long millisUntilFinished) {
//millisUntilFinished=20000;
settime.setText("" + millisUntilFinished / 1000);
}
#Override
public void onFinish() {
quescount++;
if (quescount%5==0){
round ++;
rndno.setText(String.valueOf(round));
// onBackPressed();
}
quescounter.setText("Out of "+String.valueOf(quescount));
// j = randomcount(getcount(),1);
++qcounter;
//j=qcounter;
if (qcounter<count)
setdata(String.valueOf(list.get(qcounter)));
else
{
Collections.shuffle(list);
qcounter=0;
setdata(String.valueOf(list.get(qcounter)));
}
}
};
Call this method where you take the input from the user:
public void startCountDown(long duration) {
CountDownTimer counter = new CountDownTimer(duration, 1000) {
#Override
public void onTick(long millisUntilFinished) {
//millisUntilFinished=20000;
settime.setText("" + millisUntilFinished / 1000);
}
#Override
public void onFinish() {
quescount++;
if (quescount%5==0){
round ++;
rndno.setText(String.valueOf(round));
// onBackPressed();
}
quescounter.setText("Out of "+String.valueOf(quescount));
// j = randomcount(getcount(),1);
++qcounter;
//j=qcounter;
if (qcounter<count)
setdata(String.valueOf(list.get(qcounter)));
else
{
Collections.shuffle(list);
qcounter=0;
setdata(String.valueOf(list.get(qcounter)));
}
}
};
}
You'll probably want to make your CountDownTimer variable global as well.
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();
}