Every Android view element has to contain the android:layout_width and the android:layout_height attributes in the XML. The value of these attributes can be the following:
fill_parent: with this value, the size (width or height) of the textView fill the parent that is containing it. If the parent is a LinearLayout that is filling the entire Android screen, the Textview will fill the screen.
wrap_content: this is not such a possessive value as the “fill_parent" one; here the size of the TextView is fixed to the content. If you place a short text inside, the TextView itself will be short.
- numericalue + pixels: just that, the size numerical value of your TextView in pixels.
These are generic attributes, but let's have a look at the specific ones.
android:text="Here goes the text you want to write"
Using Android code you can use the:
myTextView.setText("ChaSequence of Text");
or if you want to make reference to a string from the “string.xml" file you can do the following:
“hello" is defined in string.xml. As you can see we use the “getString" method, this is an Activity method, related to the context I explained some lines above.
Once we have the text, we can shape it to whatever we want (inside of the platform limits, of course).
Here are some attributes related to the text customization:
android:textColor. You have to use a color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb". You can find more information about this kind of color values in the following link:
RGB to Color Name Mapping (Triplet and Hex)
Using the Android code:
or if you want to use a custom color:
But, where is the resource “color"?
You have to create it in the values folder, inside resources.
<?xml version="1.0" encoding="utf-8"?>
Here you can place your custom colors.
android:textSize: numeric value + dimension measure (It's recommended to use “sp", scaled-pixels, instead of “px" as measure)
in Android code
“setTextSize" is the Units of the text and the second attribute the value. The TypedValue class provides the dimension units.
android:textStyle – Defines the style of the text. Here 3 values are allowed: bold, normal or italics.
android:typeface – Defines the typeface of the text. Here we have 4 values: monospace, serif, sans and normal.
In Android code it will be something like:
Here we define not only the style of the font (bold) but the typeface too. In this case we use a “Serif" typeface.
In this image you can see different texts with corresponding styles.