Android UI Elements: TextView

written by: Jbeerdev•edited by: Rebecca Scudder•updated: 6/13/2010

The “Android UI Elements” articles attempt to focus and analyze every single UI element in the Android programming environment on both sides: from the xml and from the Android code. In this case, we will start analyzing the most basis UI element: the TextView.

slide 1 of 4

Introduction

I think it is a good idea to have specialized articles about single Android UI elements. These articles will be helpful for people who are starting to develop in the Android platform.

About the TextView, it is the basic element in all Android application, a label in your UI; it is a piece of text.

We can create TextViews in two ways, using the XML and using the Android code.

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/hello"

/>

in the Android code we can write the following:

TextView myTextView = new TextView(this);

“this” is the Context of the application.

Hint - >The Context class is an abstract interface that is provided by the Android system to give us access to system specific resources and classes. The Activity class is a subclass of Context, so that is why we put “this”.

slide 2 of 4

TextView attributes

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.

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:

myTextView.setTypeface(Typeface.SERIF,Typeface.BOLD);

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.

slide 3 of 4

Ask, comment, interact!

Ask any question you have about the article, I will try to answer as fast as I can. Comment my code, my writing, maybe there is something that its missing or is not complete, just let me know!! In other words, interact!!

slide 4 of 4

Follow up

If you want to know when new articles are released, subscribe yourself to the Google Android RSS Otherwise, you can follow my research, articles and work in my professional twitter: jbeerdev