aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/net/lacolaco/smileessence/view/ThreeStateButton.java
blob: d5358db5d80200b9a0d4bed6839e2b98e917aff3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package net.lacolaco.smileessence.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.Button;
import net.lacolaco.smileessence.R;

public class ThreeStateButton extends Button {
    public static final int STATE_OFF = 0;
    public static final int STATE_ON = 1;
    public static final int STATE_LOCKED = 2;

    private int state = STATE_OFF;
    private String texts[] = new String[3];
    private Drawable backgrounds[] = new Drawable[3];

    public ThreeStateButton(Context context) {
        this(context, null);
    }

    public ThreeStateButton(Context context, AttributeSet attrs) {
        this(context, attrs, android.R.attr.buttonStyle);
    }

    public ThreeStateButton(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public ThreeStateButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);

        TypedArray 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();

        setState(STATE_OFF);
    }

    public int getState() {
        return state;
    }

    public void setState(int s) {
        state = s;
        setText(texts[state]);
        setBackground(backgrounds[state]);
        setEnabled(state != STATE_LOCKED);
    }
}