aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/net/lacolaco/smileessence/view/ToggleableImageButton.java
blob: b7b2d1748a36525871d6178e30c3cd5230bd2688 (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
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.ImageButton;
import net.lacolaco.smileessence.R;

public class ToggleableImageButton extends ImageButton {
    private Drawable onSrc, offSrc;

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

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

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

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

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ToggleableImageButton);
        offSrc = ta.getDrawable(R.styleable.ToggleableImageButton_offSrc);
        onSrc = ta.getDrawable(R.styleable.ToggleableImageButton_onSrc);
        ta.recycle();
    }

    public void setState(boolean isOn) {
        setImageDrawable(isOn ? onSrc : offSrc);
    }
}