6 package com.google.appinventor.components.runtime;
8 import android.app.Activity;
9 import android.content.res.ColorStateList;
10 import android.widget.CheckBox;
11 import android.widget.CompoundButton;
12 import androidx.appcompat.widget.SwitchCompat;
13 import androidx.core.graphics.drawable.DrawableCompat;
32 @DesignerComponent(version = YaVersion.SWITCH_COMPONENT_VERSION,
33 description =
"Toggle switch that raises an event when the user clicks on it. " +
34 "There are many properties affecting its appearance that can be set in " +
35 "the Designer or Blocks Editor.",
36 category = ComponentCategory.USERINTERFACE)
41 private int thumbColorActive;
42 private int thumbColorInactive;
45 private int trackColorActive;
46 private int trackColorInactive;
48 private final Activity activity;
49 private final SwitchCompat switchView;
59 this.activity = container.
$context();
67 switchView =
new SwitchCompat(activity);
80 private ColorStateList createSwitchColors(
int active_color,
int inactive_color) {
81 return new ColorStateList(
new int[][]{
82 new int[]{android.R.attr.state_checked},
97 @SimpleProperty(category = PropertyCategory.APPEARANCE)
99 return thumbColorActive;
111 thumbColorActive = argb;
112 if (switchView !=
null) {
113 DrawableCompat.setTintList(switchView.getThumbDrawable(), createSwitchColors(argb, thumbColorInactive));
126 return thumbColorInactive;
138 thumbColorInactive = argb;
139 if (switchView !=
null) {
140 DrawableCompat.setTintList(switchView.getThumbDrawable(), createSwitchColors(thumbColorActive, argb));
152 return trackColorActive;
156 return trackColorInactive;
166 @
SimpleProperty(description =
"Color of the toggle track when switched on", userVisible =
true)
168 trackColorActive = argb;
169 if (switchView !=
null) {
170 DrawableCompat.setTintList(switchView.getTrackDrawable(), createSwitchColors(argb, trackColorInactive));
181 @
SimpleProperty(description =
"Color of the toggle track when switched off", userVisible =
true)
183 trackColorInactive = argb;
184 if (switchView !=
null) {
185 DrawableCompat.setTintList(switchView.getTrackDrawable(), createSwitchColors(trackColorActive, argb));
197 public
boolean On() {
198 return view.isChecked();
210 defaultValue =
"False")
212 public void On(
boolean value) {
213 view.setChecked(value);
218 @
SimpleEvent(description =
"User change the state of the `Switch` from On to Off or back.")