Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change textcolor of switch in Android

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!

like image 657
Robin.v Avatar asked Oct 03 '12 10:10

Robin.v


People also ask

How do I change the text color on my Android?

Open your device's Settings app . Text and display. Select Color correction. Turn on Use color correction.

How can I change the font color of my Android app name?

Search for and select Open App, and then, on the New Shortcut page, tap Choose. Locate the app whose appearance you want to change. Back on the New Shortcut page, you'll see the app name; tap More (three dots), change the app's name, tap its icon, select Color, and choose a new color.

How do I change the color of my text on Kotlin?

Change Text Color of TextView in Kotlin File We can get the reference to TextView widget present in layout file and change the color dynamically with Kotlin code. To set the color to the TextView widget, call setTextColor() method on the TextView widget reference with specific color passed as argument.


2 Answers

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();
        }
    }
like image 188
imbryk Avatar answered Sep 28 '22 21:09

imbryk


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.

like image 23
ndsmyter Avatar answered Sep 28 '22 22:09

ndsmyter