พื้นฐาน android - style.xml (v-21)

appCompat Lib different attributs

colorPrimary: The primary branding color for the app. By default, this is the color applied to the action bar background.
colorPrimaryDark: Dark variant of the primary branding color. By default, this is the color applied to the status bar (via statusBarColor) and navigation bar (via navigationBarColor).
colorAccent: Bright complement to the primary branding color. By default, this is the color applied to framework controls (via colorControlActivated).
colorControlNormal: The color applied to framework controls in their normal state.
colorControlActivated: The color applied to framework controls in their activated (ex. checked, switch on) state.
colorControlHighlight: The color applied to framework control highlights (ex. ripples, list selectors).
colorButtonNormal: The color applied to framework buttons in their normal state.
colorSwitchThumbNormal: The color applied to framework switch thumbs in their normal state. (switch off)


วิธีนำมาใช้แบบแรก..แบบ Gobal

<style name="Theme.MyActivityTheme" parent="Theme.AppCompat.Light">
    <!-- colorPrimary is used for the default action bar background -->
    <item name="colorPrimary">@color/my_awesome_color</item>
    <!-- colorPrimaryDark is used for the status bar -->
    <item name="colorPrimaryDark">@color/my_awesome_darker_color</item>
    <!-- colorAccent is used as the default value for colorControlActivatedwhich is used to tint widgets -->
    <item name="colorAccent">@color/accent</item>

    // การกำหนดสีจะอยู่ตรงนี้
    <!-- etc. colorControlNormal, colorControlActivated , colorControlHighlight, and colorSwitchThumbNormal. -->
</style>

manifest.xml
<manifest>
    <activity
        android:name=".MainActivity" 
        android:theme="@style/Theme.MyActivityTheme">
    </activity>
</manifest>


พอเราเพิ่ม colorControlActivated ลงไป

<item name="colorControlActivated ">@color/accent</item>

จะมีแจ้งเตือนให้สร้างเป็น style.xml (v-21)
หมายถึงใช้ได้เฉพาะ android รุ่นที่ api เวอร์ชั่น 21 หรือมากกว่า
พอสร้าง style.xml (v-21) มาได้แล้ว ลองกำหนด Style แบบเจาะจง switch

activity_main.xml
<LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto">

    <Switch
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:theme="@style/switchStyle"/>

style.xml (v-21)
<style name="switchStyle">
    <item name="colorControlActivated">@color/your_color</item>
</style>

เสร็จแล้วลองรันแอปเวลา Enable switch จะได้เป็นสีตามที่ตั้งไว้แล้วครับ


ref :
https://stackoverflow.com/questions/26714864/how-to-change-the-color-of-a-switchcompat-from-appcompat-library
https://android-developers.googleblog.com/2014/10/appcompat-v21-material-design-for-pre.html