1
        @Override
        public void onTick(long millisUntilFinished) {
            Date date = new Date(millisUntilFinished);
            SimpleDateFormat formater = new SimpleDateFormat("m:ss.S");

            timeRemaining.setText(formater.format(date));
        }

This is the code in my onTick method for a CountDownTimer. My question is that on 2 different android devices (4.4.4) & (5.0) the text is showing up differently.

On 4.4.4 it shows up as m:ss.SSS

On 5.0 it shows as expected m:ss.S

This is reproduced in any method of showing text (debug, Toast, TextView) so it is not a display or font issue, it is with the SimpleDateFormat

First Image INCORRECT (Android 4.4.4)

INCORRECT (Android 4.4.4)

Second Image CORRECT (Android 5.0) enter image description here

Kailua Bum
  • 1,368
  • 7
  • 25
  • 40

2 Answers2

1

This behavior is actually normal. Have a look at the SimpleDateFormat documentation.

S fractional seconds (Number) 978

There is no "SSS", fractional seconds (aka "S") can be any value (most likely between 0 and 1000).

EDIT: you can eventually fix is this way too:

                Date date = new Date();
                String myTimerString = new SimpleDateFormat("m:ss.", Locale.ENGLISH).format(date);
                String myTimerFraction = new SimpleDateFormat("S", Locale.ENGLISH).format(date);
                if (myTimerFraction.length()>1)
                    myTimerFraction = myTimerFraction.substring(0,1);
                myTimerString += myTimerFraction;
Wildcopper
  • 373
  • 1
  • 3
  • 11
  • both "m:ss:S" and "m:ss:SS" and even "m:ss:SSSS" format as expected in Android 5.0 – Kailua Bum Mar 06 '15 at 09:43
  • Then it's a new feature in 5.0, or a bug fix maybe. My javadoc for SimpleDateFormat clearly says that just "S" can display 3 digits though. I tested on my Xperia Z Ultra which also runs Kitkat (4.4.4) and it does the same than on your Samsung under 4.4.4. – Wildcopper Mar 06 '15 at 09:49
  • i found a work around, not real clean... https://community.oracle.com/thread/2118022 – Kailua Bum Mar 06 '15 at 09:57
0

Probably it is related with the text size on different devices with different dpi.You have to create different <dimen> sizes for different devices.

The time is probably formatted correctly but it is not shown at all devices with different dpi (i mean that the last two characters are not shown but they exist there)

so instead of:

android:textSize="36sp"

use something like:

android:textSize="@dimens/text_size" />

where dimens folder is included in different values folders with different text size (ex: in values>dimens.xml and values-xxhdpi>dimens.xml)

note: Also be sure that you didnt play with Settings>Font size. IF you want to avoid that also then you have to get rid of sp and instead use dp

hrskrs
  • 4,447
  • 5
  • 38
  • 52
  • Its actually two of the same device, Galaxy S5, one running 4.4.4 and the other running 5.0. See updated xml – Kailua Bum Mar 06 '15 at 07:40
  • this might be true. try to view the value from somewhere else, _toast_ for example – Baby Mar 06 '15 at 07:42
  • have you checked `settings > font size` if they are same on both devices? Maybe someone played with those settings – hrskrs Mar 06 '15 at 07:43
  • @KailuaBum change `android:textSize="36sp" />` to `android:textSize="36dp" />` and try if it works – hrskrs Mar 06 '15 at 07:50
  • toast also shows format differently, m:ss.S vs. m:ss:SSS. this tells me that its not a font size issue – Kailua Bum Mar 06 '15 at 07:52
  • try changing `SimpleDateFormat formater = new SimpleDateFormat("m:ss.S");` to `SimpleDateFormat formater = new SimpleDateFormat("m:ss.SSS");` and again `show on toast` or log it to `logcat` and tell what it is showing on both devices – hrskrs Mar 06 '15 at 07:55
  • @hrskrs it shows as m:ss.SSS on both devices, in textView and Toast – Kailua Bum Mar 06 '15 at 07:58
  • Ok now change `sp` of `textSize` to `dp` and check if it is showing correctly on both devices – hrskrs Mar 06 '15 at 08:02
  • sp & dp will have no effect on Toast. So it has to be an issue with the formatting not the displaying of the TextView – Kailua Bum Mar 06 '15 at 08:10
  • @KailuaBum i said change that and try on your devices where do you want textiew to appear if they are showing correctly, not on your toast. If this will show correctly then read my edited answer and use those strategies – hrskrs Mar 06 '15 at 08:12
  • @hrskrs it is not showing correctly on Android 4.4.4, it is showing as m:ss.SSS regardless of how I set the SimpleDateFormat – Kailua Bum Mar 06 '15 at 08:15
  • @KailuaBum can you please provide some screenshots? Also have you checked `settings->font size` if same for both devices? – hrskrs Mar 06 '15 at 08:16
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/72392/discussion-between-kailua-bum-and-hrskrs). – Kailua Bum Mar 06 '15 at 08:31