package net.lacolaco.smileessence.view import android.content.Context import android.graphics.drawable.Drawable import android.support.v7.widget.AppCompatButton import android.util.AttributeSet import net.lacolaco.smileessence.R class ThreeStateButton @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = android.R.attr.buttonStyle) : AppCompatButton(context, attrs, defStyleAttr) { var state = STATE_OFF set(s) { field = s text = texts[this.state] background = backgrounds[this.state] isEnabled = this.state != STATE_LOCKED } private val texts = arrayOfNulls(3) private val backgrounds = arrayOfNulls(3) init { val ta = context.obtainStyledAttributes(attrs, R.styleable.ThreeStateButton) texts[STATE_OFF] = ta.getString(R.styleable.ThreeStateButton_off_text) texts[STATE_ON] = ta.getString(R.styleable.ThreeStateButton_on_text) texts[STATE_LOCKED] = ta.getString(R.styleable.ThreeStateButton_locked_text) backgrounds[STATE_OFF] = ta.getDrawable(R.styleable.ThreeStateButton_off_background) backgrounds[STATE_ON] = ta.getDrawable(R.styleable.ThreeStateButton_on_background) backgrounds[STATE_LOCKED] = ta.getDrawable(R.styleable.ThreeStateButton_locked_background) ta.recycle() state = STATE_OFF } companion object { val STATE_OFF = 0 val STATE_ON = 1 val STATE_LOCKED = 2 } }