Skip to content

Commit 8a97b4a

Browse files
dsandlerAndroid (Google) Code Review
authored andcommitted
Merge "Fix text transformations in Switches."
2 parents 1b96594 + 4c3308d commit 8a97b4a

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

core/java/android/widget/Switch.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import android.text.StaticLayout;
3030
import android.text.TextPaint;
3131
import android.text.TextUtils;
32+
import android.text.method.AllCapsTransformationMethod;
33+
import android.text.method.TransformationMethod2;
3234
import android.util.AttributeSet;
3335
import android.view.Gravity;
3436
import android.view.MotionEvent;
@@ -91,6 +93,7 @@ public class Switch extends CompoundButton {
9193
private ColorStateList mTextColors;
9294
private Layout mOnLayout;
9395
private Layout mOffLayout;
96+
private TransformationMethod2 mSwitchTransformationMethod;
9497

9598
@SuppressWarnings("hiding")
9699
private final Rect mTempRect = new Rect();
@@ -207,6 +210,15 @@ public void setSwitchTextAppearance(Context context, int resid) {
207210

208211
setSwitchTypefaceByIndex(typefaceIndex, styleIndex);
209212

213+
boolean allCaps = appearance.getBoolean(com.android.internal.R.styleable.
214+
TextAppearance_textAllCaps, false);
215+
if (allCaps) {
216+
mSwitchTransformationMethod = new AllCapsTransformationMethod(getContext());
217+
mSwitchTransformationMethod.setLengthChangesAllowed(true);
218+
} else {
219+
mSwitchTransformationMethod = null;
220+
}
221+
210222
appearance.recycle();
211223
}
212224

@@ -526,8 +538,12 @@ public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
526538
}
527539

528540
private Layout makeLayout(CharSequence text) {
529-
return new StaticLayout(text, mTextPaint,
530-
(int) Math.ceil(Layout.getDesiredWidth(text, mTextPaint)),
541+
final CharSequence transformed = (mSwitchTransformationMethod != null)
542+
? mSwitchTransformationMethod.getTransformation(text, this)
543+
: text;
544+
545+
return new StaticLayout(transformed, mTextPaint,
546+
(int) Math.ceil(Layout.getDesiredWidth(transformed, mTextPaint)),
531547
Layout.Alignment.ALIGN_NORMAL, 1.f, 0, true);
532548
}
533549

0 commit comments

Comments
 (0)