Skip to content Skip to sidebar Skip to footer

Is It Possible To Add A Timer To The Actionbar On Android?

I'd like a simple mm:ss timer to be displayed on my actionbar, be able to pick the number of minutes to start counting down from and then call a method once it's at 0. Is this poss

Solution 1:

I've managed to do that by adding a TextView to the Action Bar as an Action View in the menu, then updating it at regular intervals using a CountDownTimer. (Tested on an Android 3.0 emulator, an Android 4.3 emulator and a real device running Android 4.3).

Here's the XML for the menu item. Note that actionViewClass="android.widget.TextVew" (showAsAction and actionViewClass don't have the android namespace because I'm using the support action bar).

<?xml version="1.0" encoding="utf-8"?><menuxmlns:android="http://schemas.android.com/apk/res/android"xmlns:compatibility="http://schemas.android.com/apk/res-auto" ><itemandroid:id="@+id/break_timer"android:title="@string/break_timer_title"compatibility:showAsAction="always"compatibility:actionViewClass="android.widget.TextView" /></menu>

In onCreateOptionsMenu, I find the MenuItem and call getActionView to assign it to private TextView, timerText. I also apply some padding here, so that it's not right at the edge of the screen.

@OverridepublicbooleanonCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.break_timer, menu);

    MenuItemtimerItem= menu.findItem(R.id.break_timer);
    timerText = (TextView) MenuItemCompat.getActionView(timerItem);

    timerText.setPadding(10, 0, 10, 0); //Or something like that...

    startTimer(30000, 1000); //One tick every second for 30 secondsreturntrue;
}

I have a method called startTimer to start a CountDownTimer and update timerText in its onTick. I call startTimer from onCreateOptionsMenu to ensure that timerText is not accessed before it has been initialised. (Previously, I called startTimer from onCreate, but got a NullPointerException).

privatevoidstartTimer(long duration, long interval) {

    CountDownTimertimer=newCountDownTimer(duration, interval) {

        @OverridepublicvoidonFinish() {
            //TODO Whatever's meant to happen when it finishes
        }

        @OverridepublicvoidonTick(long millisecondsLeft) {
            intsecondsLeft= (int) Math.round((millisecondsLeft / (double) 1000));
            timerText.setText(secondsToString(secondsLeft));
        }
    };

    timer.start();
}

I'm sure that anyone reading this will know a better way to convert an integer number of seconds to a timecode such as "00:00:00", but just in case I'll share the secondsToString method I used in onTick, above.

privateStringsecondsToString(int improperSeconds) {

    //Seconds must be fewer than are in a dayTime secConverter = newTime();

    secConverter.hour = 0;
    secConverter.minute = 0;
    secConverter.second = 0;

    secConverter.second = improperSeconds;
    secConverter.normalize(true);

    String hours = String.valueOf(secConverter.hour);
    String minutes = String.valueOf(secConverter.minute);
    String seconds = String.valueOf(secConverter.second);

    if (seconds.length() < 2) {
        seconds = "0" + seconds;
    }
    if (minutes.length() < 2) {
        minutes = "0" + minutes;
    }
    if (hours.length() < 2) {
        hours = "0" + hours;
    }

    String timeString = hours + ":" + minutes + ":" + seconds;
    return timeString;
}

Solution 2:

Menu xml

<item
                android:id="@+id/counter"
                android:title=""
                android:showAsAction="always">
</item>

**public boolean onCreateOptionsMenu(Menu menu)"

longtimer=10000;
public Boolean onCreateOptionsMenu(Menu menu) 
{
        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.menu, menu);

       finalMenuItemcounter= menu.findItem(R.id.counter);
        newCountDownTimer(timer, 1000) {

          publicvoidonTick(long millisUntilFinished) {
             longmillis= millisUntilFinished; 
             Stringhms= (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;

            }

        publicvoidonFinish() {
             counter.setTitle("done!");
            }
         }.start();

         returntrue;

 }

Solution 3:

Yes it's possible to add timer in toolbar action use this code in Java:

privatevoidstartTimer(long duration, long interval) {

CountDownTimertimer=newCountDownTimer(duration, interval) {

    @OverridepublicvoidonFinish() {
        //TODO Whatever's meant to happen when it finishes
    }

    @OverridepublicvoidonTick(long millisecondsLeft) {
        intsecondsLeft= (int) Math.round((millisecondsLeft / (double) 1000));
        timerText.setText(secondsToString(secondsLeft));
    }
};

timer.start();
}

and also use menu file defined timer id just like this

if learn more the visit youtube :)

Post a Comment for "Is It Possible To Add A Timer To The Actionbar On Android?"