6

I have an app that crashes on ICS. Worked fine up to then (though I'm not sure if I ever really got a honeycomb platform to test on, all of our test phones are either gingerbread or lower, and now I have a couple ICS phones to play with).

The following code (called from onResume and OnPreferenceChangeListener) from my preferences page has worked fine:

protected void setBatteryAlarmSummary(String newValue){
    Preference batteryAlarm = (Preference) findPreference( getString(R.string.battery_low_alarm) );
    StringBuilder summary = new StringBuilder();
    summary.append(getString(R.string.battery_alarm_summary_label));
    summary.append(" ");
    summary.append(newValue);
    summary.append("%");
    batteryAlarm.setSummary(summary);
}

This sets the pref summary to "Low Battery Alarm at 10%". Now, with ICS, it crashes. Not when it does the setSummary, and not when the page displays, but when you scroll the preferences even a little bit, obviously triggering a render (this item is about 8 or so items down, so it's "below the fold" on the list). Fixing ICS is easy, just escape the percent sign:

summary.append("%%");

However, that code on gingerbread displays "Low Battery Alarm at 10%%"

I can write it to change based on version, but that's just silly. Did they really break backward compatibility on their preferences rendering, or is this just a Samsung thing (which, unfortunately, is the only test platform I have for ICS right now)?

Evert
  • 93,428
  • 18
  • 118
  • 189
gbryant
  • 751
  • 5
  • 19

2 Answers2

0

You can try the Unicode equivalent of the percent sign, like:

summary.append("\u0025");

this should work.

ChuongPham
  • 4,761
  • 8
  • 43
  • 53
  • The Java compiler itself will interpret this Unicode sequence. It is identical to actually writing % in the source file. – Sean Owen Jul 27 '12 at 07:27
  • Yes, but then you don't have to escape it if you just write it as "%". That's the difference. I have used the Unicode value in my projects without any issue. – ChuongPham Jul 27 '12 at 10:41
  • There is not a difference between `summary.append("%");` and `summary.append("\u0025");` though. `javac` interprets that literal. See http://stackoverflow.com/questions/8115522/a-unicode-newline-character-u000d-in-java I don't doubt it works for you, but it does not work in the OP's situation. – Sean Owen Jul 27 '12 at 11:37
0

Normally, you handle this in strings.xml:

<string name="battery_low_alarm_summary" formatted="false">Low Battery Alarm at 10%</string>

formatted="false" will make it work in all versions of Android.

But you really want a placeholder once you localize this string. I think you will have to write:

<string name="battery_low_alarm_summary">Low Battery Alarm at %d%%</string>

I have not tested this, but, I would strongly hope that buildling using any modern version of the SDK will do the right thing here, even when the result is run on old versions of Android.

Sean Owen
  • 66,182
  • 23
  • 141
  • 173