การปรับแต่งรูปลักษณ์ Switch Widget ใน Android ด้วย Drawable XML
ตัวอย่างนี้สาธิตการสร้าง Custom Switch โดยใช้ Drawable Selector สำหรับ Thumb และ Track เพื่อเปลี่ยนสีและรูปร่างตามสถานะ ON/OFF
ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่
ไปที่ File → New Project ใน Android Studio
ขั้นตอนที่ 2: Layout (activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="4dp"
android:id="@+id/relativeLayout"
tools:context=".MainActivity">
<Switch android:id="@+id/switchBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="ON"
android:thumb="@drawable/customswitchselector"
android:track="@drawable/custom_track"
android:layout_centerInParent="true"
android:textOff="OFF"/>
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/switchBtn"
android:layout_marginTop="20dp"
android:text="Switch ON and OFF"
android:textSize="24sp"
android:textAlignment="center"
android:textStyle="bold"/>
</RelativeLayout>
ขั้นตอนที่ 3: Drawable Thumb (customswitchselector.xml)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="https://schemas.android.com/apk/res/android">
<item android:state_checked="true">
<shape android:dither="true" android:shape="rectangle"
android:useLevel="false" android:visible="true">
<corners android:radius="15dp" />
<gradient android:angle="270" android:endColor="#6600FF00" android:startColor="#66AAFF00" />
<size android:width="37dp" android:height="37dp" />
<stroke android:width="4dp" android:color="#0000ffff" />
</shape>
</item>
<item android:state_checked="false">
<shape android:dither="true" android:shape="rectangle"
android:useLevel="false" android:visible="true">
<corners android:radius="15dp" />
<gradient android:angle="270" android:endColor="#ff0000" android:startColor="#ff0000" />
<size android:width="37dp" android:height="37dp" />
<stroke android:width="4dp" android:color="#0000ffff" />
</shape>
</item>
</selector>
ขั้นตอนที่ 4: Drawable Track (custom_track.xml)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:visible="true"
android:dither="true"
android:useLevel="false">
<gradient
android:startColor="#27170432"
android:endColor="#27170432"
android:angle="270"/>
<corners android:radius="15dp"/>
<size android:width="80dp" android:height="40dp" />
</shape>
ขั้นตอนที่ 5: โค้ดหลัก (MainActivity.java)
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Switch aSwitch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
aSwitch = findViewById(R.id.switchBtn);
aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
Toast.makeText(MainActivity.this, "Switch On", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Switch Off", Toast.LENGTH_SHORT).show();
}
}
});
}
}
ขั้นตอนที่ 6: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android" package="app.com.sample">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
💡 วิธีปรับแต่งแบบง่ายกว่า (Material Components)
หากใช้ Material Components Theme สามารถปรับสีได้ผ่าน Theme Attributes โดยไม่ต้องสร้าง Drawable:
<!-- ใน styles.xml -->
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<item name="colorPrimary">#6200EE</item>
<item name="colorSecondary">#03DAC5</item>
<item name="colorOnPrimary">#FFFFFF</item>
<item name="colorOnSecondary">#000000</item>
</style>
หรือกำหนดต่อ Widget โดยตรง:
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="@style/CustomSwitchTheme" />
<style name="CustomSwitchTheme">
<item name="colorControlActivated">#4CAF50</item> <!-- Track ON -->
<item name="colorSwitchThumbNormal">#FF0000</item> <!-- Thumb OFF -->
<item name="android:colorForeground">#4CAF50</item> <!-- Thumb ON -->
</style>
การทดสอบ
รันแอป สไลด์ Switch จะเห็น Thumb เปลี่ยนสี: เขียว (ON) / แดง (OFF) พร้อม Track สีเทา และ Toast แสดงสถานะ