aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/net/lacolaco/smileessence/view/ToggleableImageButton.kt
blob: f83bcf56b14f6a2957ce0c90321d2fec2e6b1dfa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package net.lacolaco.smileessence.view

import android.content.Context
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.widget.ImageButton
import net.lacolaco.smileessence.R

class ToggleableImageButton @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = android.R.attr.imageButtonStyle, defStyleRes: Int = 0) : ImageButton(context, attrs, defStyleAttr, defStyleRes) {
    private val onSrc: Drawable
    private val offSrc: Drawable

    init {
        val ta = context.obtainStyledAttributes(attrs, R.styleable.ToggleableImageButton)
        offSrc = ta.getDrawable(R.styleable.ToggleableImageButton_offSrc)
        onSrc = ta.getDrawable(R.styleable.ToggleableImageButton_onSrc)
        ta.recycle()
    }

    fun setState(isOn: Boolean) {
        setImageDrawable(if (isOn) onSrc else offSrc)
    }
}