AI2 Component  (Version nb184)
TextBoxBase.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 2011-2012 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 
21 
22 //import com.google.appinventor.components.runtime.parameters.BooleanReferenceParameter;
23 import android.graphics.drawable.Drawable;
24 import android.os.Build;
25 import android.view.View;
26 import android.view.View.OnFocusChangeListener;
27 import android.widget.EditText;
28 
36 @SimpleObject
37 public abstract class TextBoxBase extends AndroidViewComponent
38  implements OnFocusChangeListener {
39 
40  protected final EditText view;
41 
42  // Backing for text alignment
43  private int textAlignment;
44 
45  // Backing for background color
46  private int backgroundColor;
47 
48  // Backing for font typeface
49  private int fontTypeface;
50 
51  // Backing for font bold
52  private boolean bold;
53 
54  // Backing for font italic
55  private boolean italic;
56 
57  // Backing for hint text
58  private String hint;
59 
60  // Backing for text color
61  private int textColor;
62 
63  // This is our handle on Android's nice 3-d default textbox.
64  private Drawable defaultTextBoxDrawable;
65 
72  public TextBoxBase(ComponentContainer container, EditText textview) {
73  super(container);
74  view = textview;
75  // There appears to be an issue where, by default, Android 7+
76  // wants to provide suggestions in text boxes. However, we do not
77  // compile the necessary layouts for this to work correctly, which
78  // results in an application crash. This disables that feature
79  // until we include newer Android layouts.
80  if (Build.VERSION.SDK_INT >= 24 /* Nougat */ ) {
82  }
83 
84  // Listen to focus changes
85  view.setOnFocusChangeListener(this);
86 
87  defaultTextBoxDrawable = view.getBackground();
88 
89  // Add a transformation method to provide input validation
90  /* TODO(user): see comment above)
91  setTransformationMethod(new ValidationTransformationMethod());
92  */
93 
94  // Adds the component to its designated container
95  container.$add(this);
96 
98 
100  // Leave the nice default background color. Users can change it to "none" if they like
101  //
102  // TODO(user): if we make a change here we also need to change the default property value.
103  // Eventually I hope to simplify this so it has to be changed in one location
104  // only). Maybe we need another color value which would be 'SYSTEM_DEFAULT' which
105  // will not attempt to explicitly initialize with any of the properties with any
106  // particular value.
107  // BackgroundColor(Component.COLOR_NONE);
108  Enabled(true);
109  fontTypeface = Component.TYPEFACE_DEFAULT;
110  TextViewUtil.setFontTypeface(view, fontTypeface, bold, italic);
112  Hint("");
113  Text("");
115  }
116 
117  @Override
118  public View getView() {
119  return view;
120  }
121 
126  @SimpleEvent(description = "Event raised when the %type% is selected for input, such as by "
127  + "the user touching it.")
128  public void GotFocus() {
129  EventDispatcher.dispatchEvent(this, "GotFocus");
130  }
131 
136  @SimpleEvent(description = "Event raised when the %type% is no longer selected for input, such "
137  + "as if the user touches a different text box.")
138  public void LostFocus() {
139  EventDispatcher.dispatchEvent(this, "LostFocus");
140  }
141 
145  /* TODO(markf): Restore event if needed.
146  @SimpleEvent
147  public void Validate(String text, BooleanReferenceParameter accept) {
148  EventDispatcher.dispatchEvent(this, "Validate", text, accept);
149  }
150  */
151 
162  category = PropertyCategory.APPEARANCE,
163  description = "Whether the text should be left justified, centered, " +
164  "or right justified. By default, text is left justified.",
165  userVisible = false)
166  public int TextAlignment() {
167  return textAlignment;
168  }
169 
181  defaultValue = Component.ALIGNMENT_NORMAL + "")
183  userVisible = false)
184  public void TextAlignment(int alignment) {
185  this.textAlignment = alignment;
186  TextViewUtil.setAlignment(view, alignment, false);
187  }
188 
196  category = PropertyCategory.APPEARANCE,
197  description = "The background color of the input box. You can choose " +
198  "a color by name in the Designer or in the Blocks Editor. The " +
199  "default background color is 'default' (shaded 3-D look).")
200  @IsColor
201  public int BackgroundColor() {
202  return backgroundColor;
203  }
204 
216  defaultValue = Component.DEFAULT_VALUE_COLOR_DEFAULT)
218  public void BackgroundColor(int argb) {
219  backgroundColor = argb;
220  if (argb != Component.COLOR_DEFAULT) {
222  } else {
223  ViewUtil.setBackgroundDrawable(view, defaultTextBoxDrawable);
224  }
225  }
226 
233  category = PropertyCategory.BEHAVIOR,
234  description = "Whether the user can enter text into the %type%. " +
235  "By default, this is true.")
236  public boolean Enabled() {
237  return TextViewUtil.isEnabled(view);
238  }
239 
249  defaultValue = "True")
251  public void Enabled(boolean enabled) {
252  TextViewUtil.setEnabled(view, enabled);
253  }
254 
263  category = PropertyCategory.APPEARANCE,
264  userVisible = false,
265  description = "Whether the font for the text should be bold. By " +
266  "default, it is not.")
267  public boolean FontBold() {
268  return bold;
269  }
270 
278  defaultValue = "False")
280  userVisible = false)
281  public void FontBold(boolean bold) {
282  this.bold = bold;
283  TextViewUtil.setFontTypeface(view, fontTypeface, bold, italic);
284  }
285 
294  category = PropertyCategory.APPEARANCE,
295  description = "Whether the text should appear in italics. By " +
296  "default, it does not.",
297  userVisible = false)
298  public boolean FontItalic() {
299  return italic;
300  }
301 
309  defaultValue = "False")
310  @SimpleProperty(userVisible = false)
311  public void FontItalic(boolean italic) {
312  this.italic = italic;
313  TextViewUtil.setFontTypeface(view, fontTypeface, bold, italic);
314  }
315 
322  category = PropertyCategory.APPEARANCE,
323  description = "The font size for the text. By default, it is " +
324  Component.FONT_DEFAULT_SIZE + " points.")
325  public float FontSize() {
327  }
328 
335  defaultValue = Component.FONT_DEFAULT_SIZE + "")
337  public void FontSize(float size) {
339  }
340 
351  category = PropertyCategory.APPEARANCE,
352  description = "The font for the text. The value can be changed in " +
353  "the Designer.",
354  userVisible = false)
355  public int FontTypeface() {
356  return fontTypeface;
357  }
358 
369  defaultValue = Component.TYPEFACE_DEFAULT + "")
371  userVisible = false)
372  public void FontTypeface(int typeface) {
373  fontTypeface = typeface;
374  TextViewUtil.setFontTypeface(view, fontTypeface, bold, italic);
375  }
376 
383  category = PropertyCategory.APPEARANCE,
384  description = "Text that should appear faintly in the %type% to " +
385  "provide a hint as to what the user should enter. This can only be " +
386  "seen if the Text property is empty.")
387  public String Hint() {
388  return hint;
389  }
390 
400  defaultValue = "")
402  public void Hint(String hint) {
403  this.hint = hint;
404  view.setHint(hint);
405  view.invalidate();
406  }
407 
414  public String Text() {
415  return TextViewUtil.getText(view);
416  }
417 
425  defaultValue = "")
427  // This kind of breaks the appearance/behavior dichotomy
428  category = PropertyCategory.BEHAVIOR,
429  description = "The text in the %type%, which can be set by the " +
430  "programmer in the Designer or Blocks Editor, or it can be entered by " +
431  "the user (unless the <code>Enabled</code> property is false).")
432  public void Text(String text) {
433  TextViewUtil.setText(view, text);
434  }
435 
443  category = PropertyCategory.APPEARANCE,
444  description = "The color for the text. You can choose a color by name " +
445  "in the Designer or in the Blocks Editor. The default text color is " +
446  "black.")
447  @IsColor
448  public int TextColor() {
449  return textColor;
450  }
451 
459  defaultValue = Component.DEFAULT_VALUE_COLOR_BLACK)
461  public void TextColor(int argb) {
462  textColor = argb;
463  if (argb != Component.COLOR_DEFAULT) {
465  } else {
467  }
468  }
469 
474  description = "Sets the %type% active.")
475  public void RequestFocus() {
476  view.requestFocus();
477  }
478 
479  // OnFocusChangeListener implementation
480 
481  @Override
482  public void onFocusChange(View previouslyFocused, boolean gainFocus) {
483  if (gainFocus) {
484  // Initialize content backing for input validation
485  // TODO(sharon): this field stayed in TextBox. It isn't being used yet,
486  // and I'm not sure what to do with this assignment.
487  // text = TextViewUtil.getText(view);
488 
489  GotFocus();
490  } else {
491  LostFocus();
492  }
493  }
494 }
com.google.appinventor.components.runtime.EventDispatcher
Definition: EventDispatcher.java:22
com.google.appinventor.components.runtime.TextBoxBase.Enabled
void Enabled(boolean enabled)
Definition: TextBoxBase.java:251
com.google.appinventor.components.runtime.util.TextViewUtil.setText
static void setText(TextView textview, String text)
Definition: TextViewUtil.java:188
com.google.appinventor.components.runtime.TextBoxBase.TextBoxBase
TextBoxBase(ComponentContainer container, EditText textview)
Definition: TextBoxBase.java:72
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_TEXTALIGNMENT
static final String PROPERTY_TYPE_TEXTALIGNMENT
Definition: PropertyTypeConstants.java:260
com.google.appinventor.components.runtime.Component.COLOR_DEFAULT
static final int COLOR_DEFAULT
Definition: Component.java:68
com.google.appinventor.components.annotations.SimpleFunction
Definition: SimpleFunction.java:23
com.google.appinventor.components.runtime.TextBoxBase.view
final EditText view
Definition: TextBoxBase.java:40
com.google.appinventor.components.runtime.TextBoxBase.FontTypeface
int FontTypeface()
Definition: TextBoxBase.java:355
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_TYPEFACE
static final String PROPERTY_TYPE_TYPEFACE
Definition: PropertyTypeConstants.java:272
com.google.appinventor.components.runtime.util.EclairUtil.disableSuggestions
static void disableSuggestions(EditText textview)
Definition: EclairUtil.java:117
com.google.appinventor.components.runtime.TextBoxBase.TextColor
int TextColor()
Definition: TextBoxBase.java:448
com.google.appinventor.components.runtime.TextBoxBase.getView
View getView()
Definition: TextBoxBase.java:118
com.google.appinventor.components.runtime.util
-*- mode: java; c-basic-offset: 2; -*-
Definition: AccountChooser.java:7
com.google.appinventor.components.runtime.TextBoxBase.FontSize
float FontSize()
Definition: TextBoxBase.java:325
com.google.appinventor.components.annotations.DesignerProperty
Definition: DesignerProperty.java:25
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.ComponentContainer.setChildWidth
void setChildWidth(AndroidViewComponent component, int width)
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_BOOLEAN
static final String PROPERTY_TYPE_BOOLEAN
Definition: PropertyTypeConstants.java:35
com.google.appinventor.components.runtime.TextBoxBase.LostFocus
void LostFocus()
Definition: TextBoxBase.java:138
com.google.appinventor.components.runtime.TextBoxBase.GotFocus
void GotFocus()
Definition: TextBoxBase.java:128
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.util.TextViewUtil.setFontTypeface
static void setFontTypeface(TextView textview, int typeface, boolean bold, boolean italic)
Definition: TextViewUtil.java:126
com.google.appinventor.components.runtime.TextBoxBase.TextAlignment
int TextAlignment()
Definition: TextBoxBase.java:166
com.google.appinventor.components.runtime.util.TextViewUtil.getText
static String getText(TextView textview)
Definition: TextViewUtil.java:167
com.google.appinventor.components.runtime.ComponentContainer.$add
void $add(AndroidViewComponent component)
com.google.appinventor.components.runtime.TextBoxBase
Definition: TextBoxBase.java:37
com.google.appinventor.components.runtime.util.TextViewUtil.setAlignment
static void setAlignment(TextView textview, int alignment, boolean centerVertically)
Definition: TextViewUtil.java:35
com.google.appinventor.components.runtime.TextBoxBase.BackgroundColor
void BackgroundColor(int argb)
Definition: TextBoxBase.java:218
com.google.appinventor.components.runtime.util.TextViewUtil.getFontSize
static float getFontSize(TextView textview, Context context)
Definition: TextViewUtil.java:99
com.google.appinventor.components.runtime.Form.isDarkTheme
boolean isDarkTheme()
Definition: Form.java:2584
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_TEXTAREA
static final String PROPERTY_TYPE_TEXTAREA
Definition: PropertyTypeConstants.java:248
com.google.appinventor.components.runtime.Component.DEFAULT_VALUE_COLOR_DEFAULT
static final String DEFAULT_VALUE_COLOR_DEFAULT
Definition: Component.java:84
com.google.appinventor.components.runtime.TextBoxBase.TextColor
void TextColor(int argb)
Definition: TextBoxBase.java:461
com.google.appinventor.components.runtime.TextBoxBase.BackgroundColor
int BackgroundColor()
Definition: TextBoxBase.java:201
com.google.appinventor.components.runtime.util.TextViewUtil
Definition: TextViewUtil.java:22
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.common.ComponentConstants.TEXTBOX_PREFERRED_WIDTH
static final int TEXTBOX_PREFERRED_WIDTH
Definition: ComponentConstants.java:50
com.google.appinventor.components.runtime.TextBoxBase.FontBold
boolean FontBold()
Definition: TextBoxBase.java:267
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.util.ViewUtil.setBackgroundDrawable
static void setBackgroundDrawable(View view, Drawable drawable)
Definition: ViewUtil.java:200
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.TextBoxBase.FontSize
void FontSize(float size)
Definition: TextBoxBase.java:337
com.google.appinventor.components.runtime.TextBoxBase.Text
String Text()
Definition: TextBoxBase.java:414
com.google.appinventor.components.runtime
Copyright 2009-2011 Google, All Rights reserved.
Definition: AccelerometerSensor.java:8
com.google.appinventor.components.runtime.util.TextViewUtil.isEnabled
static boolean isEnabled(TextView textview)
Definition: TextViewUtil.java:77
com.google.appinventor.components.runtime.Component
Definition: Component.java:17
com.google.appinventor.components.runtime.Component.ALIGNMENT_NORMAL
static final int ALIGNMENT_NORMAL
Definition: Component.java:32
com.google.appinventor.components.common
Definition: ComponentCategory.java:7
com.google.appinventor.components.runtime.Component.TYPEFACE_DEFAULT
static final int TYPEFACE_DEFAULT
Definition: Component.java:106
com.google.appinventor.components.runtime.TextBoxBase.Hint
void Hint(String hint)
Definition: TextBoxBase.java:402
com.google.appinventor.components.runtime.TextBoxBase.RequestFocus
void RequestFocus()
Definition: TextBoxBase.java:475
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.TextBoxBase.Enabled
boolean Enabled()
Definition: TextBoxBase.java:236
com.google.appinventor.components.runtime.ComponentContainer.$form
Form $form()
com.google.appinventor.components.runtime.ComponentContainer.$context
Activity $context()
com.google.appinventor.components.common.ComponentConstants
Definition: ComponentConstants.java:13
com.google.appinventor.components.runtime.util.TextViewUtil.setEnabled
static void setEnabled(TextView textview, boolean enabled)
Definition: TextViewUtil.java:87
com.google.appinventor.components.runtime.TextBoxBase.Hint
String Hint()
Definition: TextBoxBase.java:387
com.google.appinventor.components.runtime.AndroidViewComponent
Definition: AndroidViewComponent.java:27
com.google.appinventor.components.runtime.TextBoxBase.onFocusChange
void onFocusChange(View previouslyFocused, boolean gainFocus)
Definition: TextBoxBase.java:482
com.google.appinventor.components.runtime.util.TextViewUtil.setTextColor
static void setTextColor(TextView textview, int argb)
Definition: TextViewUtil.java:210
com.google.appinventor.components.runtime.util.EclairUtil
Definition: EclairUtil.java:31
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.runtime.TextBoxBase.FontItalic
boolean FontItalic()
Definition: TextBoxBase.java:298
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
com.google.appinventor.components.runtime.util.ViewUtil
Definition: ViewUtil.java:22