Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chronometer counting in characters

Tags:

android

I just added a chronometer to my app and it has a weird behavior. Instead of counting in numbers it's doing it in CHARS! Example:

00:00:00
00:00:0(
00:00:0)
00:00:0/
00:00:0*
00:00:0+
... LOOPS AFTER 10
00:00:00
00:00:0(
00:00:0)
00:00:0/
00:00:0*
00:00:0+

xml

        <Chronometer
            android:id="@+id/tracking_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@id/record_indicator"
            android:textColor="@color/tracking_time"
            android:textSize="14dip"
            android:textStyle="bold" />

onCreate

        mTrackingTime = (Chronometer)this.findViewById(R.id.tracking_time);
        mTrackingTime.setOnChronometerTickListener(this);

start

    mTrackingTime.setBase(System.currentTimeMillis());
    mTrackingTime.start();

callback

    @Override
    public void onChronometerTick(final Chronometer chronometer) {
    }
like image 973
MLProgrammer-CiM Avatar asked Apr 30 '26 22:04

MLProgrammer-CiM


2 Answers

These characters appear when the base is larger than SystemClock.elapsedRealtime(), which is the milliseconds since boot.

It displays the result of this argument:

SystemClock.elapsedRealtime() - base

The default, or if you call:

chronometer.setBase(SystemClock.elapsedRealtime());

Then the chronometer will start from 00:00

If you call:

chronometer.setBase(SystemClock.elapsedRealtime() - 10000);

Then the chronometer will start from 00:10

If you call:

chronometer.setBase(SystemClock.elapsedRealtime() + 10000);

Then the chronometer will draw rubbish for 10 seconds, and than start from 00:00

So if you call:

chronometer.setBase(System.currentTimeMillis());

You should expect to see rubbish for about 44 years, and than it will be ok.

like image 198
Amir Uval Avatar answered May 02 '26 11:05

Amir Uval


As per my understanding Chronometer.setBase() wants millisecond based on the elapsedRealTime. This means that epoch milliseconds don't work. In order to be able to use epoch milliseconds, you have to call Chronometer.setBase() like following:

chronometer.setBase(SystemClock.elapsedRealtime());

The math effectively converts the epoch milliseconds to elapsed real time milliseconds.

Try this and see if that fixes your problem...

Updated as per @Ef Es comment..

like image 20
Praful Bhatnagar Avatar answered May 02 '26 11:05

Praful Bhatnagar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!