AI2 Component  (Version nb184)
Switch.java
Go to the documentation of this file.
1 // -*- mode: java; c-basic-offset: 2; -*-
2 // Copyright 2018-2020 MIT, All rights reserved
3 // Released under the Apache License, Version 2.0
4 // http://www.apache.org/licenses/LICENSE-2.0
5 
6 package com.google.appinventor.components.runtime;
7 
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;
24 
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)
37 @SimpleObject
38 public final class Switch extends ToggleBase<CompoundButton> {
39 
40  // Backing for thumb color
41  private int thumbColorActive;
42  private int thumbColorInactive;
43 
44  // Backing for track color
45  private int trackColorActive;
46  private int trackColorInactive;
47 
48  private final Activity activity;
49  private final SwitchCompat switchView;
50 
56  public Switch(ComponentContainer container) {
57  super(container);
58 
59  this.activity = container.$context();
60 
61  // Using AppCompat, Switch component is only supported in API 14 and higher
62  // TODO: If we bump minSDK to 14, then we can change this to only use SwitchCompat.
64  switchView = null;
65  view = new CheckBox(activity);
66  } else {
67  switchView = new SwitchCompat(activity);
68  view = switchView;
69  }
70 
71  On(false);
72 
77  initToggle();
78  }
79 
80  private ColorStateList createSwitchColors(int active_color, int inactive_color) {
81  return new ColorStateList(new int[][]{
82  new int[]{android.R.attr.state_checked},
83  new int[]{}
84  },
85  new int[]{
86  active_color,
87  inactive_color
88  });
89  }
90 
97  @SimpleProperty(category = PropertyCategory.APPEARANCE)
98  public int ThumbColorActive() {
99  return thumbColorActive;
100  }
101 
108  defaultValue = Component.DEFAULT_VALUE_COLOR_WHITE)
110  public void ThumbColorActive(int argb) {
111  thumbColorActive = argb;
112  if (switchView != null) {
113  DrawableCompat.setTintList(switchView.getThumbDrawable(), createSwitchColors(argb, thumbColorInactive));
114  view.invalidate();
115  }
116  }
117 
124  @SimpleProperty(category = PropertyCategory.APPEARANCE, userVisible = true)
125  public int ThumbColorInactive() {
126  return thumbColorInactive;
127  }
128 
135  defaultValue = Component.DEFAULT_VALUE_COLOR_LTGRAY)
137  public void ThumbColorInactive(int argb) {
138  thumbColorInactive = argb;
139  if (switchView != null) {
140  DrawableCompat.setTintList(switchView.getThumbDrawable(), createSwitchColors(thumbColorActive, argb));
141  view.invalidate();
142  }
143  }
144 
150  @SimpleProperty(category = PropertyCategory.APPEARANCE, userVisible = true)
151  public int TrackColorActive() {
152  return trackColorActive;
153  }
154  @SimpleProperty(category = PropertyCategory.APPEARANCE, userVisible = true)
155  public int TrackColorInactive() {
156  return trackColorInactive;
157  }
158 
165  defaultValue = Component.DEFAULT_VALUE_COLOR_GREEN)
166  @SimpleProperty(description = "Color of the toggle track when switched on", userVisible = true)
167  public void TrackColorActive(int argb) {
168  trackColorActive = argb;
169  if (switchView != null) {
170  DrawableCompat.setTintList(switchView.getTrackDrawable(), createSwitchColors(argb, trackColorInactive));
171  view.invalidate();
172  }
173  }
174 
180  defaultValue = Component.DEFAULT_VALUE_COLOR_DKGRAY)
181  @SimpleProperty(description = "Color of the toggle track when switched off", userVisible = true)
182  public void TrackColorInactive(int argb) {
183  trackColorInactive = argb;
184  if (switchView != null) {
185  DrawableCompat.setTintList(switchView.getTrackDrawable(), createSwitchColors(trackColorActive, argb));
186  view.invalidate();
187  }
188  }
189 
196  category = PropertyCategory.BEHAVIOR)
197  public boolean On() {
198  return view.isChecked();
199  }
200 
210  defaultValue = "False")
212  public void On(boolean value) {
213  view.setChecked(value);
214  view.invalidate();
215  }
216 
217  @Override
218  @SimpleEvent(description = "User change the state of the `Switch` from On to Off or back.")
219  public void Changed() {
220  super.Changed();
221  }
222 
223 }
com.google.appinventor.components.runtime.Switch.TrackColorInactive
int TrackColorInactive()
Definition: Switch.java:155
com.google.appinventor.components.runtime.Component.COLOR_GREEN
static final int COLOR_GREEN
Definition: Component.java:60
com.google.appinventor.components.runtime.Switch.On
boolean On()
Definition: Switch.java:197
com.google.appinventor.components.runtime.util
-*- mode: java; c-basic-offset: 2; -*-
Definition: AccountChooser.java:7
com.google.appinventor.components.common.YaVersion
Definition: YaVersion.java:14
com.google.appinventor.components.annotations.DesignerProperty
Definition: DesignerProperty.java:25
com.google.appinventor.components.runtime.Switch
Definition: Switch.java:38
com.google.appinventor.components
com.google.appinventor.components.runtime.Component.DEFAULT_VALUE_COLOR_WHITE
static final String DEFAULT_VALUE_COLOR_WHITE
Definition: Component.java:82
com.google.appinventor.components.runtime.Component.DEFAULT_VALUE_COLOR_DKGRAY
static final String DEFAULT_VALUE_COLOR_DKGRAY
Definition: Component.java:74
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_BOOLEAN
static final String PROPERTY_TYPE_BOOLEAN
Definition: PropertyTypeConstants.java:35
com.google.appinventor.components.runtime.Component.COLOR_LTGRAY
static final int COLOR_LTGRAY
Definition: Component.java:61
com.google.appinventor.components.annotations.DesignerComponent
Definition: DesignerComponent.java:22
com.google.appinventor.components.annotations.SimpleEvent
Definition: SimpleEvent.java:20
com.google.appinventor.components.annotations.PropertyCategory.BEHAVIOR
BEHAVIOR
Definition: PropertyCategory.java:15
com.google.appinventor.components.runtime.Switch.ThumbColorInactive
void ThumbColorInactive(int argb)
Definition: Switch.java:137
com.google.appinventor.components.runtime.ToggleBase< CompoundButton >::initToggle
void initToggle()
Definition: ToggleBase.java:60
com.google.appinventor.components.runtime.Switch.On
void On(boolean value)
Definition: Switch.java:212
com.google.appinventor.components.runtime.CheckBox
Definition: CheckBox.java:33
com.google.appinventor.components.runtime.util.SdkLevel
Definition: SdkLevel.java:19
com.google.appinventor.components.runtime.ToggleBase< CompoundButton >::view
T view
Definition: ToggleBase.java:32
com.google.appinventor.components.runtime.util.SdkLevel.LEVEL_ICE_CREAM_SANDWICH
static final int LEVEL_ICE_CREAM_SANDWICH
Definition: SdkLevel.java:30
com.google.appinventor.components.annotations.SimpleProperty
Definition: SimpleProperty.java:23
com.google.appinventor.components.runtime.Switch.ThumbColorInactive
int ThumbColorInactive()
Definition: Switch.java:125
com.google.appinventor.components.annotations.PropertyCategory
Definition: PropertyCategory.java:13
com.google.appinventor.components.runtime.Switch.ThumbColorActive
void ThumbColorActive(int argb)
Definition: Switch.java:110
com.google.appinventor.components.runtime.ComponentContainer
Definition: ComponentContainer.java:16
com.google.appinventor.components.runtime.util.SdkLevel.getLevel
static int getLevel()
Definition: SdkLevel.java:45
com.google.appinventor.components.runtime
Copyright 2009-2011 Google, All Rights reserved.
Definition: AccelerometerSensor.java:8
com.google.appinventor.components.runtime.Component
Definition: Component.java:17
com.google.appinventor.components.runtime.Switch.ThumbColorActive
int ThumbColorActive()
Definition: Switch.java:98
com.google.appinventor.components.common
Definition: ComponentCategory.java:7
com.google.appinventor.components.runtime.Component.COLOR_GRAY
static final int COLOR_GRAY
Definition: Component.java:59
com.google.appinventor.components.common.ComponentCategory
Definition: ComponentCategory.java:48
com.google.appinventor.components.annotations.SimpleObject
Definition: SimpleObject.java:23
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_COLOR
static final String PROPERTY_TYPE_COLOR
Definition: PropertyTypeConstants.java:63
com.google.appinventor.components.runtime.Component.COLOR_WHITE
static final int COLOR_WHITE
Definition: Component.java:66
com.google
com
com.google.appinventor.components.runtime.Switch.Switch
Switch(ComponentContainer container)
Definition: Switch.java:56
com.google.appinventor.components.runtime.Component.DEFAULT_VALUE_COLOR_LTGRAY
static final String DEFAULT_VALUE_COLOR_LTGRAY
Definition: Component.java:77
com.google.appinventor.components.runtime.ComponentContainer.$context
Activity $context()
com.google.appinventor.components.runtime.ToggleBase
Definition: ToggleBase.java:29
com.google.appinventor.components.runtime.Switch.TrackColorActive
int TrackColorActive()
Definition: Switch.java:151
com.google.appinventor.components.runtime.Component.DEFAULT_VALUE_COLOR_GREEN
static final String DEFAULT_VALUE_COLOR_GREEN
Definition: Component.java:76
com.google.appinventor.components.runtime.Switch.Changed
void Changed()
Definition: Switch.java:219
com.google.appinventor.components.annotations.PropertyCategory.APPEARANCE
APPEARANCE
Definition: PropertyCategory.java:16
com.google.appinventor.components.common.PropertyTypeConstants
Definition: PropertyTypeConstants.java:14
com.google.appinventor.components.annotations
com.google.appinventor