AI2 Component  (Version nb184)
TextViewUtil.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.content.res.ColorStateList;
12 import android.graphics.Typeface;
13 import android.view.Gravity;
14 import android.widget.TextView;
15 import android.text.Html;
16 import android.content.Context;
17 
22 public class TextViewUtil {
23 
24  private TextViewUtil() {
25  }
26 
35  public static void setAlignment(TextView textview, int alignment, boolean centerVertically) {
36  int horizontalGravity;
37  switch (alignment) {
38  default:
39  throw new IllegalArgumentException();
40 
42  horizontalGravity = Gravity.LEFT;
43  break;
44 
46  horizontalGravity = Gravity.CENTER_HORIZONTAL;
47  break;
48 
50  horizontalGravity = Gravity.RIGHT;
51  break;
52  }
53  int verticalGravity = centerVertically ? Gravity.CENTER_VERTICAL : Gravity.TOP;
54  textview.setGravity(horizontalGravity | verticalGravity);
55  textview.invalidate();
56  }
57 
66  public static void setBackgroundColor(TextView textview, int argb) {
67  textview.setBackgroundColor(argb);
68  textview.invalidate();
69  }
70 
77  public static boolean isEnabled(TextView textview) {
78  return textview.isEnabled();
79  }
80 
87  public static void setEnabled(TextView textview, boolean enabled) {
88  textview.setEnabled(enabled);
89  textview.invalidate();
90  }
91 
99  public static float getFontSize(TextView textview, Context context) {
100  float scaledDensity = context.getResources().getDisplayMetrics().scaledDensity;
101  return textview.getTextSize()/scaledDensity;
102  }
103 
110  public static void setFontSize(TextView textview, float size) {
111  textview.setTextSize(size);
112  textview.requestLayout();
113  }
114 
126  public static void setFontTypeface(TextView textview, int typeface,
127  boolean bold, boolean italic) {
128  Typeface tf;
129  switch (typeface) {
130  default:
131  throw new IllegalArgumentException();
132 
134  tf = Typeface.DEFAULT;
135  break;
136 
138  tf = Typeface.SERIF;
139  break;
140 
142  tf = Typeface.SANS_SERIF;
143  break;
144 
146  tf = Typeface.MONOSPACE;
147  break;
148  }
149 
150  int style = 0;
151  if (bold) {
152  style |= Typeface.BOLD;
153  }
154  if (italic) {
155  style |= Typeface.ITALIC;
156  }
157  textview.setTypeface(Typeface.create(tf, style));
158  textview.requestLayout();
159  }
160 
167  public static String getText(TextView textview) {
168  return textview.getText().toString();
169  }
170 
177  public static void setTextHTML(TextView textview, String text) {
178  textview.setText(Html.fromHtml(text));
179  textview.requestLayout();
180  }
181 
188  public static void setText(TextView textview, String text) {
189  textview.setText(text);
190  textview.requestLayout();
191  }
192 
199  public static void setPadding(TextView textview, int padding) {
200  textview.setPadding(padding, padding, 0, 0);
201  textview.requestLayout();
202  }
203 
210  public static void setTextColor(TextView textview, int argb) {
211  textview.setTextColor(argb);
212  textview.invalidate();
213  }
214 
215  public static void setTextColors(TextView textview, ColorStateList colorStateList) {
216  textview.setTextColor(colorStateList);
217  }
218 
225  public static void setMinWidth(TextView textview, int minWidth) {
226  // According to https://developer.android.com/reference/android/widget/TextView.html#setMinWidth(int), the minimum
227  // width of TextView is the maximum of setMinWidth and setMinimumWidth. Talk about NIH syndrome!
228  textview.setMinWidth(minWidth);
229  textview.setMinimumWidth(minWidth);
230  }
231 
238  public static void setMinHeight(TextView textview, int minHeight) {
239  // According to https://developer.android.com/reference/android/widget/TextView.html#setMinHeight(int), the minimum
240  // height of TextView is the maximum of setMinHeight and setMinimumHeight. Talk about NIH syndrome!
241  textview.setMinHeight(minHeight);
242  textview.setMinimumHeight(minHeight);
243  }
244 
252  public static void setMinSize(TextView textview, int minWidth, int minHeight) {
253  TextViewUtil.setMinWidth(textview, minWidth);
254  TextViewUtil.setMinHeight(textview, minHeight);
255  }
256 }
com.google.appinventor.components.runtime.util.TextViewUtil.setText
static void setText(TextView textview, String text)
Definition: TextViewUtil.java:188
com.google.appinventor.components.runtime.util.TextViewUtil.setMinHeight
static void setMinHeight(TextView textview, int minHeight)
Definition: TextViewUtil.java:238
com.google.appinventor.components.runtime.util.TextViewUtil.setTextHTML
static void setTextHTML(TextView textview, String text)
Definition: TextViewUtil.java:177
com.google.appinventor.components
com.google.appinventor.components.runtime.Component.TYPEFACE_SERIF
static final int TYPEFACE_SERIF
Definition: Component.java:108
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.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.util.TextViewUtil
Definition: TextViewUtil.java:22
com.google.appinventor.components.runtime.util.TextViewUtil.setBackgroundColor
static void setBackgroundColor(TextView textview, int argb)
Definition: TextViewUtil.java:66
com.google.appinventor.components.runtime.util.TextViewUtil.setFontSize
static void setFontSize(TextView textview, float size)
Definition: TextViewUtil.java:110
com.google.appinventor.components.runtime
Copyright 2009-2011 Google, All Rights reserved.
Definition: AccelerometerSensor.java:8
com.google.appinventor.components.runtime.util.TextViewUtil.isEnabled
static boolean isEnabled(TextView textview)
Definition: TextViewUtil.java:77
com.google.appinventor.components.runtime.Component
Definition: Component.java:17
com.google.appinventor.components.runtime.util.TextViewUtil.setTextColors
static void setTextColors(TextView textview, ColorStateList colorStateList)
Definition: TextViewUtil.java:215
com.google.appinventor.components.runtime.Component.ALIGNMENT_NORMAL
static final int ALIGNMENT_NORMAL
Definition: Component.java:32
com.google.appinventor.components.runtime.Component.TYPEFACE_DEFAULT
static final int TYPEFACE_DEFAULT
Definition: Component.java:106
com.google
com
com.google.appinventor.components.runtime.Component.TYPEFACE_MONOSPACE
static final int TYPEFACE_MONOSPACE
Definition: Component.java:109
com.google.appinventor.components.runtime.util.TextViewUtil.setEnabled
static void setEnabled(TextView textview, boolean enabled)
Definition: TextViewUtil.java:87
com.google.appinventor.components.runtime.util.TextViewUtil.setPadding
static void setPadding(TextView textview, int padding)
Definition: TextViewUtil.java:199
com.google.appinventor.components.runtime.util.TextViewUtil.setTextColor
static void setTextColor(TextView textview, int argb)
Definition: TextViewUtil.java:210
com.google.appinventor.components.runtime.Component.TYPEFACE_SANSSERIF
static final int TYPEFACE_SANSSERIF
Definition: Component.java:107
com.google.appinventor.components.runtime.util.TextViewUtil.setMinSize
static void setMinSize(TextView textview, int minWidth, int minHeight)
Definition: TextViewUtil.java:252
com.google.appinventor.components.runtime.util.TextViewUtil.setMinWidth
static void setMinWidth(TextView textview, int minWidth)
Definition: TextViewUtil.java:225
com.google.appinventor.components.runtime.Component.ALIGNMENT_OPPOSITE
static final int ALIGNMENT_OPPOSITE
Definition: Component.java:34
com.google.appinventor
com.google.appinventor.components.runtime.Component.ALIGNMENT_CENTER
static final int ALIGNMENT_CENTER
Definition: Component.java:33