From 638b2c53f00f552693cc8f425eaa862343e6cf20 Mon Sep 17 00:00:00 2001 From: cyBerta Date: Tue, 23 Nov 2021 15:42:41 +0100 Subject: add OnCheckedChangedListener in SimpleCheckbox --- .../bitmaskclient/base/views/SimpleCheckBox.java | 25 +++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'app/src/main/java/se') diff --git a/app/src/main/java/se/leap/bitmaskclient/base/views/SimpleCheckBox.java b/app/src/main/java/se/leap/bitmaskclient/base/views/SimpleCheckBox.java index 7cd790db..09a9132e 100644 --- a/app/src/main/java/se/leap/bitmaskclient/base/views/SimpleCheckBox.java +++ b/app/src/main/java/se/leap/bitmaskclient/base/views/SimpleCheckBox.java @@ -9,11 +9,19 @@ 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 checkedChangeListener = new WeakReference(null); + private boolean checked; + + public interface OnCheckedChangeListener { + void onCheckedChanged(SimpleCheckBox simpleCheckBox, boolean isChecked); + } public SimpleCheckBox(Context context) { @@ -45,6 +53,21 @@ public class SimpleCheckBox extends RelativeLayout { } public void setChecked(boolean checked) { - this.checkView.setVisibility(checked ? VISIBLE : INVISIBLE); + 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); } } -- cgit v1.2.3