Skip to content

Commit 1f56cd1

Browse files
imhappidsn5ft
authored andcommitted
[Slider] Fixes an exception caused by DecimalFormat not taking into account locale
PiperOrigin-RevId: 681165249
1 parent 039b484 commit 1f56cd1

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

lib/java/com/google/android/material/slider/BaseSlider.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@
102102
import java.lang.annotation.Retention;
103103
import java.lang.annotation.RetentionPolicy;
104104
import java.math.BigDecimal;
105+
import java.text.NumberFormat;
106+
import java.text.ParseException;
105107
import java.util.ArrayList;
106108
import java.util.Collections;
107109
import java.util.Iterator;
@@ -239,6 +241,8 @@ abstract class BaseSlider<
239241
"Floating point value used for %s(%s). Using floats can have rounding errors which may"
240242
+ " result in incorrect values. Instead, consider using integers with a custom"
241243
+ " LabelFormatter to display the value correctly.";
244+
private static final String WARNING_PARSE_ERROR =
245+
"Error parsing value(%s), valueFrom(%s), and valueTo(%s) into a float.";
242246

243247
private static final int TIMEOUT_SEND_ACCESSIBILITY_EVENT = 200;
244248
private static final int HALO_ALPHA = 63;
@@ -3251,7 +3255,7 @@ protected void onPopulateNodeForVirtualView(
32513255
info.addAction(AccessibilityNodeInfoCompat.AccessibilityActionCompat.ACTION_SET_PROGRESS);
32523256

32533257
List<Float> values = slider.getValues();
3254-
final float value = values.get(virtualViewId);
3258+
float value = values.get(virtualViewId);
32553259
float valueFrom = slider.getValueFrom();
32563260
float valueTo = slider.getValueTo();
32573261

@@ -3264,6 +3268,16 @@ protected void onPopulateNodeForVirtualView(
32643268
}
32653269
}
32663270

3271+
NumberFormat nf = NumberFormat.getNumberInstance();
3272+
nf.setMaximumFractionDigits(2);
3273+
try {
3274+
valueFrom = nf.parse(nf.format(valueFrom)).floatValue();
3275+
valueTo = nf.parse(nf.format(valueTo)).floatValue();
3276+
value = nf.parse(nf.format(value)).floatValue();
3277+
} catch (ParseException e) {
3278+
Log.w(TAG, String.format(WARNING_PARSE_ERROR, value, valueFrom, valueTo));
3279+
}
3280+
32673281
info.setRangeInfo(RangeInfoCompat.obtain(RANGE_TYPE_FLOAT, valueFrom, valueTo, value));
32683282
info.setClassName(SeekBar.class.getName());
32693283
StringBuilder contentDescription = new StringBuilder();

0 commit comments

Comments
 (0)