Learn Android UI

TextView With Example In Android Studio

In Android, TextView displays text to the user and optionally allows them to edit it programmatically. TextView is a complete text editor, however basic class is configured to not allow editing but we can edit it.

TextView Size of Plain, Large, Medium, and Small Text Android Studio
View is the parent class of TextView. Being a subclass of view the text view component can be used in your app’s GUI inside a ViewGroup, or as the content view of an activity.

We can create a TextView instance by declaring it inside a layout(XML file) or by instantiating it programmatically(Java Class).

TextView code in XML:

<TextView android:id="@+id/simpleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AbhiAndroid" />

TextView code in JAVA:

TextView textView = (TextView) findViewById(R.id.textView);
textView.setText("AbhiAndroid"); //set text for text view

Attributes of TextView:

Now let’s we discuss about the attributes that helps us to configure a TextView in your xml file.

1. id: id is an attribute used to uniquely identify a text view. Below is the example code in which we set the id of a text view.

<TextView
android:id="@+id/simpleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

2. gravity: The gravity attribute is an optional attribute which is used to control the alignment of the text like left, right, center, top, bottom, center_vertical, center_horizontal etc.

Below is the example code with explanation included in which we set the center_horizontal gravity for text of a TextView.

<TextView
android:id="@+id/simpleTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="AbhiAndroid"
android:textSize="20sp"
android:gravity="center_horizontal"/> <!--center horizontal gravity-->

Gravity in TextView Android
3. text: text attribute is used to set the text in a text view. We can set the text in xml as well as in the java class.

Below is the example code with explanation included in which we set the text “AbhiAndroid” in a text view.

<TextView
android:id="@+id/simpleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="25sp"
android:text="AbhiAndroid"/><!--Display Text as AbhiAndroid-->

text attribute in TextView Android
In Java class:

Below is the example code in which we set the text in a textview programmatically means in java class.

TextView textView = (TextView)findViewById(R.id.textView);
textView.setText("AbhiAndroid"); //set text for text view

4. textColor: textColor attribute is used to set the text color of a text view. Color value is in the form of “#argb”, “#rgb”, “#rrggbb”, or “#aarrggbb”.

Below is the example code with explanation included in which we set the red color for the displayed text.

<TextView
android:id="@+id/simpleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AbhiAndroid"
android:layout_centerInParent="true"
android:textSize="25sp"
android:textColor="#f00"/><!--red color for text view-->

textcolor in TextView
In Java class:

Below is the example code in which we set the text color of a text view programmatically means in java class.

TextView textView = (TextView)findViewById(R.id.textView);
textView.setTextColor(Color.RED); //set red color for text view

5. textSize: textSize attribute is used to set the size of text of a text view. We can set the text size in sp(scale independent pixel) or dp(density pixel).

Below is the example code in which we set the 20sp size for the text of a text view.

<TextView
    android:id="@+id/simpleTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="AbhiAndroid"
    android:layout_centerInParent="true"
    android:textSize="40sp" /><!--Set size-->

textsize attribute in TextView
In Java class:

Below is the example code in which we set the text size of a text view programmatically means in java class.

TextView textView = (TextView)findViewById(R.id.textView);
textView.setTextSize(20); //set 20sp size of text

6. textStyle: textStyle attribute is used to set the text style of a text view. The possible text styles are bold, italic and normal.  If we need to use two or more styles for a text view then “|” operator is used for that.

Below is the example code with explanation included in which we  set the bold and italic text styles for text.

<TextView
    android:id="@+id/simpleTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="AbhiAndroid"
    android:layout_centerInParent="true"
    android:textSize="40sp"
    android:textStyle="bold|italic"/><!--bold and italic text style of text-->

textStyle in TextView
7. background: background attribute is used to set the background of a text view. We can set a color or a drawable in the background of a text view.

8. padding: padding attribute is used to set the padding from left, right, top or bottom. In above example code of background we also set the 10dp padding from all the side’s of text view.

Below is the example code with explanation included in which we set the black color for the background, white color for the displayed text and set 10dp padding from all the side’s for text view.

<TextView
android:id="@+id/simpleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AbhiAndroid"
android:layout_centerInParent="true"
android:textSize="40sp" 
android:padding="10dp"
android:textColor="#fff"
android:background="#000"/> <!--red color for background of text view-->

padding and background attribute in TextView
In Java class:

Below is the example code in which we set the background color of a text view programmatically means in java class.

TextView textView = (TextView)findViewById(R.id.textView);
textView.setBackgroundColor(Color.BLACK);//set background color

Example of TextView:

Below is the example of TextView in which we display a text view and set the text in xml file and then change the text on button click event programmatically. Below is the final output and code:

Download Code ?

TextView Example in Android Studio
Step 1: Create a new project and name it textViewExample.

Select File -> New -> New Project. Fill the forms and click "Finish" button.

Step 2: Open res -> layout -> xml (or) activity_main.xml and add following code. Here we will create a button and a textview in Relative Layout.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/simpleTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="Before Clicking"
        android:textColor="#f00"
        android:textSize="25sp"
        android:textStyle="bold|italic"
        android:layout_marginTop="50dp"/>

    <Button
        android:id="@+id/btnChangeText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="#f00"
        android:padding="10dp"
        android:text="Change Text"
        android:textColor="#fff"
        android:textStyle="bold" />
</RelativeLayout>

Step 3: Open app -> java -> package and open MainActivity.java and add the following code. Here we will change the text of TextView after the user click on Button.

package example.abhiandriod.textviewexample;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); //set the layout
        final TextView simpleTextView = (TextView) findViewById(R.id.simpleTextView); //get the id for TextView
        Button changeText = (Button) findViewById(R.id.btnChangeText); //get the id for button
        changeText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                simpleTextView.setText("After Clicking"); //set the text after clicking button
            }
        });
    }


}

Output:

Now run the app in Emulator and click on the button. You will see text will change “After Clicking”.

TextView Example Output

DOWNLOAD THIS FREE eBook!

This free eBook will help you master the learning of Android App Development in Android Studio!

10 thoughts on “TextView With Example In Android Studio”

  1. In the padding section… in the code example… the last line of code:
    android:background=”#000″/>
    the comment says it’s a red color but the hex code indicates black…

    and thanks for this site i already bookmarked it hehe

Leave a Reply to Jaskaran Singh Cancel reply

Your email address will not be published. Required fields are marked *



Continue Reading:

Download Free - Master Android App Development Sidebar

DOWNLOAD THIS FREE eBook!

This free eBook will help you master the learning of Android App Development in Android Studio!
close-link

Android Developer Facebook Group Free

Premium Project Source Code:




DOWNLOAD THIS FREE eBook!

This free eBook will help you master the learning of Android App Development in Android Studio!
close-link

DOWNLOAD THIS FREE eBook!

This free eBook will help you master the learning of Android App Development in Android Studio!
close-link

DOWNLOAD THIS FREE eBook!

This free eBook will help you master the learning of Android App Development in Android Studio!
close-link

See How AbhiAndroid Step By Step Video Training Helps You Master Android App Development

Video Training - Unlock step by step video training with new content added regularly. Develop Android Apps.
Android App Source Code - Get amazing Ecommerce, Food Ordering and Ultimate WebView source code with documentation.
GET ACCESS NOW
close-link

With a very poor revenue from selling source code files or using Google AdSense, we need your help to survive this website. Please contribute any amount you can afford
Pay
* Terms & Conditions Apply
close-link