Loading core/java/android/widget/Switch.java +34 −8 Original line number Diff line number Diff line Loading @@ -90,6 +90,7 @@ public class Switch extends CompoundButton { private boolean mSplitTrack; private CharSequence mTextOn; private CharSequence mTextOff; private boolean mShowText; private int mTouchMode; private int mTouchSlop; Loading Loading @@ -188,6 +189,7 @@ public class Switch extends CompoundButton { mTrackDrawable = a.getDrawable(com.android.internal.R.styleable.Switch_track); mTextOn = a.getText(com.android.internal.R.styleable.Switch_textOn); mTextOff = a.getText(com.android.internal.R.styleable.Switch_textOff); mShowText = a.getBoolean(com.android.internal.R.styleable.Switch_showText, true); mThumbTextPadding = a.getDimensionPixelSize( com.android.internal.R.styleable.Switch_thumbTextPadding, 0); mSwitchMinWidth = a.getDimensionPixelSize( Loading Loading @@ -533,8 +535,30 @@ public class Switch extends CompoundButton { requestLayout(); } /** * Sets whether the on/off text should be displayed. * * @param showText {@code true} to display on/off text * @hide */ public void setShowText(boolean showText) { if (mShowText != showText) { mShowText = showText; requestLayout(); } } /** * @return whether the on/off text should be displayed * @hide */ public boolean getShowText() { return mShowText; } @Override public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { if (mShowText) { if (mOnLayout == null) { mOnLayout = makeLayout(mTextOn); } Loading @@ -542,6 +566,7 @@ public class Switch extends CompoundButton { if (mOffLayout == null) { mOffLayout = makeLayout(mTextOff); } } mTrackDrawable.getPadding(mTempRect); Loading @@ -568,9 +593,10 @@ public class Switch extends CompoundButton { @Override public void onPopulateAccessibilityEvent(AccessibilityEvent event) { super.onPopulateAccessibilityEvent(event); Layout layout = isChecked() ? mOnLayout : mOffLayout; if (layout != null && !TextUtils.isEmpty(layout.getText())) { event.getText().add(layout.getText()); final CharSequence text = isChecked() ? mTextOn : mTextOff; if (text != null) { event.getText().add(text); } } Loading core/res/res/values/attrs.xml +2 −0 Original line number Diff line number Diff line Loading @@ -6757,6 +6757,8 @@ <attr name="switchPadding" format="dimension" /> <!-- Whether to split the track and leave a gap for the thumb drawable. --> <attr name="splitTrack" /> <!-- Whether to draw on/off text. --> <attr name="showText" format="boolean" /> </declare-styleable> <declare-styleable name="Pointer"> Loading core/res/res/values/styles_material.xml +3 −2 Original line number Diff line number Diff line Loading @@ -493,11 +493,12 @@ please see styles_device_defaults.xml. <item name="thumb">@drawable/switch_thumb_material_anim</item> <item name="splitTrack">true</item> <item name="switchTextAppearance">@style/TextAppearance.Material.Widget.Switch</item> <item name="textOn"></item> <item name="textOff"></item> <item name="textOn">@string/capital_on</item> <item name="textOff">@string/capital_off</item> <item name="switchMinWidth">4dip</item> <item name="switchPadding">4dip</item> <item name="background">?attr/selectableItemBackgroundBorderless</item> <item name="showText">false</item> </style> <style name="Widget.Material.EditText" parent="Widget.EditText"/> Loading Loading
core/java/android/widget/Switch.java +34 −8 Original line number Diff line number Diff line Loading @@ -90,6 +90,7 @@ public class Switch extends CompoundButton { private boolean mSplitTrack; private CharSequence mTextOn; private CharSequence mTextOff; private boolean mShowText; private int mTouchMode; private int mTouchSlop; Loading Loading @@ -188,6 +189,7 @@ public class Switch extends CompoundButton { mTrackDrawable = a.getDrawable(com.android.internal.R.styleable.Switch_track); mTextOn = a.getText(com.android.internal.R.styleable.Switch_textOn); mTextOff = a.getText(com.android.internal.R.styleable.Switch_textOff); mShowText = a.getBoolean(com.android.internal.R.styleable.Switch_showText, true); mThumbTextPadding = a.getDimensionPixelSize( com.android.internal.R.styleable.Switch_thumbTextPadding, 0); mSwitchMinWidth = a.getDimensionPixelSize( Loading Loading @@ -533,8 +535,30 @@ public class Switch extends CompoundButton { requestLayout(); } /** * Sets whether the on/off text should be displayed. * * @param showText {@code true} to display on/off text * @hide */ public void setShowText(boolean showText) { if (mShowText != showText) { mShowText = showText; requestLayout(); } } /** * @return whether the on/off text should be displayed * @hide */ public boolean getShowText() { return mShowText; } @Override public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { if (mShowText) { if (mOnLayout == null) { mOnLayout = makeLayout(mTextOn); } Loading @@ -542,6 +566,7 @@ public class Switch extends CompoundButton { if (mOffLayout == null) { mOffLayout = makeLayout(mTextOff); } } mTrackDrawable.getPadding(mTempRect); Loading @@ -568,9 +593,10 @@ public class Switch extends CompoundButton { @Override public void onPopulateAccessibilityEvent(AccessibilityEvent event) { super.onPopulateAccessibilityEvent(event); Layout layout = isChecked() ? mOnLayout : mOffLayout; if (layout != null && !TextUtils.isEmpty(layout.getText())) { event.getText().add(layout.getText()); final CharSequence text = isChecked() ? mTextOn : mTextOff; if (text != null) { event.getText().add(text); } } Loading
core/res/res/values/attrs.xml +2 −0 Original line number Diff line number Diff line Loading @@ -6757,6 +6757,8 @@ <attr name="switchPadding" format="dimension" /> <!-- Whether to split the track and leave a gap for the thumb drawable. --> <attr name="splitTrack" /> <!-- Whether to draw on/off text. --> <attr name="showText" format="boolean" /> </declare-styleable> <declare-styleable name="Pointer"> Loading
core/res/res/values/styles_material.xml +3 −2 Original line number Diff line number Diff line Loading @@ -493,11 +493,12 @@ please see styles_device_defaults.xml. <item name="thumb">@drawable/switch_thumb_material_anim</item> <item name="splitTrack">true</item> <item name="switchTextAppearance">@style/TextAppearance.Material.Widget.Switch</item> <item name="textOn"></item> <item name="textOff"></item> <item name="textOn">@string/capital_on</item> <item name="textOff">@string/capital_off</item> <item name="switchMinWidth">4dip</item> <item name="switchPadding">4dip</item> <item name="background">?attr/selectableItemBackgroundBorderless</item> <item name="showText">false</item> </style> <style name="Widget.Material.EditText" parent="Widget.EditText"/> Loading