TextView in Android

In this lesson, we will learn about TextView in Android. Text view is one of the simplest widget 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 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.

TextView in Android Learn Android for free owlbuddy.com

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 Example: -
<?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 added TextView in the XML layout file. In this example we used several attibutes now i would explain each attibute that why we are using them.

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

After looking at this XML layout file example. Lets jump to out next example. In this example we will how we can set text into our Text View using Java code.

Java Example: -

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

We have TextView class in Java and which helps us to create TextView in our activity. In this example you can see we are using setText message to set text in our TextView. Apart from this you can use many other methods to make TextView more better like.

  • setTextColor(): To set color of text in TextView.
  • setTextSize(): To set size of text in TextView

check out the Output of code which we wrote in the XML layout file example

Text View in Android

Share this tutorial with your friends on your social Media..

TextView in Android TextView in Android TextView in Android TextView in Android

Now You Can Follow All These Tutorials on Your Android Mobile Just Download This App.