Skip to content

CustomSwitchView

jimbo edited this page May 29, 2026 · 1 revision

CustomSwitchView

์š”์•ฝ (Summary)

  • ๊ฐ„๋‹จํ•œ ์†์„ฑ๊ฐ’์œผ๋กœ Apple(iOS) ์Šคํƒ€์ผ์˜ ํ† ๊ธ€ ์Šค์œ„์น˜๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” ViewClass ์ž…๋‹ˆ๋‹ค.
    (A toggle switch View that replicates the iOS UISwitch style using simple attribute values.)

์ง€์›ํ•˜๋Š” ๊ธฐ๋Šฅ (Supported Features)

Attribute Type Description
switchChecked Boolean Sets the initial checked state of the switch.
switchTrackColorOn Color Track color when the switch is ON.
switchTrackColorOff Color Track color when the switch is OFF.
switchThumbColor Color Color of the thumb (circle).
switchThumbSize Dimension Diameter of the thumb. If not set, auto-calculated from track height.
switchThumbMargin Dimension Margin between the thumb and the track edge. Default is 2dp.
switchAnimDuration Integer Duration of the toggle animation in milliseconds. Default is 250ms.
switchTouchPadding Dimension Expands the touch area via TouchDelegate without changing the visual size.

์œ ์˜ ์‚ฌํ•ญ (Notice)

  • switchTouchPadding ์€ ๋ถ€๋ชจ View ์— TouchDelegate ๋ฅผ ์„ค์ •ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.
    ๋ถ€๋ชจ View ๊ฐ€ ์ด๋ฏธ TouchDelegate ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š” ๊ฒฝ์šฐ ๋ฎ์–ด์“ฐ๊ธฐ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    (switchTouchPadding works by setting a TouchDelegate on the parent View. If the parent already uses a TouchDelegate, it may be overwritten.)

  • switchThumbSize ๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด track ๋†’์ด ๊ธฐ์ค€์œผ๋กœ thumb ํฌ๊ธฐ๊ฐ€ ์ž๋™ ๊ณ„์‚ฐ๋ฉ๋‹ˆ๋‹ค.
    (If switchThumbSize is not set, the thumb size is automatically calculated based on the track height.)

์‚ฌ์šฉ๋ฒ• (How To)

  • xml
<hmju.widget.view.CustomSwitchView
    android:id="@+id/vSwitch"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:switchChecked="false"
    app:switchTrackColorOn="#00A18E"
    app:switchTrackColorOff="#8E9292"
    app:switchThumbColor="#FFFFFF"
    app:switchThumbSize="12dp"
    app:switchThumbMargin="4dp"
    app:switchAnimDuration="400"
    app:switchTouchPadding="16dp" />
  • kotlin
// ์ƒํƒœ ๋ณ€๊ฒฝ ์ฝœ๋ฐฑ
vSwitch.setOnCheckedChangeListener { isChecked ->
    if (isChecked) {
        // ON ์ฒ˜๋ฆฌ
    } else {
        // OFF ์ฒ˜๋ฆฌ
    }
}

// ์ฝ”๋“œ์—์„œ ์ƒํƒœ ๋ณ€๊ฒฝ
vSwitch.isChecked = true

Gradle

implementation 'com.github.sieunju.widget:view:$latestVersion'

Clone this wiki locally