How to set the color to a switch in appcelerator?

392 Views Asked by At

I tried with On tint color, color, tint color to change the color of the switch. It isn't working either way.

1

There are 1 best solutions below

8
miga On BEST ANSWER

For Android you can use this theme to set a track and thumb tint color:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="MyThemeActionbar" parent="Theme.AppCompat">
        <!-- switch -->
        <item name="switchStyle">@style/newSwitchStyle</item>
    </style>


    <style name="newSwitchStyle" parent="android:Widget.CompoundButton">
        <item name="track">@drawable/abc_switch_track_mtrl_alpha</item>
        <item name="android:thumb">@drawable/abc_switch_thumb_material</item>
        <item name="switchTextAppearance">@style/TextAppearance.AppCompat.Widget.Switch</item>
        <item name="android:background">?attr/controlBackground</item>
        <item name="showText">false</item>
        <item name="switchPadding">@dimen/abc_switch_padding</item>
        <item name="android:textOn">@string/abc_capital_on</item>
        <item name="android:textOff">@string/abc_capital_off</item>

        <!-- track and button -->
        <item name="trackTint">#00f</item>
        <item name="thumbTint">#f00</item>
    </style>
</resources>
  • Create a file platform/android/res/values/mytheme.xml with the content above
  • apply the theme with e.g. var win = Ti.UI.createWindow({theme: "Theme.MyThemeActionbar",}); or
  • set it globally in the tiapp.xml:

<android xmlns:android="http://schemas.android.com/apk/res/android">
    <manifest>
        <application android:theme="@style/Theme.MyThemeActionbar"/>
    </manifest>
</android>

Check: http://docs.appcelerator.com/platform/latest/#!/guide/Android_Themes for applying/adding a theme