Darren Murtagh
Darren Murtagh

Reputation: 591

Android Chronometer: time format issue

Why my chronometer wouldn't stop after I've told it to?

I have found out that it seems to be isolated to the tablet I have tested it on (7 inch Galaxy Tab). I have no idea why it would work on my phone yet not work on the tablet. The Chronometer is set up the same in both layouts (since I am using different layouts for the different screen sizes).

Here is how the Chronometer is handled in the class:

this.chrono = (Chronometer) findViewById(R.id.calling_crono);

chrono.setOnChronometerTickListener(new OnChronometerTickListener() {

            public void onChronometerTick(Chronometer chronometer) {
                String currentTime= chrono.getText().toString();
            if(currentTime.equals("00:20")) 
            {
              chrono.stop();
              Intent intent = new Intent(QuizActivity.this, ResultActivity.class);
            Bundle b = new Bundle();

            int end = -5;
            b.putInt("score", score);
            b.putInt("end", end);
            intent.putExtras(b); 
            startActivity(intent);
            finish();
             }
            }
        });
        public void startCrono() {
 chrono.setBase(SystemClock.elapsedRealtime());
 chrono.start();
}

Here is how it is set up in the normal-size screen:

<Chronometer
    android:id="@+id/calling_crono"
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content"
    android:textColor="#000099" 
    android:textStyle="bold"
    android:textSize="20sp"/>

And here it is in the large-size screen:

 <Chronometer
    android:id="@+id/calling_crono"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="50sp"
    android:textColor="#000099"
    android:textStyle="bold" />

The basic problem is that on the phone it works as normal, which is to move on after 20 seconds, but on the tablet it won't stop and keeps going ahead. Is there some reason why this works on my phone (Xperia), but not on the tablet. The only thing I can think off is the fact that they use different xml layout files.

Upvotes: 0

Views: 718

Answers (1)

Onik
Onik

Reputation: 19959

I believe what you're experiencing isn't related to "different layout xml files". It seems to be an Android bug, probably this one: Chronometer in Android 4.2.

To check that I've done a couple of tests (with only one layout) on a normal screen size phone (Android 4.2.2) and Samsung Galaxy Tab 7 (Android 4.0.3). I get the following string format of the returned value of chrono.getText().toString()

  1. Android 4.0.3: "mm:ss"
  2. Android 4.2.2: "m:ss"

which corresponds to the reported bug.

Assuming that your Galaxy Tab 7 has Android 4.2 explains why the Chronometer doesn't stop and keeps going ahead: the condition currentTime.equals("00:20") is false because you compare two strings of different format, "m:ss" with "mm:ss".

If my assumption is wrong and it does not regard to the Android version yet there is a solution to handle it.

Solution: I recommend formatting currentTime in a certain manner, e.g. "mm:ss" and then compare the formatted time with "00:20". The following code snippet uses SimpleDateFormat:

chrono.setOnChronometerTickListener(new OnChronometerTickListener() {

        public void onChronometerTick(Chronometer chronometer) {

            String currentTime= chrono.getText().toString();
            Date date = null;
            try {
                date = new SimpleDateFormat("mm:ss").parse(currentTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            String newTime = new SimpleDateFormat("mm:ss").format(date);
            ...

Now do the comparison using newTime:

if(newTime.equals("00:20")) {
    // your code
}

Upvotes: 1

Related Questions