AI2 Component  (Version nb184)
ViewUtil.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.util;
8 
10 
11 import android.graphics.drawable.Drawable;
12 import android.util.Log;
13 import android.view.View;
14 import android.widget.ImageView;
15 import android.widget.LinearLayout;
16 import android.widget.TableRow;
17 
22 public final class ViewUtil {
23 
24  private ViewUtil() {
25  }
26 
35  private static int calculatePixels(View view, int sizeInDP) {
36  return (int) (view.getContext().getResources().getDisplayMetrics().density * sizeInDP);
37  }
38 
39 
40  public static void setChildWidthForHorizontalLayout(View view, int width) {
41  // In a horizontal layout, if a child's width is set to fill parent, we must set the
42  // LayoutParams width to 0 and the weight to 1. For other widths, we set the weight to 0
43  Object layoutParams = view.getLayoutParams();
44  if (layoutParams instanceof LinearLayout.LayoutParams) {
45  LinearLayout.LayoutParams linearLayoutParams = (LinearLayout.LayoutParams) layoutParams;
46  switch (width) {
48  linearLayoutParams.width = LinearLayout.LayoutParams.WRAP_CONTENT;
49  linearLayoutParams.weight = 0;
50  break;
52  linearLayoutParams.width = 0;
53  linearLayoutParams.weight = 1;
54  break;
55  default:
56  linearLayoutParams.width = calculatePixels(view, width);
57  linearLayoutParams.weight = 0;
58  break;
59  }
60  view.requestLayout();
61  } else {
62  Log.e("ViewUtil", "The view does not have linear layout parameters");
63  }
64  }
65 
66  public static void setChildHeightForHorizontalLayout(View view, int height) {
67  // In a horizontal layout, if a child's height is set to fill parent, we can simply set the
68  // LayoutParams height to fill parent.
69  Object layoutParams = view.getLayoutParams();
70  if (layoutParams instanceof LinearLayout.LayoutParams) {
71  LinearLayout.LayoutParams linearLayoutParams = (LinearLayout.LayoutParams) layoutParams;
72  switch (height) {
74  linearLayoutParams.height = LinearLayout.LayoutParams.WRAP_CONTENT;
75  break;
77  linearLayoutParams.height = LinearLayout.LayoutParams.FILL_PARENT;
78  break;
79  default:
80  linearLayoutParams.height = calculatePixels(view, height);
81  break;
82  }
83  view.requestLayout();
84  } else {
85  Log.e("ViewUtil", "The view does not have linear layout parameters");
86  }
87  }
88 
89  public static void setChildWidthForVerticalLayout(View view, int width) {
90  // In a vertical layout, if a child's width is set to fill parent, we can simply set the
91  // LayoutParams width to fill parent.
92  Object layoutParams = view.getLayoutParams();
93  if (layoutParams instanceof LinearLayout.LayoutParams) {
94  LinearLayout.LayoutParams linearLayoutParams = (LinearLayout.LayoutParams) layoutParams;
95  switch (width) {
97  linearLayoutParams.width = LinearLayout.LayoutParams.WRAP_CONTENT;
98  break;
100  linearLayoutParams.width = LinearLayout.LayoutParams.FILL_PARENT;
101  break;
102  default:
103  linearLayoutParams.width = calculatePixels(view, width);
104  break;
105  }
106 // System.err.println("ViewUtil: setChildWidthForVerticalLayout: view = " + view + " width = " + width);
107  view.requestLayout();
108  } else {
109  Log.e("ViewUtil", "The view does not have linear layout parameters");
110  }
111  }
112 
113  public static void setChildHeightForVerticalLayout(View view, int height) {
114  // In a vertical layout, if a child's height is set to fill parent, we must set the
115  // LayoutParams height to 0 and the weight to 1. For other heights, we set the weight to 0
116  Object layoutParams = view.getLayoutParams();
117  if (layoutParams instanceof LinearLayout.LayoutParams) {
118  LinearLayout.LayoutParams linearLayoutParams = (LinearLayout.LayoutParams) layoutParams;
119  switch (height) {
121  linearLayoutParams.height = LinearLayout.LayoutParams.WRAP_CONTENT;
122  linearLayoutParams.weight = 0;
123  break;
125  linearLayoutParams.height = 0;
126  linearLayoutParams.weight = 1;
127  break;
128  default:
129  linearLayoutParams.height = calculatePixels(view, height);
130  linearLayoutParams.weight = 0;
131  break;
132  }
133  view.requestLayout();
134  } else {
135  Log.e("ViewUtil", "The view does not have linear layout parameters");
136  }
137  }
138 
139  public static void setChildWidthForTableLayout(View view, int width) {
140  Object layoutParams = view.getLayoutParams();
141  if (layoutParams instanceof TableRow.LayoutParams) {
142  TableRow.LayoutParams tableLayoutParams = (TableRow.LayoutParams) layoutParams;
143  switch (width) {
145  tableLayoutParams.width = TableRow.LayoutParams.WRAP_CONTENT;
146  break;
148  tableLayoutParams.width = TableRow.LayoutParams.FILL_PARENT;
149  break;
150  default:
151  tableLayoutParams.width = calculatePixels(view, width);
152  break;
153  }
154  view.requestLayout();
155  } else {
156  Log.e("ViewUtil", "The view does not have table layout parameters");
157  }
158  }
159 
160  public static void setChildHeightForTableLayout(View view, int height) {
161  Object layoutParams = view.getLayoutParams();
162  if (layoutParams instanceof TableRow.LayoutParams) {
163  TableRow.LayoutParams tableLayoutParams = (TableRow.LayoutParams) layoutParams;
164  switch (height) {
166  tableLayoutParams.height = TableRow.LayoutParams.WRAP_CONTENT;
167  break;
169  tableLayoutParams.height = TableRow.LayoutParams.FILL_PARENT;
170  break;
171  default:
172  tableLayoutParams.height = calculatePixels(view, height);
173  break;
174  }
175  view.requestLayout();
176  } else {
177  Log.e("ViewUtil", "The view does not have table layout parameters");
178  }
179  }
180 
184  public static void setBackgroundImage(View view, Drawable drawable) {
185  view.setBackgroundDrawable(drawable);
186  view.requestLayout();
187  }
188 
192  public static void setImage(ImageView view, Drawable drawable) {
193  view.setImageDrawable(drawable);
194  if (drawable != null) {
195  view.setAdjustViewBounds(true);
196  }
197  view.requestLayout();
198  }
199 
200  public static void setBackgroundDrawable(View view, Drawable drawable) {
201  view.setBackgroundDrawable(drawable);
202  view.invalidate();
203  }
204 }
com.google.appinventor.components
com.google.appinventor.components.runtime.Component.LENGTH_FILL_PARENT
static final int LENGTH_FILL_PARENT
Definition: Component.java:119
com.google.appinventor.components.runtime.util.ViewUtil.setBackgroundImage
static void setBackgroundImage(View view, Drawable drawable)
Definition: ViewUtil.java:184
com.google.appinventor.components.runtime.util.ViewUtil.setChildWidthForHorizontalLayout
static void setChildWidthForHorizontalLayout(View view, int width)
Definition: ViewUtil.java:40
com.google.appinventor.components.runtime.util.ViewUtil.setChildHeightForTableLayout
static void setChildHeightForTableLayout(View view, int height)
Definition: ViewUtil.java:160
com.google.appinventor.components.runtime.util.ViewUtil.setChildWidthForVerticalLayout
static void setChildWidthForVerticalLayout(View view, int width)
Definition: ViewUtil.java:89
com.google.appinventor.components.runtime.util.ViewUtil.setImage
static void setImage(ImageView view, Drawable drawable)
Definition: ViewUtil.java:192
com.google.appinventor.components.runtime.Component.LENGTH_PREFERRED
static final int LENGTH_PREFERRED
Definition: Component.java:118
com.google.appinventor.components.runtime.util.ViewUtil.setBackgroundDrawable
static void setBackgroundDrawable(View view, Drawable drawable)
Definition: ViewUtil.java:200
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.util.ViewUtil.setChildHeightForVerticalLayout
static void setChildHeightForVerticalLayout(View view, int height)
Definition: ViewUtil.java:113
com.google.appinventor.components.runtime.util.ViewUtil.setChildWidthForTableLayout
static void setChildWidthForTableLayout(View view, int width)
Definition: ViewUtil.java:139
com.google
com
com.google.appinventor.components.runtime.util.ViewUtil.setChildHeightForHorizontalLayout
static void setChildHeightForHorizontalLayout(View view, int height)
Definition: ViewUtil.java:66
com.google.appinventor.components.runtime.LinearLayout
Definition: LinearLayout.java:20
com.google.appinventor
com.google.appinventor.components.runtime.util.ViewUtil
Definition: ViewUtil.java:22