7 package com.google.appinventor.components.runtime.util;
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;
35 private static int calculatePixels(View view,
int sizeInDP) {
36 return (
int) (view.getContext().getResources().getDisplayMetrics().density * sizeInDP);
43 Object layoutParams = view.getLayoutParams();
48 linearLayoutParams.width =
LinearLayout.LayoutParams.WRAP_CONTENT;
49 linearLayoutParams.weight = 0;
52 linearLayoutParams.width = 0;
53 linearLayoutParams.weight = 1;
56 linearLayoutParams.width = calculatePixels(view, width);
57 linearLayoutParams.weight = 0;
62 Log.e(
"ViewUtil",
"The view does not have linear layout parameters");
69 Object layoutParams = view.getLayoutParams();
74 linearLayoutParams.height =
LinearLayout.LayoutParams.WRAP_CONTENT;
77 linearLayoutParams.height =
LinearLayout.LayoutParams.FILL_PARENT;
80 linearLayoutParams.height = calculatePixels(view, height);
85 Log.e(
"ViewUtil",
"The view does not have linear layout parameters");
92 Object layoutParams = view.getLayoutParams();
97 linearLayoutParams.width =
LinearLayout.LayoutParams.WRAP_CONTENT;
100 linearLayoutParams.width =
LinearLayout.LayoutParams.FILL_PARENT;
103 linearLayoutParams.width = calculatePixels(view, width);
107 view.requestLayout();
109 Log.e(
"ViewUtil",
"The view does not have linear layout parameters");
116 Object layoutParams = view.getLayoutParams();
117 if (layoutParams instanceof
LinearLayout.LayoutParams) {
121 linearLayoutParams.height =
LinearLayout.LayoutParams.WRAP_CONTENT;
122 linearLayoutParams.weight = 0;
125 linearLayoutParams.height = 0;
126 linearLayoutParams.weight = 1;
129 linearLayoutParams.height = calculatePixels(view, height);
130 linearLayoutParams.weight = 0;
133 view.requestLayout();
135 Log.e(
"ViewUtil",
"The view does not have linear layout parameters");
140 Object layoutParams = view.getLayoutParams();
141 if (layoutParams instanceof TableRow.LayoutParams) {
142 TableRow.LayoutParams tableLayoutParams = (TableRow.LayoutParams) layoutParams;
145 tableLayoutParams.width = TableRow.LayoutParams.WRAP_CONTENT;
148 tableLayoutParams.width = TableRow.LayoutParams.FILL_PARENT;
151 tableLayoutParams.width = calculatePixels(view, width);
154 view.requestLayout();
156 Log.e(
"ViewUtil",
"The view does not have table layout parameters");
161 Object layoutParams = view.getLayoutParams();
162 if (layoutParams instanceof TableRow.LayoutParams) {
163 TableRow.LayoutParams tableLayoutParams = (TableRow.LayoutParams) layoutParams;
166 tableLayoutParams.height = TableRow.LayoutParams.WRAP_CONTENT;
169 tableLayoutParams.height = TableRow.LayoutParams.FILL_PARENT;
172 tableLayoutParams.height = calculatePixels(view, height);
175 view.requestLayout();
177 Log.e(
"ViewUtil",
"The view does not have table layout parameters");
185 view.setBackgroundDrawable(drawable);
186 view.requestLayout();
192 public static void setImage(ImageView view, Drawable drawable) {
193 view.setImageDrawable(drawable);
194 if (drawable !=
null) {
195 view.setAdjustViewBounds(
true);
197 view.requestLayout();
201 view.setBackgroundDrawable(drawable);