How to change textcolor of switch in Android
Asked Answered
B

3

19

I'm creating an application which uses Android 4.0. I'm wondering if it is possible to change the text color of the text in a switch.

I've tried setting the text color, but it doesn't work.

Any ideas?

Thanks in advance!

Benilda answered 3/10, 2012 at 10:46 Comment(1)
There's no specific code, its just a switch in the layout, which is found by ID. And in the code i set the switch.setTextColor(Color.WHITE);Benilda
I
67

You must use android:switchTextAppearance attribute, eg:

android:switchTextAppearance="@style/SwitchTextAppearance"

and in styles:

<style name="SwitchTextAppearance" parent="@android:style/TextAppearance.Holo.Small">
    <item name="android:textColor">@color/my_switch_color</item>
</style>

you can also do it in code, also using above styles:

mySwitch.setSwitchTextAppearance(getActivity(), R.style.SwitchTextAppearance);

...and as for setTextColor and Switch - this color will be used if your SwitchTextAppearance style doesn't provide a textColor

you can check it in Switch source code in setSwitchTextAppearance:

    ColorStateList colors;
    int ts;

    colors = appearance.getColorStateList(com.android.internal.R.styleable.
            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.
            TextAppearance_textSize, 0);
    if (ts != 0) {
        if (ts != mTextPaint.getTextSize()) {
            mTextPaint.setTextSize(ts);
            requestLayout();
        }
    }
Instructions answered 29/3, 2013 at 9:26 Comment(1)
I wonder why couldn't they just make setTextColor work normally instead of all this bloat.Thigpen
G
1

I think you have to look at the theme which you are using for your application. Because the color of the switch is the responsibility of the theme, afaik. So I would suggest you have a look on how you can change the settings of a theme. Or you could create a custom theme with the new colors.

Gnathion answered 3/10, 2012 at 12:44 Comment(0)
O
0

TextView.setTextColor() takes an int representing the color (eg. 0xFFF5DC49) not the resource id from the xml file. In an activity, you can do something like:

textView1.setTextColor(getResources().getColor(R.color.mycolor))

outside of an activity you'll need a Context eg.

textView1.setTextColor(context.getResources().getColor(R.color.mycolor))

For more refer this

Orren answered 3/10, 2012 at 10:53 Comment(2)
Hello, first thanks for your response! This isn't the answer that I'm looking for, i got it working for textviews and editTexts. But I need it for a switch and I'm not sure how..Benilda
Actually setTextColor is not used for Switch, unless the style provided for android:switchTextAppearance doesn't define a textColorInstructions

© 2022 - 2024 — McMap. All rights reserved.