AI2 Component  (Version nb184)
ToggleBase.java
Go to the documentation of this file.
1 // -*- mode: java; c-basic-offset: 2; -*-
2 // Copyright 2009-2011 Google, All Rights reserved
3 // Copyright 2018 MIT, All rights reserved
4 // Released under the Apache License, Version 2.0
5 // http://www.apache.org/licenses/LICENSE-2.0
6 
7 package com.google.appinventor.components.runtime;
8 
17 
18 import android.view.View;
19 import android.view.View.OnFocusChangeListener;
20 import android.widget.CompoundButton;
21 import android.widget.CompoundButton.OnCheckedChangeListener;
22 
28 @SimpleObject
29 public abstract class ToggleBase<T extends CompoundButton> extends AndroidViewComponent
30  implements OnCheckedChangeListener, OnFocusChangeListener {
31 
32  protected T view;
33 
34  // Backing for background color
35  private int backgroundColor;
36 
37  // Backing for font typeface
38  private int fontTypeface;
39 
40  // Backing for font bold
41  private boolean bold;
42 
43  // Backing for font italic
44  private boolean italic;
45 
46  // Backing for text color
47  private int textColor;
48 
54  @SuppressWarnings("WeakerAccess") // Could be used by extensions
56  super(container);
57  }
58 
59  @SuppressWarnings("WeakerAccess") // Could be used by extensions
60  protected void initToggle() {
61  // Listen to focus changes
62  view.setOnFocusChangeListener(this);
63  view.setOnCheckedChangeListener(this);
64 
65  // Adds the component to its designated container
66  container.$add(this);
68  Enabled(true);
69  fontTypeface = Component.TYPEFACE_DEFAULT;
70  TextViewUtil.setFontTypeface(view, fontTypeface, bold, italic);
72  Text("");
74 
75  }
76 
77  @Override
78  public View getView() {
79  return view;
80  }
81 
85  @SimpleEvent(description = "User tapped and released the %type%.")
86  public void Changed() {
87  EventDispatcher.dispatchEvent(this, "Changed");
88  }
89 
93  @SimpleEvent(description = "%type% became the focused component.")
94  public void GotFocus() {
95  EventDispatcher.dispatchEvent(this, "GotFocus");
96  }
97 
101  @SimpleEvent(description = "%type% stopped being the focused component.")
102  public void LostFocus() {
103  EventDispatcher.dispatchEvent(this, "LostFocus");
104  }
105 
113  defaultValue = Component.DEFAULT_VALUE_COLOR_NONE)
114  @SimpleProperty(description = "The background color of the %type% as an alpha-red-green-blue integer.")
115  public void BackgroundColor(int argb) {
116  backgroundColor = argb;
117  if (argb != Component.COLOR_DEFAULT) {
119  } else {
121  }
122  }
123 
132  category = PropertyCategory.APPEARANCE)
133  @IsColor
134  public int BackgroundColor() {
135  return backgroundColor;
136  }
137 
144  defaultValue = "True")
145  @SimpleProperty(description = "True if the %type% is active and clickable.")
146  public void Enabled(boolean enabled) {
147  TextViewUtil.setEnabled(view, enabled);
148  }
149 
157  category = PropertyCategory.BEHAVIOR)
158  public boolean Enabled() {
159  return view.isEnabled();
160  }
161 
169  defaultValue = "False")
171  userVisible = false,
172  description = "Set to true if the text of the %type% should be bold.")
173  public void FontBold(boolean bold) {
174  this.bold = bold;
175  TextViewUtil.setFontTypeface(view, fontTypeface, bold, italic);
176  }
177 
187  category = PropertyCategory.APPEARANCE,
188  userVisible = false)
189  public boolean FontBold() {
190  return bold;
191  }
192 
200  defaultValue = "False")
202  userVisible = false,
203  description = "Set to true if the text of the %type% should be italic.")
204  public void FontItalic(boolean italic) {
205  this.italic = italic;
206  TextViewUtil.setFontTypeface(view, fontTypeface, bold, italic);
207  }
208 
218  category = PropertyCategory.APPEARANCE,
219  userVisible = false)
220  public boolean FontItalic() {
221  return italic;
222  }
223 
230  defaultValue = Component.FONT_DEFAULT_SIZE + "")
231  @SimpleProperty(description = "Specifies the text font size of the %type% in scale-independent "
232  + "pixels.")
233  public void FontSize(float size) {
235  }
236 
244  category = PropertyCategory.APPEARANCE)
245  public float FontSize() {
247  }
248 
259  defaultValue = Component.TYPEFACE_DEFAULT + "")
261  description = "Specifies the text font face of the %type%.",
262  userVisible = false)
263  public void FontTypeface(int typeface) {
264  fontTypeface = typeface;
265  TextViewUtil.setFontTypeface(view, fontTypeface, bold, italic);
266  }
267 
279  category = PropertyCategory.APPEARANCE,
280  userVisible = false)
281  public int FontTypeface() {
282  return fontTypeface;
283  }
284 
291  @SimpleProperty(description = "Specifies the text displayed by the %type%.")
292  public void Text(String text) {
293  TextViewUtil.setText(view, text);
294  }
295 
303  category = PropertyCategory.APPEARANCE)
304  public String Text() {
305  return TextViewUtil.getText(view);
306  }
307 
315  defaultValue = Component.DEFAULT_VALUE_COLOR_BLACK)
316  @SimpleProperty(description = "Specifies the text color of the %type% as an "
317  + "alpha-red-green-blue integer.")
318  public void TextColor(int argb) {
319  textColor = argb;
320  if (argb != Component.COLOR_DEFAULT) {
322  } else {
324  }
325  }
326 
335  category = PropertyCategory.APPEARANCE)
336  @IsColor
337  public int TextColor() {
338  return textColor;
339  }
340 
341  // OnCheckedChangeListener implementation
342 
343  public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
344  Changed();
345  }
346 
347  // OnFocusChangeListener implementation
348 
349  public void onFocusChange(View previouslyFocused, boolean gainFocus) {
350  if (gainFocus) {
351  GotFocus();
352  } else {
353  LostFocus();
354  }
355  }
356 }
com.google.appinventor.components.runtime.EventDispatcher
Definition: EventDispatcher.java:22
com.google.appinventor.components.runtime.util.TextViewUtil.setText
static void setText(TextView textview, String text)
Definition: TextViewUtil.java:188
com.google.appinventor.components.runtime.Component.COLOR_DEFAULT
static final int COLOR_DEFAULT
Definition: Component.java:68
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_TYPEFACE
static final String PROPERTY_TYPE_TYPEFACE
Definition: PropertyTypeConstants.java:272
com.google.appinventor.components.runtime.ToggleBase.FontBold
boolean FontBold()
Definition: ToggleBase.java:189
com.google.appinventor.components.runtime.util
-*- mode: java; c-basic-offset: 2; -*-
Definition: AccountChooser.java:7
com.google.appinventor.components.runtime.ToggleBase.Enabled
boolean Enabled()
Definition: ToggleBase.java:158
com.google.appinventor.components.annotations.DesignerProperty
Definition: DesignerProperty.java:25
com.google.appinventor.components.runtime.Component.DEFAULT_VALUE_COLOR_NONE
static final String DEFAULT_VALUE_COLOR_NONE
Definition: Component.java:70
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_STRING
static final String PROPERTY_TYPE_STRING
Definition: PropertyTypeConstants.java:237
com.google.appinventor.components
com.google.appinventor.components.runtime.ToggleBase.GotFocus
void GotFocus()
Definition: ToggleBase.java:94
com.google.appinventor.components.runtime.ToggleBase.FontTypeface
int FontTypeface()
Definition: ToggleBase.java:281
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_BOOLEAN
static final String PROPERTY_TYPE_BOOLEAN
Definition: PropertyTypeConstants.java:35
com.google.appinventor.components.annotations.SimpleEvent
Definition: SimpleEvent.java:20
com.google.appinventor.components.runtime.ToggleBase.FontItalic
boolean FontItalic()
Definition: ToggleBase.java:220
com.google.appinventor.components.annotations.PropertyCategory.BEHAVIOR
BEHAVIOR
Definition: PropertyCategory.java:15
com.google.appinventor.components.runtime.util.TextViewUtil.setFontTypeface
static void setFontTypeface(TextView textview, int typeface, boolean bold, boolean italic)
Definition: TextViewUtil.java:126
com.google.appinventor.components.runtime.util.TextViewUtil.getText
static String getText(TextView textview)
Definition: TextViewUtil.java:167
com.google.appinventor.components.runtime.ToggleBase.onCheckedChanged
void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
Definition: ToggleBase.java:343
com.google.appinventor.components.runtime.ToggleBase.Changed
void Changed()
Definition: ToggleBase.java:86
com.google.appinventor.components.runtime.ComponentContainer.$add
void $add(AndroidViewComponent component)
com.google.appinventor.components.runtime.ToggleBase.LostFocus
void LostFocus()
Definition: ToggleBase.java:102
com.google.appinventor.components.runtime.util.TextViewUtil.getFontSize
static float getFontSize(TextView textview, Context context)
Definition: TextViewUtil.java:99
com.google.appinventor.components.runtime.ToggleBase.initToggle
void initToggle()
Definition: ToggleBase.java:60
com.google.appinventor.components.runtime.Form.isDarkTheme
boolean isDarkTheme()
Definition: Form.java:2584
com.google.appinventor.components.runtime.ToggleBase.TextColor
int TextColor()
Definition: ToggleBase.java:337
com.google.appinventor.components.runtime.util.TextViewUtil
Definition: TextViewUtil.java:22
com.google.appinventor.components.runtime.Component.COLOR_NONE
static final int COLOR_NONE
Definition: Component.java:54
com.google.appinventor.components.runtime.EventDispatcher.dispatchEvent
static boolean dispatchEvent(Component component, String eventName, Object...args)
Definition: EventDispatcher.java:188
com.google.appinventor.components.runtime.util.TextViewUtil.setBackgroundColor
static void setBackgroundColor(TextView textview, int argb)
Definition: TextViewUtil.java:66
com.google.appinventor.components.runtime.ToggleBase.getView
View getView()
Definition: ToggleBase.java:78
com.google.appinventor.components.runtime.ToggleBase.view
T view
Definition: ToggleBase.java:32
com.google.appinventor.components.annotations.SimpleProperty
Definition: SimpleProperty.java:23
com.google.appinventor.components.runtime.util.TextViewUtil.setFontSize
static void setFontSize(TextView textview, float size)
Definition: TextViewUtil.java:110
com.google.appinventor.components.annotations.PropertyCategory
Definition: PropertyCategory.java:13
com.google.appinventor.components.runtime.Component.COLOR_BLACK
static final int COLOR_BLACK
Definition: Component.java:55
com.google.appinventor.components.runtime.ComponentContainer
Definition: ComponentContainer.java:16
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.ToggleBase.FontSize
float FontSize()
Definition: ToggleBase.java:245
com.google.appinventor.components.common
Definition: ComponentCategory.java:7
com.google.appinventor.components.runtime.ToggleBase.BackgroundColor
int BackgroundColor()
Definition: ToggleBase.java:134
com.google.appinventor.components.runtime.Component.TYPEFACE_DEFAULT
static final int TYPEFACE_DEFAULT
Definition: Component.java:106
com.google.appinventor.components.runtime.ToggleBase.onFocusChange
void onFocusChange(View previouslyFocused, boolean gainFocus)
Definition: ToggleBase.java:349
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.appinventor.components.runtime.AndroidViewComponent.container
final ComponentContainer container
Definition: AndroidViewComponent.java:29
com.google
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_NON_NEGATIVE_FLOAT
static final String PROPERTY_TYPE_NON_NEGATIVE_FLOAT
Definition: PropertyTypeConstants.java:200
com
com.google.appinventor.components.runtime.ToggleBase.Text
String Text()
Definition: ToggleBase.java:304
com.google.appinventor.components.runtime.ComponentContainer.$form
Form $form()
com.google.appinventor.components.runtime.ComponentContainer.$context
Activity $context()
com.google.appinventor.components.runtime.ToggleBase
Definition: ToggleBase.java:29
com.google.appinventor.components.runtime.util.TextViewUtil.setEnabled
static void setEnabled(TextView textview, boolean enabled)
Definition: TextViewUtil.java:87
com.google.appinventor.components.runtime.AndroidViewComponent
Definition: AndroidViewComponent.java:27
com.google.appinventor.components.runtime.util.TextViewUtil.setTextColor
static void setTextColor(TextView textview, int argb)
Definition: TextViewUtil.java:210
com.google.appinventor.components.annotations.IsColor
Definition: IsColor.java:13
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.runtime.Component.FONT_DEFAULT_SIZE
static final float FONT_DEFAULT_SIZE
Definition: Component.java:89
com.google.appinventor.components.annotations
com.google.appinventor.components.runtime.Component.DEFAULT_VALUE_COLOR_BLACK
static final String DEFAULT_VALUE_COLOR_BLACK
Definition: Component.java:71
com.google.appinventor