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.

Answers


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
}

Need Your Help

Restore a SQL Server 2008 database to SQL Server 2012 Express

sql-server-2008 backup restore sql-server-2012-express

When I try to restore my .bak file, a SQL Server 2008 database file from my production server, to my local SQL Server 2012 Express server, I get the following screenshot:

RestSharp - how to influence JSON serialization (naming)?

c# json restsharp

I am trying to talk to a REST service and I'm trying to call a POST method where I need to provide some data in the post body.