So first open menu.xml which is in res/values/menu.xml and add this item to it:
<item
android:id="@+id/counter"
android:title=""
android:showAsAction="always">
</item>
android:id="@+id/counter"
android:title=""
android:showAsAction="always">
</item>
then in your Activity that you want to show the countdown timer, replace the code in "public boolean onCreateOptionsMenu(Menu menu)" with the code below:
public void onFinish() {
counter.setTitle("done!");
}
}.start();
return true;
}
If you want to set the countdown time to a different value, then just change the timer variable to a different value, the timer should be in milliseconds
1 second = 1000 milliseconds
1 minute = 60000 milliseconds
1 hour = 3600000 milliseconds
This is how it looks like :
long timer = 10000;
public Boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu, menu);
final MenuItem counter = menu.findItem(R.id.counter);
new CountDownTimer(timer, 1000) {
public void onTick(long millisUntilFinished) {
long millis = millisUntilFinished;
String hms = (TimeUnit.MILLISECONDS.toHours(millis))+":"+ (TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)))+":"+ (TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
counter.setTitle(hms);
timer = millis;
}
public Boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu, menu);
final MenuItem counter = menu.findItem(R.id.counter);
new CountDownTimer(timer, 1000) {
public void onTick(long millisUntilFinished) {
long millis = millisUntilFinished;
String hms = (TimeUnit.MILLISECONDS.toHours(millis))+":"+ (TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)))+":"+ (TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
counter.setTitle(hms);
timer = millis;
}
public void onFinish() {
counter.setTitle("done!");
}
}.start();
return true;
}
If you want to set the countdown time to a different value, then just change the timer variable to a different value, the timer should be in milliseconds
1 second = 1000 milliseconds
1 minute = 60000 milliseconds
1 hour = 3600000 milliseconds
This is how it looks like :
0 comments :
Post a Comment