AI2 Component  (Version nb184)
Label.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 
19 
20 import android.util.Log;
21 import android.view.View;
22 import android.widget.LinearLayout;
23 import android.widget.TextView;
24 
33 @DesignerComponent(version = YaVersion.LABEL_COMPONENT_VERSION,
34  description = "A Label displays a piece of text, which is " +
35  "specified through the <code>Text</code> property. Other properties, " +
36  "all of which can be set in the Designer or Blocks Editor, control " +
37  "the appearance and placement of the text.",
38  category = ComponentCategory.USERINTERFACE)
39 @SimpleObject
40 public final class Label extends AndroidViewComponent {
41 
42  // default margin around a label in DPs
43  // note that the spacing between adjacent labels will be twice this value
44  // because each label has a margin
45  private static final int DEFAULT_LABEL_MARGIN = 2;
46 
47  // default margin in density-independent pixels. This must be
48  // computed using the view
49  private int defaultLabelMarginInDp = 0;
50 
51  private final TextView view;
52 
53  private final LinearLayout.LayoutParams linearLayoutParams;
54 
55  // Backing for text alignment
56  private int textAlignment;
57 
58  // Backing for background color
59  private int backgroundColor;
60 
61  // Backing for font typeface
62  private int fontTypeface;
63 
64  // Backing for font bold
65  private boolean bold;
66 
67  // Backing for font italic
68  private boolean italic;
69 
70  // Whether or not the label should have a margin
71  private boolean hasMargins;
72 
73  // Backing for text color
74  private int textColor;
75 
76  // Label Format
77  private boolean htmlFormat;
78 
79  // HTML content of the label
80  private String htmlContent;
81 
88  super(container);
89  view = new TextView(container.$context());
90 
91  // Adds the component to its designated container
92  container.$add(this);
93 
94  // Get the layout parameters to use in setting margins (and potentially
95  // other things.
96  // There will be a bug if the label view does not have linear layout params.
97  // TODO(hal): Generalize this for other types of layouts
98  Object lp = view.getLayoutParams();
99  // The following instanceof check will fail if we have not previously
100  // added the label to the container (Why?)
101  if (lp instanceof LinearLayout.LayoutParams) {
102  linearLayoutParams = (LinearLayout.LayoutParams) lp;
103  defaultLabelMarginInDp = dpToPx(view, DEFAULT_LABEL_MARGIN);
104  } else {
105  defaultLabelMarginInDp = 0;
106  linearLayoutParams = null;
107  Log.e("Label", "Error: The label's view does not have linear layout parameters");
108  new RuntimeException().printStackTrace();
109  }
110 
111  // Default property values
114  fontTypeface = Component.TYPEFACE_DEFAULT;
115  TextViewUtil.setFontTypeface(view, fontTypeface, bold, italic);
117  Text("");
119  HTMLFormat(false);
120  HasMargins(true);
121  }
122 
123  // put this in the right file
124  private static int dpToPx(View view, int dp) {
125  float density = view.getContext().getResources().getDisplayMetrics().density;
126  return Math.round((float)dp * density);
127  }
128 
129  @Override
130  public View getView() {
131  return view;
132  }
133 
144  category = PropertyCategory.APPEARANCE,
145  userVisible = false)
146  public int TextAlignment() {
147  return textAlignment;
148  }
149 
160  defaultValue = Component.ALIGNMENT_NORMAL + "")
162  userVisible = false)
163  public void TextAlignment(int alignment) {
164  this.textAlignment = alignment;
165  TextViewUtil.setAlignment(view, alignment, false);
166  }
167 
175  category = PropertyCategory.APPEARANCE)
176  @IsColor
177  public int BackgroundColor() {
178  return backgroundColor;
179  }
180 
188  defaultValue = Component.DEFAULT_VALUE_COLOR_NONE)
190  public void BackgroundColor(int argb) {
191  backgroundColor = argb;
192  if (argb != Component.COLOR_DEFAULT) {
193  TextViewUtil.setBackgroundColor(view, argb);
194  } else {
196  }
197  }
198 
207  category = PropertyCategory.APPEARANCE,
208  userVisible = false)
209  public boolean FontBold() {
210  return bold;
211  }
212 
220  defaultValue = "False")
222  userVisible = false)
223  public void FontBold(boolean bold) {
224  this.bold = bold;
225  TextViewUtil.setFontTypeface(view, fontTypeface, bold, italic);
226  }
227 
236  category = PropertyCategory.APPEARANCE,
237  userVisible = false)
238  public boolean FontItalic() {
239  return italic;
240  }
241 
249  defaultValue = "False")
251  userVisible = false)
252  public void FontItalic(boolean italic) {
253  this.italic = italic;
254  TextViewUtil.setFontTypeface(view, fontTypeface, bold, italic);
255  }
256 
263  category = PropertyCategory.APPEARANCE,
264  description = "Reports whether or not the label appears with margins. All four "
265  + "margins (left, right, top, bottom) are the same. This property has no effect "
266  + "in the designer, where labels are always shown with margins.",
267  userVisible = true)
268  public boolean HasMargins() {
269  return hasMargins;
270  }
271 
281  defaultValue = "True")
283  userVisible = true)
284  public void HasMargins(boolean hasMargins) {
285  this.hasMargins = hasMargins;
286  setLabelMargins(hasMargins);
287  }
288 
289 private void setLabelMargins(boolean hasMargins) {
290  int m = hasMargins ? defaultLabelMarginInDp : 0 ;
291  linearLayoutParams.setMargins(m, m, m, m);
292  view.invalidate();
293 }
294 
300  @SimpleProperty(
301  category = PropertyCategory.APPEARANCE)
302  public float FontSize() {
303  return TextViewUtil.getFontSize(view, container.$context());
304  }
305 
312  defaultValue = Component.FONT_DEFAULT_SIZE + "")
314  public void FontSize(float size) {
315  TextViewUtil.setFontSize(view, size);
316  }
317 
328  category = PropertyCategory.APPEARANCE,
329  userVisible = false)
330  public int FontTypeface() {
331  return fontTypeface;
332  }
333 
344  defaultValue = Component.TYPEFACE_DEFAULT + "")
346  userVisible = false)
347  public void FontTypeface(int typeface) {
348  fontTypeface = typeface;
349  TextViewUtil.setFontTypeface(view, fontTypeface, bold, italic);
350  }
351 
358  category = PropertyCategory.APPEARANCE)
359  public String Text() {
360  return TextViewUtil.getText(view);
361  }
362 
369  defaultValue = "")
371  public void Text(String text) {
372  if (htmlFormat) {
373  TextViewUtil.setTextHTML(view, text);
374  } else {
375  TextViewUtil.setText(view, text);
376  }
377  htmlContent = text;
378  }
379 
387  public String HTMLContent() {
388  if (htmlFormat) {
389  return htmlContent;
390  } else {
391  return TextViewUtil.getText(view);
392  }
393  }
394 
395 
403  category = PropertyCategory.APPEARANCE,
404  description = "If true, then this label will show html text else it " +
405  "will show plain text. Note: Not all HTML is supported.")
406  public boolean HTMLFormat() {
407  return htmlFormat;
408  }
409 
417  defaultValue = "False")
418  @SimpleProperty(userVisible = false)
419  public void HTMLFormat(boolean fmt) {
420  htmlFormat = fmt;
421  if (htmlFormat) {
422  String txt = TextViewUtil.getText(view);
423  TextViewUtil.setTextHTML(view, txt);
424  } else {
425  String txt = TextViewUtil.getText(view);
426  TextViewUtil.setText(view, txt);
427  }
428  }
429 
437  category = PropertyCategory.APPEARANCE)
438  @IsColor
439  public int TextColor() {
440  return textColor;
441  }
442 
450  defaultValue = Component.DEFAULT_VALUE_COLOR_BLACK)
452  public void TextColor(int argb) {
453  textColor = argb;
454  if (argb != Component.COLOR_DEFAULT) {
455  TextViewUtil.setTextColor(view, argb);
456  } else {
458  }
459  }
460 }
com.google.appinventor.components.runtime.Label
Definition: Label.java:40
com.google.appinventor.components.runtime.util.TextViewUtil.setText
static void setText(TextView textview, String text)
Definition: TextViewUtil.java:188
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_TEXTALIGNMENT
static final String PROPERTY_TYPE_TEXTALIGNMENT
Definition: PropertyTypeConstants.java:260
com.google.appinventor.components.runtime.Label.BackgroundColor
void BackgroundColor(int argb)
Definition: Label.java:190
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.util
-*- mode: java; c-basic-offset: 2; -*-
Definition: AccountChooser.java:7
com.google.appinventor.components.runtime.Label.Label
Label(ComponentContainer container)
Definition: Label.java:87
com.google.appinventor.components.runtime.Label.HTMLContent
String HTMLContent()
Definition: Label.java:387
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.util.TextViewUtil.setTextHTML
static void setTextHTML(TextView textview, String text)
Definition: TextViewUtil.java:177
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
com.google.appinventor.components.runtime.Label.TextColor
int TextColor()
Definition: Label.java:439
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_BOOLEAN
static final String PROPERTY_TYPE_BOOLEAN
Definition: PropertyTypeConstants.java:35
com.google.appinventor.components.annotations.DesignerComponent
Definition: DesignerComponent.java:22
com.google.appinventor.components.runtime.Label.BackgroundColor
int BackgroundColor()
Definition: Label.java:177
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.Label.TextColor
void TextColor(int argb)
Definition: Label.java:452
com.google.appinventor.components.runtime.ComponentContainer.$add
void $add(AndroidViewComponent component)
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.util.TextViewUtil.getFontSize
static float getFontSize(TextView textview, Context context)
Definition: TextViewUtil.java:99
com.google.appinventor.components.runtime.Label.FontTypeface
int FontTypeface()
Definition: Label.java:330
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.Label.HasMargins
boolean HasMargins()
Definition: Label.java:268
com.google.appinventor.components.runtime.Label.TextAlignment
int TextAlignment()
Definition: Label.java:146
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.util.TextViewUtil.setBackgroundColor
static void setBackgroundColor(TextView textview, int argb)
Definition: TextViewUtil.java:66
com.google.appinventor.components.runtime.Label.FontBold
boolean FontBold()
Definition: Label.java:209
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.runtime.Label.Text
String Text()
Definition: Label.java:359
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.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.Label.Text
void Text(String text)
Definition: Label.java:371
com.google.appinventor.components.common.ComponentCategory
Definition: ComponentCategory.java:48
com.google.appinventor.components.runtime.Component.TYPEFACE_DEFAULT
static final int TYPEFACE_DEFAULT
Definition: Component.java:106
com.google.appinventor.components.annotations.SimpleObject
Definition: SimpleObject.java:23
com.google.appinventor.components.runtime.Label.HTMLFormat
boolean HTMLFormat()
Definition: Label.java:406
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_COLOR
static final String PROPERTY_TYPE_COLOR
Definition: PropertyTypeConstants.java:63
com.google.appinventor.components.runtime.Label.FontItalic
boolean FontItalic()
Definition: Label.java:238
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.appinventor.components.runtime.Label.FontSize
float FontSize()
Definition: Label.java:302
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.ComponentContainer.$form
Form $form()
com.google.appinventor.components.runtime.ComponentContainer.$context
Activity $context()
com.google.appinventor.components.runtime.Label.FontSize
void FontSize(float size)
Definition: Label.java:314
com.google.appinventor.components.runtime.Label.getView
View getView()
Definition: Label.java:130
com.google.appinventor.components.runtime.LinearLayout
Definition: LinearLayout.java:20
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