summaryrefslogtreecommitdiff
path: root/app/src/main/java/se/leap/bitmaskclient/base/views/SimpleCheckBox.java
blob: 09a9132e99b1eaf3526cb424dae7cbfa31959a1b (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package se.leap.bitmaskclient.base.views;

import android.annotation.TargetApi;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;

import androidx.appcompat.widget.AppCompatImageView;

import java.lang.ref.WeakReference;

import se.leap.bitmaskclient.R;

public class SimpleCheckBox extends RelativeLayout {

    AppCompatImageView checkView;
    private WeakReference<OnCheckedChangeListener> checkedChangeListener = new WeakReference<OnCheckedChangeListener>(null);
    private boolean checked;

    public interface OnCheckedChangeListener {
        void onCheckedChanged(SimpleCheckBox simpleCheckBox, boolean isChecked);
    }


    public SimpleCheckBox(Context context) {
        super(context);
        initLayout(context);
    }

    public SimpleCheckBox(Context context, AttributeSet attrs) {
        super(context, attrs);
        initLayout(context);
    }

    public SimpleCheckBox(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initLayout(context);
    }

    @TargetApi(21)
    public SimpleCheckBox(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initLayout(context);
    }

    private void initLayout(Context context) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rootview = inflater.inflate(R.layout.v_simple_checkbox, this, true);
        this.checkView = rootview.findViewById(R.id.check_view);
    }

    public void setChecked(boolean checked) {
        if (this.checked != checked) {
            this.checkView.setVisibility(checked ? VISIBLE : INVISIBLE);
            this.checked = checked;
            OnCheckedChangeListener listener = checkedChangeListener.get();
            if (listener != null) {
                listener.onCheckedChanged(this, this.checked);
            }
        }
    }

    public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {
        checkedChangeListener = new WeakReference<>(listener);
    }

    public void toggle() {
        setChecked(!this.checked);
    }
}