Create TextView Programmatically in Android

Hello Readers! In this post, you will learn how to create TextView programmatically in android. You will go through different steps that explains to create Textview dynamically and adding it in java file.

You may also visit post to know more about TextView and it’s useful xml attributes to customise it.


Tutorialwing Dynamic TextView Tutorial Output Create TextView programmatically in android

Tutorialwing Dynamic TextView Tutorial Output

Video Output

First step is to create New Project. Follow the steps below to create new project. Please ignore the steps if you have already created project.

1. Creating New Project

Step Description
1. Open Android Studio.
2. Go to File => New => New Project. Write application name as DynamicTextView. Then, click next button.
3. Select minimum SDK you need. However, we have selected 17 as minimum SDK. Then, click next button
4. Then, select Empty Activity => click next => click finish.
5. If you have followed above process correctly, you will get a newly created project successfully. However, you can also visit post to create a new project to know steps in detail.

Now, we will modify the xml and java files to create TextView programmatically and add it in xml file. Please follow the steps below.

2. Modify Values Folder

Open res/values/strings.xml file and add below code into it.

	<string name="app_name">DynamicTextView</string>
	<string name="clicked_on_me">You clicked on me.</string>
	<string name="click_on_me">Click on me</string>

Other values folders have not been changed. So, we are not going to mention it here.

3. Modify Layout Folder

Open res/layout/activity_main.xml file. Add below code into it.

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


Note that LinearLayout has id rootLayout. In Java file, we will create TextView dynamically and add it into this LinearLayout having id rootLayout.

4.Create Android TextView Programmatically / Dynamically

Open src/main/java/com.tutorialwing.dynamictextview/ file. Then, add below code into it. You may also visit post to use Textview widget in xml file in android

package com.tutorialwing.dynamictextview;

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

	protected void onCreate(Bundle savedInstanceState) {

		LinearLayout linearLayout = findViewById(;

		// Create TextView programmatically.
		TextView textView = new TextView(this);
		textView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
		textView.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Toast.makeText(MainActivity.this, R.string.clicked_on_me, Toast.LENGTH_LONG).show();

		// Add TextView to LinearLayout
		if (linearLayout != null) {

Note that we are creating TextView Dynamically. Then, We have added it’s layoutParams, gravity and other attributes dynamically. Finally, added this TextView in LinearLayout having id rootLayout.

AndroidManifest.xml file

Since AndroidManifest file is very important for project. We are also going to mention it here. Code inside main/AndroidManifest.xml file is as below.

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.tutorialwing.dynamictextview"

		<activity android:name=".MainActivity">
				<action android:name="android.intent.action.MAIN"/>

				<category android:name="android.intent.category.LAUNCHER"/>


Finally, when you run the application, you will get output as shown above.

Leave a Reply