TextView in Android

In this tutorial, we will learn about TextView in Android. Text view is one of the simplest widgets in android. We use Text View to show some simple text to the user. You can show paragraphs using Text View or User Name etc. It totally depends upon the requirement of the Activity. In this lesson, we would learn how to add a Text View in the XML layout file and how we can set text into TextView from our Java.

So without wasting time, we will directly move to our example. In this example, I would show how to add a TextView in the XML layout file.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:text="Welcome to Owlbuddy"
        android:textColor="#86AD33"
        android:textSize="20sp"
        android:textStyle="bold" />
</LinearLayout>

In this example, you can see how to add TextView in the XML layout file. In this example, we used several attributes now I will explain each attribute and why we are using them.

  • id: we are using the id attribute to assign a unique id to Text View
  • width: we are using the width attribute to set the width of the Text View
  • height: we are using the height attribute to set the height of the Text View
  • layout_marginBottom we are using the layout_marginBottom attribute to set the margin from bottom to Text view
  • text: we are using text attribute to set text in the Text View
  • textColor: we are using the textColor attribute to set the color of the text in Text View
  • textSize: we are using the textSize attribute to set the size of the text in Text View
  • textStyle: we are using the textStyle attribute to give style to the text of Text View. In this case, I am setting the style bold to make the text thicker than usual

After looking at this XML layout file example. Let’s jump to our next example. In this example, we will see how we can set text into our Text View using Java code.

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        LinearLayout linearLayout =  (LinearLayout) findViewById(R.id.linearlayout);
        TextView textView = new TextView(this);
        textView.setText("Welcome to Owlbuddy.com");
        linearLayout.addView(textView);
    }
}

We have a TextView class in Java that helps us to create TextView in our Activity. In this example, you can see we are using a setText message to set text in our TextView. Apart from this, you can use many other methods to make TextView better.

  • setTextColor(): To set the color of the text in TextView.
  • setTextSize(): To set the size of the text in TextView
Spread the love
Scroll to Top