Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 890a631e authored by Chet Haase's avatar Chet Haase Committed by Android (Google) Code Review
Browse files

Merge "Made the Switch class public."

parents f8c015d2 150176d8
Loading
Loading
Loading
Loading
+78 −59

File changed.

Preview size limit exceeded, changes collapsed.

+26 −22
Original line number Diff line number Diff line
@@ -41,9 +41,15 @@ import com.android.internal.R;
/**
 * A Switch is a two-state toggle switch widget that can select between two
 * options. The user may drag the "thumb" back and forth to choose the selected option,
 * or simply tap to toggle as if it were a checkbox.
 * or simply tap to toggle as if it were a checkbox. The {@link #setText(CharSequence) text}
 * property controls the text displayed in the label for the switch, whereas the
 * {@link #setTextOff(CharSequence) off} and {@link #setTextOn(CharSequence) on} text
 * controls the text on the thumb. Similarly, the
 * {@link #setTextAppearance(android.content.Context, int) textAppearance} and the related
 * setTypeface() methods control the typeface and style of label text, whereas the
 * {@link #setSwitchTextAppearance(android.content.Context, int) switchTextAppearance} and
 * the related seSwitchTypeface() methods control that of the thumb.
 *
 * @hide
 */
public class Switch extends CompoundButton {
    private static final int TOUCH_MODE_IDLE = 0;
@@ -132,8 +138,8 @@ public class Switch extends CompoundButton {
        TypedArray a = context.obtainStyledAttributes(attrs,
                com.android.internal.R.styleable.Switch, defStyle, 0);

        mThumbDrawable = a.getDrawable(com.android.internal.R.styleable.Switch_switchThumb);
        mTrackDrawable = a.getDrawable(com.android.internal.R.styleable.Switch_switchTrack);
        mThumbDrawable = a.getDrawable(com.android.internal.R.styleable.Switch_thumb);
        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);
        mThumbTextPadding = a.getDimensionPixelSize(
@@ -146,7 +152,7 @@ public class Switch extends CompoundButton {
        int appearance = a.getResourceId(
                com.android.internal.R.styleable.Switch_switchTextAppearance, 0);
        if (appearance != 0) {
            setSwitchTextAppearance(appearance);
            setSwitchTextAppearance(context, appearance);
        }
        a.recycle();

@@ -162,9 +168,9 @@ public class Switch extends CompoundButton {
     * Sets the switch text color, size, style, hint color, and highlight color
     * from the specified TextAppearance resource.
     */
    public void setSwitchTextAppearance(int resid) {
    public void setSwitchTextAppearance(Context context, int resid) {
        TypedArray appearance =
                getContext().obtainStyledAttributes(resid,
                context.obtainStyledAttributes(resid,
                        com.android.internal.R.styleable.TextAppearance);

        ColorStateList colors;
@@ -174,6 +180,9 @@ public class Switch extends CompoundButton {
                TextAppearance_textColor);
        if (colors != null) {
            mTextColors = colors;
        } else {
            // If no color set in TextAppearance, default to the view's textColor
            mTextColors = getTextColors();
        }

        ts = appearance.getDimensionPixelSize(com.android.internal.R.styleable.
@@ -244,7 +253,7 @@ public class Switch extends CompoundButton {
    }

    /**
     * Sets the typeface and style in which the text should be displayed on the switch.
     * Sets the typeface in which the text should be displayed on the switch.
     * Note that not all Typeface families actually have bold and italic
     * variants, so you may need to use
     * {@link #setSwitchTypeface(Typeface, int)} to get the appearance
@@ -263,18 +272,14 @@ public class Switch extends CompoundButton {
    }

    /**
     * Returns the text for when the button is in the checked state.
     *
     * @return The text.
     * Returns the text displayed when the button is in the checked state.
     */
    public CharSequence getTextOn() {
        return mTextOn;
    }

    /**
     * Sets the text for when the button is in the checked state.
     *
     * @param textOn The text.
     * Sets the text displayed when the button is in the checked state.
     */
    public void setTextOn(CharSequence textOn) {
        mTextOn = textOn;
@@ -282,18 +287,14 @@ public class Switch extends CompoundButton {
    }

    /**
     * Returns the text for when the button is not in the checked state.
     *
     * @return The text.
     * Returns the text displayed when the button is not in the checked state.
     */
    public CharSequence getTextOff() {
        return mTextOff;
    }

    /**
     * Sets the text for when the button is not in the checked state.
     *
     * @param textOff The text.
     * Sets the text displayed when the button is not in the checked state.
     */
    public void setTextOff(CharSequence textOff) {
        mTextOff = textOff;
@@ -582,8 +583,11 @@ public class Switch extends CompoundButton {
        mThumbDrawable.setBounds(thumbLeft, switchTop, thumbRight, switchBottom);
        mThumbDrawable.draw(canvas);

        // mTextColors should not be null, but just in case
        if (mTextColors != null) {
            mTextPaint.setColor(mTextColors.getColorForState(getDrawableState(),
                    mTextColors.getDefaultColor()));
        }
        mTextPaint.drawableState = getDrawableState();

        Layout switchText = getTargetCheckedState() ? mOnLayout : mOffLayout;
+2 −2
Original line number Diff line number Diff line
@@ -5318,9 +5318,9 @@

    <declare-styleable name="Switch">
        <!-- Drawable to use as the "thumb" that switches back and forth. -->
        <attr name="switchThumb" format="reference" />
        <attr name="thumb" />
        <!-- Drawable to use as the "track" that the switch thumb slides within. -->
        <attr name="switchTrack" format="reference" />
        <attr name="track" format="reference" />
        <!-- Text to use when the switch is in the checked/"on" state. -->
        <attr name="textOn" />
        <!-- Text to use when the switch is in the unchecked/"off" state. -->
+5 −0
Original line number Diff line number Diff line
@@ -1713,6 +1713,11 @@
  <public type="attr" name="switchTextOn" />
  <public type="attr" name="switchTextOff" />
  <public type="attr" name="switchPreferenceStyle" />
  <public type="attr" name="switchTextAppearance" />
  <public type="attr" name="track" />
  <public type="attr" name="switchMinWidth" />
  <public type="attr" name="switchPadding" />
  <public type="attr" name="thumbTextPadding" />

  <public type="attr" name="textSuggestionsWindowStyle" />
  <public type="attr" name="textEditSuggestionItemLayout" />
+18 −2
Original line number Diff line number Diff line
@@ -725,6 +725,11 @@ please see styles_device_defaults.xml.
        <item name="android:quickContactWindowSize">modeLarge</item>
    </style>

    <style name="Widget.CompoundButton.Switch">
        <item name="android:textOn">@android:string/capital_on</item>
        <item name="android:textOff">@android:string/capital_off</item>
    </style>

    <!-- Text Appearances -->
    <eat-comment />

@@ -1919,8 +1924,8 @@ please see styles_device_defaults.xml.
    </style>

    <style name="Widget.Holo.CompoundButton.Switch">
        <item name="android:switchTrack">@android:drawable/switch_track_holo_dark</item>
        <item name="android:switchThumb">@android:drawable/switch_inner_holo_dark</item>
        <item name="android:track">@android:drawable/switch_track_holo_dark</item>
        <item name="android:thumb">@android:drawable/switch_inner_holo_dark</item>
        <item name="android:switchTextAppearance">@android:style/TextAppearance.Holo.Widget.Switch</item>
        <item name="android:textOn">@android:string/capital_on</item>
        <item name="android:textOff">@android:string/capital_off</item>
@@ -2293,6 +2298,17 @@ please see styles_device_defaults.xml.
        <item name="android:itemPadding">8dip</item>
    </style>

    <style name="Widget.Holo.Light.CompoundButton.Switch" parent="Widget.CompoundButton.Switch">
        <item name="android:track">@android:drawable/switch_track_holo_light</item>
        <item name="android:thumb">@android:drawable/switch_inner_holo_light</item>
        <item name="android:switchTextAppearance">@android:style/TextAppearance.Holo.Widget.Switch</item>
        <item name="android:textOn">@android:string/capital_on</item>
        <item name="android:textOff">@android:string/capital_off</item>
        <item name="android:thumbTextPadding">12dip</item>
        <item name="android:switchMinWidth">96dip</item>
        <item name="android:switchPadding">16dip</item>
    </style>

    <!-- Animation Styles -->

    <style name="Animation.Holo" parent="Animation">
Loading