AI2 Component  (Version nb184)
HVArrangement.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 
9 import android.app.Activity;
10 
11 import android.graphics.drawable.Drawable;
12 
13 import android.os.Handler;
14 
15 import android.util.Log;
16 
17 import android.view.View;
18 import android.view.ViewGroup;
19 
20 import android.widget.FrameLayout;
21 import android.widget.HorizontalScrollView;
22 import android.widget.ScrollView;
23 
28 
31 
36 
37 import java.io.IOException;
38 
47 @SimpleObject
49  private final Activity context;
50 
51  // Layout
52  private final int orientation;
53  private final LinearLayout viewLayout;
54  private ViewGroup frameContainer;
55  private boolean scrollable = false;
56  // translates App Inventor alignment codes to Android gravity
57  private AlignmentUtil alignmentSetter;
58 
59  // the alignment for this component's LinearLayout
60  private int horizontalAlignment;
61  private int verticalAlignment;
62  // Backing for background color
63  private int backgroundColor;
64  // This is the Drawable corresponding to the Image property.
65  // If an Image has never been set or if the most recent Image could not be loaded, this is null.
66  private Drawable backgroundImageDrawable;
67  // Image path
68  private String imagePath = "";
69 
70  private Drawable defaultButtonDrawable;
71 
72  private final Handler androidUIHandler = new Handler();
73 
74  private static final String LOG_TAG = "HVArrangement";
75 
84  public HVArrangement(ComponentContainer container, int orientation, boolean scrollable) {
85  super(container);
86  context = container.$context();
87 
88  this.orientation = orientation;
89  this.scrollable = scrollable;
90  viewLayout = new LinearLayout(context, orientation,
93 
94  viewLayout.setBaselineAligned(false);
95  alignmentSetter = new AlignmentUtil(viewLayout);
98  alignmentSetter.setHorizontalAlignment(horizontalAlignment);
99  alignmentSetter.setVerticalAlignment(verticalAlignment);
100 
101  if (scrollable) {
102  switch (orientation) {
104  Log.d(LOG_TAG, "Setting up frameContainer = ScrollView()");
105  frameContainer = new ScrollView(context);
106  break;
108  Log.d(LOG_TAG, "Setting up frameContainer = HorizontalScrollView()");
109  frameContainer = new HorizontalScrollView(context);
110  break;
111  }
112  } else {
113  Log.d(LOG_TAG, "Setting up frameContainer = FrameLayout()");
114  frameContainer = new FrameLayout(context);
115  }
116 
117  frameContainer.setLayoutParams(new ViewGroup.LayoutParams(ComponentConstants.EMPTY_HV_ARRANGEMENT_WIDTH, ComponentConstants.EMPTY_HV_ARRANGEMENT_HEIGHT));
118  frameContainer.addView(viewLayout.getLayoutManager(), new ViewGroup.LayoutParams(
119  ViewGroup.LayoutParams.MATCH_PARENT,
120  ViewGroup.LayoutParams.MATCH_PARENT));
121 
122  // Save the default values in case the user wants them back later.
123  defaultButtonDrawable = getView().getBackground();
124 
125  container.$add(this);
127 
128  }
129 
130 
131 
132  // ComponentContainer implementation
133 
134  @Override
135  public Activity $context() {
136  return context;
137  }
138 
139  @Override
140  public Form $form() {
141  return container.$form();
142  }
143 
144  @Override
145  public void $add(AndroidViewComponent component) {
146  viewLayout.add(component);
147  }
148 
149  @Override
150  public void setChildWidth(final AndroidViewComponent component, int width) {
151  setChildWidth(component, width, 0);
152  }
153 
154  public void setChildWidth(final AndroidViewComponent component, int width, final int trycount) {
155  int cWidth = container.$form().Width();
156  if (cWidth == 0 && trycount < 2) { // We're not really ready yet...
157  final int fWidth = width; // but give up after two tries...
158  androidUIHandler.postDelayed(new Runnable() {
159  @Override
160  public void run() {
161  Log.d(LOG_TAG, "(HVArrangement)Width not stable yet... trying again");
162  setChildWidth(component, fWidth, trycount + 1);
163  }
164  }, 100); // Try again in 1/10 of a second
165  }
166  if (width <= LENGTH_PERCENT_TAG) {
167  Log.d(LOG_TAG, "HVArrangement.setChildWidth(): width = " + width + " parent Width = " + cWidth + " child = " + component);
168  width = cWidth * (- (width - LENGTH_PERCENT_TAG)) / 100;
169  }
170 
171  component.setLastWidth(width);
172 
175  } else {
176  ViewUtil.setChildWidthForVerticalLayout(component.getView(), width);
177  }
178  }
179 
180  @Override
181  public void setChildHeight(final AndroidViewComponent component, int height) {
182  int cHeight = container.$form().Height();
183  if (cHeight == 0) { // Not ready yet...
184  final int fHeight = height;
185  androidUIHandler.postDelayed(new Runnable() {
186  @Override
187  public void run() {
188  Log.d(LOG_TAG, "(HVArrangement)Height not stable yet... trying again");
189  setChildHeight(component, fHeight);
190  }
191  }, 100); // Try again in 1/10 of a second
192  }
193  if (height <= LENGTH_PERCENT_TAG) {
194  height = cHeight * (- (height - LENGTH_PERCENT_TAG)) / 100;
195  }
196 
197  component.setLastHeight(height);
198 
201  } else {
202  ViewUtil.setChildHeightForVerticalLayout(component.getView(), height);
203  }
204  }
205 
206  // AndroidViewComponent implementation
207 
208  @Override
209  public View getView() {
210  return frameContainer; //: viewLayout.getLayoutManager();
211  }
212 
213  // The numeric encodings are defined Component Constants
214 
220  category = PropertyCategory.APPEARANCE,
221  description = "A number that encodes how contents of the %type% are aligned " +
222  " horizontally. The choices are: 1 = left aligned, 2 = right aligned, " +
223  " 3 = horizontally centered. Alignment has no effect if the arrangement's width is " +
224  "automatic.")
225  public int AlignHorizontal() {
226  return horizontalAlignment;
227  }
228 
239  public void AlignHorizontal(int alignment) {
240  try {
241  // notice that the throw will prevent the alignment from being changed
242  // if the argument is illegal
243  alignmentSetter.setHorizontalAlignment(alignment);
244  horizontalAlignment = alignment;
245  } catch (IllegalArgumentException e) {
246  container.$form().dispatchErrorOccurredEvent(this, "HorizontalAlignment",
248  }
249  }
250 
257  category = PropertyCategory.APPEARANCE,
258  description = "A number that encodes how the contents of the %type% are aligned " +
259  " vertically. The choices are: 1 = aligned at the top, 2 = vertically centered, " +
260  "3 = aligned at the bottom. Alignment has no effect if the arrangement's height " +
261  "is automatic.")
262  public int AlignVertical() {
263  return verticalAlignment;
264  }
265 
276  public void AlignVertical(int alignment) {
277  try {
278  // notice that the throw will prevent the alignment from being changed
279  // if the argument is illegal
280  alignmentSetter.setVerticalAlignment(alignment);
281  verticalAlignment = alignment;
282  } catch (IllegalArgumentException e) {
283  container.$form().dispatchErrorOccurredEvent(this, "VerticalAlignment",
285  }
286  }
287 
295  description = "Returns the background color of the %type%")
296  public int BackgroundColor() {
297  return backgroundColor;
298  }
299 
311  defaultValue = Component.DEFAULT_VALUE_COLOR_DEFAULT)
312  @SimpleProperty(description = "Specifies the background color of the %type%. " +
313  "The background color will not be visible if an Image is being displayed.")
314  public void BackgroundColor(int argb) {
315  backgroundColor = argb;
316 // getView().setBackgroundColor(argb);
317  updateAppearance();
318 
319  }
320 
327  category = PropertyCategory.APPEARANCE)
328  public String Image() {
329  return imagePath;
330  }
331 
341  @DesignerProperty(editorType = PropertyTypeConstants.PROPERTY_TYPE_ASSET, defaultValue = "")
342  @SimpleProperty(description = "Specifies the path of the background image for the %type%. " +
343  "If there is both an Image and a BackgroundColor, only the Image will be visible.")
344  public void Image(String path) {
345  // If it's the same as on the prior call and the prior load was successful,
346  // do nothing.
347  if (path.equals(imagePath) && backgroundImageDrawable != null) {
348  return;
349  }
350 
351  imagePath = (path == null) ? "" : path;
352 
353  // Clear the prior background image.
354  backgroundImageDrawable = null;
355 
356  // Load image from file.
357  if (imagePath.length() > 0) {
358  try {
359  backgroundImageDrawable = MediaUtil.getBitmapDrawable(container.$form(), imagePath);
360  } catch (IOException ioe) {
361  // Fall through with a value of null for backgroundImageDrawable.
362  }
363  }
364 
365  // Update the appearance based on the new value of backgroundImageDrawable.
366  updateAppearance();
367  }
368 
369 
370  // Update appearance based on values of backgroundImageDrawable, backgroundColor and shape.
371  // Images take precedence over background colors.
372  private void updateAppearance() {
373  // If there is no background image,
374  // the appearance depends solely on the background color and shape.
375  if (backgroundImageDrawable == null) {
376  if (backgroundColor == Component.COLOR_DEFAULT) {
377  // If there is no background image and color is default,
378  // restore original 3D bevel appearance.
379  ViewUtil.setBackgroundDrawable(viewLayout.getLayoutManager(), defaultButtonDrawable);
380  } else {
381  // Clear the background image.
382  ViewUtil.setBackgroundDrawable(viewLayout.getLayoutManager(), null);
383  viewLayout.getLayoutManager().setBackgroundColor(backgroundColor);
384  }
385  } else {
386  // If there is a background image
387  ViewUtil.setBackgroundImage(viewLayout.getLayoutManager(), backgroundImageDrawable);
388  }
389  }
390 
391 }
com.google.appinventor.components.runtime.util.MediaUtil.getBitmapDrawable
static BitmapDrawable getBitmapDrawable(Form form, String mediaPath)
Definition: MediaUtil.java:419
com.google.appinventor.components.runtime.AndroidViewComponent.setLastHeight
void setLastHeight(int height)
Definition: AndroidViewComponent.java:143
com.google.appinventor.components.runtime.Component.COLOR_DEFAULT
static final int COLOR_DEFAULT
Definition: Component.java:68
com.google.appinventor.components.runtime.HVArrangement.setChildHeight
void setChildHeight(final AndroidViewComponent component, int height)
Definition: HVArrangement.java:181
com.google.appinventor.components.runtime.HVArrangement.AlignVertical
int AlignVertical()
Definition: HVArrangement.java:262
com.google.appinventor.components.runtime.HVArrangement.AlignHorizontal
int AlignHorizontal()
Definition: HVArrangement.java:225
com.google.appinventor.components.runtime.util.ErrorMessages.ERROR_BAD_VALUE_FOR_VERTICAL_ALIGNMENT
static final int ERROR_BAD_VALUE_FOR_VERTICAL_ALIGNMENT
Definition: ErrorMessages.java:155
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.annotations.DesignerProperty
Definition: DesignerProperty.java:25
com.google.appinventor.components.runtime.LinearLayout.add
void add(AndroidViewComponent component)
Definition: LinearLayout.java:108
com.google.appinventor.components
com.google.appinventor.components.runtime.HVArrangement.$context
Activity $context()
Definition: HVArrangement.java:135
com.google.appinventor.components.runtime.HVArrangement.AlignHorizontal
void AlignHorizontal(int alignment)
Definition: HVArrangement.java:239
com.google.appinventor.components.runtime.util.MediaUtil
Definition: MediaUtil.java:53
com.google.appinventor.components.runtime.util.AlignmentUtil.setHorizontalAlignment
void setHorizontalAlignment(int alignment)
Definition: AlignmentUtil.java:30
com.google.appinventor.components.runtime.AndroidViewComponent.setLastWidth
void setLastWidth(int width)
Definition: AndroidViewComponent.java:129
com.google.appinventor.components.runtime.util.ViewUtil.setChildWidthForHorizontalLayout
static void setChildWidthForHorizontalLayout(View view, int width)
Definition: ViewUtil.java:40
com.google.appinventor.components.runtime.Component.LENGTH_PERCENT_TAG
static final int LENGTH_PERCENT_TAG
Definition: Component.java:123
com.google.appinventor.components.runtime.Component.LAYOUT_ORIENTATION_VERTICAL
static final int LAYOUT_ORIENTATION_VERTICAL
Definition: Component.java:95
com.google.appinventor.components.runtime.ComponentContainer.$add
void $add(AndroidViewComponent component)
com.google.appinventor.components.runtime.HVArrangement.setChildWidth
void setChildWidth(final AndroidViewComponent component, int width, final int trycount)
Definition: HVArrangement.java:154
com.google.appinventor.components.runtime.util.ViewUtil.setChildWidthForVerticalLayout
static void setChildWidthForVerticalLayout(View view, int width)
Definition: ViewUtil.java:89
com.google.appinventor.components.runtime.HVArrangement.$form
Form $form()
Definition: HVArrangement.java:140
com.google.appinventor.components.runtime.util.AlignmentUtil
Definition: AlignmentUtil.java:18
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.common.PropertyTypeConstants.PROPERTY_TYPE_HORIZONTAL_ALIGNMENT
static final String PROPERTY_TYPE_HORIZONTAL_ALIGNMENT
Definition: PropertyTypeConstants.java:42
com.google.appinventor.components.common.ComponentConstants.EMPTY_HV_ARRANGEMENT_WIDTH
static final int EMPTY_HV_ARRANGEMENT_WIDTH
Definition: ComponentConstants.java:31
com.google.appinventor.components.runtime.util.ErrorMessages.ERROR_BAD_VALUE_FOR_HORIZONTAL_ALIGNMENT
static final int ERROR_BAD_VALUE_FOR_HORIZONTAL_ALIGNMENT
Definition: ErrorMessages.java:154
com.google.appinventor.components.runtime.HVArrangement.getView
View getView()
Definition: HVArrangement.java:209
com.google.appinventor.components.runtime.Component.LAYOUT_ORIENTATION_HORIZONTAL
static final int LAYOUT_ORIENTATION_HORIZONTAL
Definition: Component.java:94
com.google.appinventor.components.runtime.HVArrangement.BackgroundColor
int BackgroundColor()
Definition: HVArrangement.java:296
com.google.appinventor.components.runtime.HVArrangement.AlignVertical
void AlignVertical(int alignment)
Definition: HVArrangement.java:276
com.google.appinventor.components.annotations.SimpleProperty
Definition: SimpleProperty.java:23
com.google.appinventor.components.runtime.HVArrangement.$add
void $add(AndroidViewComponent component)
Definition: HVArrangement.java:145
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.LinearLayout.setBaselineAligned
void setBaselineAligned(boolean baselineAligned)
Definition: LinearLayout.java:123
com.google.appinventor.components.runtime.LinearLayout.getLayoutManager
ViewGroup getLayoutManager()
Definition: LinearLayout.java:104
com.google.appinventor.components.runtime.Form.Height
int Height()
Definition: Form.java:1948
com.google.appinventor.components.runtime.ComponentContainer
Definition: ComponentContainer.java:16
com.google.appinventor.components.runtime.util.AlignmentUtil.setVerticalAlignment
void setVerticalAlignment(int alignment)
Definition: AlignmentUtil.java:51
com.google.appinventor.components.runtime
Copyright 2009-2011 Google, All Rights reserved.
Definition: AccelerometerSensor.java:8
com.google.appinventor.components.runtime.Form.Width
int Width()
Definition: Form.java:1936
com.google.appinventor.components.runtime.Component
Definition: Component.java:17
com.google.appinventor.components.common
Definition: ComponentCategory.java:7
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.common.ComponentConstants.LAYOUT_ORIENTATION_HORIZONTAL
static final int LAYOUT_ORIENTATION_HORIZONTAL
Definition: ComponentConstants.java:26
com.google.appinventor.components.common.ComponentConstants.VERTICAL_ALIGNMENT_DEFAULT
static final int VERTICAL_ALIGNMENT_DEFAULT
Definition: ComponentConstants.java:64
com.google.appinventor.components.annotations.SimpleObject
Definition: SimpleObject.java:23
com.google.appinventor.components.runtime.AndroidViewComponent.getView
abstract View getView()
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_COLOR
static final String PROPERTY_TYPE_COLOR
Definition: PropertyTypeConstants.java:63
com.google.appinventor.components.runtime.HVArrangement
Definition: HVArrangement.java:48
com.google.appinventor.components.runtime.AndroidViewComponent.container
final ComponentContainer container
Definition: AndroidViewComponent.java:29
com.google.appinventor.components.runtime.util.ViewUtil.setChildHeightForVerticalLayout
static void setChildHeightForVerticalLayout(View view, int height)
Definition: ViewUtil.java:113
com.google
com
com.google.appinventor.components.common.ComponentConstants.HORIZONTAL_ALIGNMENT_DEFAULT
static final int HORIZONTAL_ALIGNMENT_DEFAULT
Definition: ComponentConstants.java:63
com.google.appinventor.components.common.ComponentConstants.EMPTY_HV_ARRANGEMENT_HEIGHT
static final int EMPTY_HV_ARRANGEMENT_HEIGHT
Definition: ComponentConstants.java:32
com.google.appinventor.components.runtime.util.ViewUtil.setChildHeightForHorizontalLayout
static void setChildHeightForHorizontalLayout(View view, int height)
Definition: ViewUtil.java:66
com.google.appinventor.components.runtime.ComponentContainer.$form
Form $form()
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_VERTICAL_ALIGNMENT
static final String PROPERTY_TYPE_VERTICAL_ALIGNMENT
Definition: PropertyTypeConstants.java:43
com.google.appinventor.components.runtime.ComponentContainer.$context
Activity $context()
com.google.appinventor.components.common.ComponentConstants
Definition: ComponentConstants.java:13
com.google.appinventor.components.runtime.HVArrangement.setChildWidth
void setChildWidth(final AndroidViewComponent component, int width)
Definition: HVArrangement.java:150
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.Form
Definition: Form.java:126
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_ASSET
static final String PROPERTY_TYPE_ASSET
Definition: PropertyTypeConstants.java:22
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.components.runtime.HVArrangement.HVArrangement
HVArrangement(ComponentContainer container, int orientation, boolean scrollable)
Definition: HVArrangement.java:84
com.google.appinventor.components.runtime.Image
Definition: Image.java:52
com.google.appinventor
com.google.appinventor.components.runtime.util.ViewUtil
Definition: ViewUtil.java:22