@@ -49,6 +49,7 @@ public class MaterialSwitch extends SwitchCompat {
4949 private static final int DEF_STYLE_RES = R .style .Widget_Material3_CompoundButton_MaterialSwitch ;
5050
5151 @ NonNull private final SwitchWidth switchWidth = SwitchWidth .create (this );
52+ @ NonNull private final ThumbPosition thumbPosition = new ThumbPosition ();
5253
5354 @ Nullable private Drawable trackDrawable ;
5455 @ Nullable private Drawable trackDecorationDrawable ;
@@ -239,6 +240,12 @@ public PorterDuff.Mode getTrackDecorationTintMode() {
239240 return trackDecorationTintMode ;
240241 }
241242
243+ // TODO(b/227338106): remove this workaround to use super.getThumbPosition() directly after
244+ // AppCompat 1.6.0-stable is released.
245+ private float getThumbPosition () {
246+ return thumbPosition .get ();
247+ }
248+
242249 private void refreshTrackDrawable () {
243250 trackDrawable = setDrawableTintListIfNeeded (trackDrawable , trackTintList , getTrackTintMode ());
244251 trackDecorationDrawable = setDrawableTintListIfNeeded (
@@ -325,4 +332,36 @@ private static Field createSwitchWidthField() {
325332 }
326333 }
327334 }
335+
336+ // TODO(b/227338106): remove this workaround to use super.getThumbPosition() directly after
337+ // AppCompat 1.6.0-stable is released.
338+ @ SuppressLint ("PrivateApi" )
339+ private final class ThumbPosition {
340+ private final Field thumbPositionField ;
341+
342+ private ThumbPosition () {
343+ thumbPositionField = createThumbPositionField ();
344+ }
345+
346+ float get () {
347+ try {
348+ if (thumbPositionField != null ) {
349+ return thumbPositionField .getFloat (MaterialSwitch .this );
350+ }
351+ } catch (IllegalAccessException e ) {
352+ // Fall through
353+ }
354+ return isChecked () ? 1 : 0 ;
355+ }
356+
357+ private Field createThumbPositionField () {
358+ try {
359+ Field thumbPositionField = SwitchCompat .class .getDeclaredField ("mThumbPosition" );
360+ thumbPositionField .setAccessible (true );
361+ return thumbPositionField ;
362+ } catch (Exception e ) {
363+ return null ;
364+ }
365+ }
366+ }
328367}
0 commit comments