Android UI Tutorial - Android TextView








The TextView view is used to display text to the user. This is the most basic view and one that you will frequently use when you develop Android applications.

If you need to allow users to edit the text displayed, you should use the subclass of TextView, EditText.

In some other platforms, the TextView is commonly known as the label view. Its sole purpose is to display text on the screen.

Example

The following layout xml resource file has a definition for TextView.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

</LinearLayout>




Reference color resource in TextView

The following code shows how to Reference color resource in TextView

res/values/colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="background_color">#aa0000</color>
</resources>

Layout xml

<?xml version="1.0" encoding="utf-8"?>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/background_color"
    android:gravity="center"
    android:text="Hello World, MainActivity!" />

null




Set text for TextView in XML

The following example shows how to Set text for TextView in XML.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Example from java2s.com"
        />
</LinearLayout>

Java code

package com.java2s.app;
//  www  .  j  av a2  s.c  o m
import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}
null