Escaping% in ICS settings. Should I really write string processing on a specific version?

I have an application that crashes on ICS. Worked well before that (although I'm not sure that I ever really got a honeycomb platform for testing, all of our test phones are gingerbread or lower, and now I have several ICS phones to play with).

The following code (called from onResume and OnPreferenceChangeListener) from my settings page 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 preliminary summary to โ€œLow Battery Signal at 10%โ€. Now, with ICS, he is falling. Not when it does setSummary, and not when the page is displayed, but when you scroll through the settings a bit, obviously causing a rendering (this element has about 8 or so elements, so it is โ€œbelow the foldโ€ in the list). Fixing ICS is easy, just avoid the percent sign:

 summary.append("%%"); 

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

I can write it for a change based on the version, but that is just plain stupid. Did they really break backward compatibility when rendering their preferences, or is it just a Samsung thing (which, unfortunately, is the only test platform for ICS)?

+6
source share
2 answers

You can try the Unicode equivalent of a percent sign, for example:

summary.append("\u0025");

this should work.

0
source

Usually you process 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 to fill in the space as soon as you localize this line. I think you will have to write:

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

I did not test this, but I would really hope that building using any modern version of the SDK will work here, even if the result runs on older versions of Android.

0
source

Source: https://habr.com/ru/post/920542/


All Articles