AI2 Component  (Version nb184)
AndroidViewComponent.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-2017 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 
26 @SimpleObject
27 public abstract class AndroidViewComponent extends VisibleComponent {
28 
29  protected final ComponentContainer container;
30 
31  private int percentWidthHolder = LENGTH_UNKNOWN;
32  private int percentHeightHolder = LENGTH_UNKNOWN;
33  private int lastSetWidth = LENGTH_UNKNOWN;
34  private int lastSetHeight = LENGTH_UNKNOWN;
35 
36  private int column = ComponentConstants.DEFAULT_ROW_COLUMN;
37  private int row = ComponentConstants.DEFAULT_ROW_COLUMN;
38 
45  this.container = container;
46  }
47 
51  public abstract View getView();
52 
58  description = "Specifies whether the %type% should be visible on the screen. "
59  + "Value is true if the %type% is showing and false if hidden.",
60  category = PropertyCategory.APPEARANCE)
61  public boolean Visible() {
62  return getView().getVisibility() == View.VISIBLE;
63  }
64 
71  defaultValue = "True")
73  public void Visible(boolean visibility) {
74  // The principle of least astonishment suggests we not offer the
75  // Android option INVISIBLE.
76  getView().setVisibility(visibility ? View.VISIBLE : View.GONE);
77  }
78 
84  @Override
86  public int Width() {
87  int zWidth = (int)(getView().getWidth() / container.$form().deviceDensity());
88 // System.err.println("AndroidViewComponent: Width() Called, returning " + zWidth);
89  return zWidth;
90  }
91 
97  @Override
98  @SimpleProperty(description = "Specifies the horizontal width of the %type%, measured in pixels.")
99  public void Width(int width) {
100  container.setChildWidth(this, width);
101  lastSetWidth = width;
102  if (width <= Component.LENGTH_PERCENT_TAG) {
104  } else {
106  }
107  }
108 
116  @Override
117  @SimpleProperty(description = "Specifies the horizontal width of the %type% as a percentage of "
118  + "the width of the Screen.")
119  public void WidthPercent(int pCent) {
120  if (pCent < 0 || pCent > 100) {
121  container.$form().dispatchErrorOccurredEvent(this, "WidthPercent",
123  return;
124  }
125  int v = -pCent + Component.LENGTH_PERCENT_TAG;
126  Width(v);
127  }
128 
129  public void setLastWidth(int width) {
130 // System.err.println(this + " percentWidthHolder being set to " + width);
131  percentWidthHolder = width;
132  }
133 
134  public int getSetWidth() {
135 // System.err.println(this + " getSetWidth() percentWidthHolder = " + percentWidthHolder);
136  if (percentWidthHolder == LENGTH_UNKNOWN) {
137  return Width(); // best guess...
138  } else {
139  return percentWidthHolder;
140  }
141  }
142 
143  public void setLastHeight(int height) {
144 // System.err.println(this + " percentHeightHolder being set to " + height);
145  percentHeightHolder = height;
146  }
147 
148  public int getSetHeight() {
149 // System.err.println(this + " getSetHeight() percentHeightHolder = " + percentHeightHolder);
150  if (percentHeightHolder == LENGTH_UNKNOWN) {
151  return Height(); // best guess...
152  } else {
153  return percentHeightHolder;
154  }
155  }
156 
167  public void CopyWidth(AndroidViewComponent sourceComponent) {
168  Width(sourceComponent.lastSetWidth);
169  }
170 
176  @Override
178  public int Height() {
179  return (int)(getView().getHeight() / container.$form().deviceDensity());
180  }
181 
187  @Override
188  @SimpleProperty(description = "Specifies the vertical height of the %type%, measured in pixels.")
189  public void Height(int height) {
190  container.setChildHeight(this, height);
191  lastSetHeight = height;
192  if (height <= Component.LENGTH_PERCENT_TAG) {
194  } else {
196  }
197  }
198 
206  @Override
207  @SimpleProperty(description = "Specifies the vertical height of the %type% as a percentage of "
208  + "the height of the Screen.")
209  public void HeightPercent(int pCent) {
210  if (pCent < 0 || pCent > 100) {
211  container.$form().dispatchErrorOccurredEvent(this, "HeightPercent",
213  return;
214  }
215  int v = -pCent + Component.LENGTH_PERCENT_TAG;
216  Height(v);
217  }
218 
229  public void CopyHeight(AndroidViewComponent sourceComponent) {
230  Height(sourceComponent.lastSetHeight);
231  }
232 
238  @SimpleProperty(userVisible = false)
239  public int Column() {
240  return column;
241  }
242 
248  @SimpleProperty(userVisible = false)
249  public void Column(int column) {
250  this.column = column;
251  }
252 
258  @SimpleProperty(userVisible = false)
259  public int Row() {
260  return row;
261  }
262 
268  @SimpleProperty(userVisible = false)
269  public void Row(int row) {
270  this.row = row;
271  }
272 
273  // Component implementation
274 
275  @Override
277  return container.$form();
278  }
279 }
com.google.appinventor.components.runtime.AndroidViewComponent.setLastHeight
void setLastHeight(int height)
Definition: AndroidViewComponent.java:143
com.google.appinventor.components.runtime.HandlesEventDispatching
Definition: HandlesEventDispatching.java:15
com.google.appinventor.components.runtime.util.ErrorMessages
Definition: ErrorMessages.java:17
com.google.appinventor.components.runtime.util
-*- mode: java; c-basic-offset: 2; -*-
Definition: AccountChooser.java:7
com.google.appinventor.components.runtime.Form.PercentStorageRecord.Dim
Definition: Form.java:265
com.google.appinventor.components.runtime.AndroidViewComponent.getSetHeight
int getSetHeight()
Definition: AndroidViewComponent.java:148
com.google.appinventor.components.annotations.DesignerProperty
Definition: DesignerProperty.java:25
com.google.appinventor.components.runtime.AndroidViewComponent.Visible
void Visible(boolean visibility)
Definition: AndroidViewComponent.java:73
com.google.appinventor.components.runtime.AndroidViewComponent.Height
int Height()
Definition: AndroidViewComponent.java:178
com.google.appinventor.components
com.google.appinventor.components.runtime.AndroidViewComponent.getSetWidth
int getSetWidth()
Definition: AndroidViewComponent.java:134
com.google.appinventor.components.runtime.AndroidViewComponent.HeightPercent
void HeightPercent(int pCent)
Definition: AndroidViewComponent.java:209
com.google.appinventor.components.runtime.ComponentContainer.setChildWidth
void setChildWidth(AndroidViewComponent component, int width)
com.google.appinventor.components.runtime.Component.LENGTH_UNKNOWN
static final int LENGTH_UNKNOWN
Definition: Component.java:120
com.google.appinventor.components.runtime.Form.PercentStorageRecord
Definition: Form.java:264
com.google.appinventor.components.runtime.AndroidViewComponent.Width
int Width()
Definition: AndroidViewComponent.java:86
com.google.appinventor.components.runtime.AndroidViewComponent.setLastWidth
void setLastWidth(int width)
Definition: AndroidViewComponent.java:129
com.google.appinventor.components.runtime.Component.LENGTH_PERCENT_TAG
static final int LENGTH_PERCENT_TAG
Definition: Component.java:123
com.google.appinventor.components.runtime.ComponentContainer.setChildHeight
void setChildHeight(AndroidViewComponent component, int height)
com.google.appinventor.components.runtime.Form.PercentStorageRecord.Dim.HEIGHT
HEIGHT
Definition: Form.java:266
com.google.appinventor.components.runtime.AndroidViewComponent.CopyWidth
void CopyWidth(AndroidViewComponent sourceComponent)
Definition: AndroidViewComponent.java:167
com.google.appinventor.components.runtime.VisibleComponent
Definition: VisibleComponent.java:20
com.google.appinventor.components.runtime.Form.PercentStorageRecord.Dim.WIDTH
WIDTH
Definition: Form.java:266
com.google.appinventor.components.runtime.AndroidViewComponent.Column
int Column()
Definition: AndroidViewComponent.java:239
com.google.appinventor.components.runtime.AndroidViewComponent.WidthPercent
void WidthPercent(int pCent)
Definition: AndroidViewComponent.java:119
com.google.appinventor.components.runtime.AndroidViewComponent.getDispatchDelegate
HandlesEventDispatching getDispatchDelegate()
Definition: AndroidViewComponent.java:276
com.google.appinventor.components.runtime.AndroidViewComponent.CopyHeight
void CopyHeight(AndroidViewComponent sourceComponent)
Definition: AndroidViewComponent.java:229
com.google.appinventor.components.annotations.SimpleProperty
Definition: SimpleProperty.java:23
com.google.appinventor.components.annotations.PropertyCategory
Definition: PropertyCategory.java:13
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_VISIBILITY
static final String PROPERTY_TYPE_VISIBILITY
Definition: PropertyTypeConstants.java:278
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.AndroidViewComponent.AndroidViewComponent
AndroidViewComponent(ComponentContainer container)
Definition: AndroidViewComponent.java:44
com.google.appinventor.components.common
Definition: ComponentCategory.java:7
com.google.appinventor.components.runtime.util.ErrorMessages.ERROR_BAD_PERCENT
static final int ERROR_BAD_PERCENT
Definition: ErrorMessages.java:205
com.google.appinventor.components.runtime.Form.dispatchErrorOccurredEvent
void dispatchErrorOccurredEvent(final Component component, final String functionName, final int errorNumber, final Object... messageArgs)
Definition: Form.java:1011
com.google.appinventor.components.runtime.Form.registerPercentLength
void registerPercentLength(AndroidViewComponent component, int length, PercentStorageRecord.Dim dim)
Definition: Form.java:707
com.google.appinventor.components.annotations.SimpleObject
Definition: SimpleObject.java:23
com.google.appinventor.components.runtime.AndroidViewComponent.getView
abstract View getView()
com.google.appinventor.components.runtime.AndroidViewComponent.container
final ComponentContainer container
Definition: AndroidViewComponent.java:29
com.google
com
com.google.appinventor.components.common.ComponentConstants.DEFAULT_ROW_COLUMN
static final int DEFAULT_ROW_COLUMN
Definition: ComponentConstants.java:29
com.google.appinventor.components.runtime.Form.unregisterPercentLength
void unregisterPercentLength(AndroidViewComponent component, PercentStorageRecord.Dim dim)
Definition: Form.java:713
com.google.appinventor.components.runtime.ComponentContainer.$form
Form $form()
com.google.appinventor.components.common.ComponentConstants
Definition: ComponentConstants.java:13
com.google.appinventor.components.runtime.AndroidViewComponent
Definition: AndroidViewComponent.java:27
com.google.appinventor.components.runtime.AndroidViewComponent.Visible
boolean Visible()
Definition: AndroidViewComponent.java:61
com.google.appinventor.components.runtime.AndroidViewComponent.Row
int Row()
Definition: AndroidViewComponent.java:259
com.google.appinventor.components.annotations.SimplePropertyCopier
Definition: SimplePropertyCopier.java:24
com.google.appinventor.components.runtime.Form
Definition: Form.java:126
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