How To Create A TextView Programmatically In Android Studio

How To Create A TextView Programmatically In Android Studio

Create a TextView inside a LayoutView using javascript in Android Studio. If you are working on creating a Terminal Like View, which will add texts one by one according to task status or want to create a TextView inside a layout according to user actions, then this article is for you. Additionally, you can also use this for many more works/actions. In this article, we will show you how to do so.

Before we get started we need These:

  • Android Studio 
  • An existing App project with an activity

So, we are all set. Let’s Get Started!

Step 1 – Adding A Target Layout For Creating TextView:

Before we can create a TextView, we must specify where it should be created. And we are calling that Layout as Target Layout. So, first of all, choose the right place to create TextView. Now, add a Horizontal or Vertical Linear Layout (Up to you) with a specific ID like this:

<LinearLayout
android:id="@+id/targetlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

</LinearLayout>

Change ‘targetlayout‘ with your desired ID. You may add more attributions according to your needs to change the design of it (e.g. background color, margin, padding, etc)

Now let’s code to create a TextView inside our TargetLayout.

Step 2 – Creating A TextView Inside The Target Layout Programmatically:

Before we jump to create a TextView, we will need to import two classes for using TextView in our Activity.java file and adding it in our LinearLayout:

import android.widget.LinearLayout;
import android.widget.TextView;

Now we have to set the LinearLayout reference from the XML by ID using findByView() :

LinearLayout target = (LinearLayout) findViewById(R.id.targetlayout);

Change ‘target’ with your preferred name and ‘targetlayout’ with the ID of the LinearLayout where you want to create the TextView.

Now, to create a TextView programmatically we will use this line:

TextView ntext = new TextView(getApplicationContext());

Change ‘ntext‘ with your desired name.

Then we will need to create some attributions for that TextView. So, first of all, let’s create a LayoutParams for our new TextView:

LayoutParams nlparams = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, // Width Of The TextView
LayoutParams.WRAP_CONTENT); // Height Of The TextView

Change ‘nlparams‘ with your desired name. Note that the text which started with ‘//‘ and light color is a comment. You can change the ‘WRAP_CONTENT‘ with ‘MATCH_PARENT‘ or with a specific DP.

Then we have to apply the LayoutParams to the TextView:

ntext.setLayoutParams(nlparams);

Now, set the text that you want to show in the TextView using this line:

ntext.setText("Your Texts Inside This Double Quotation");

Now to add the TextView inside the TargetLayout use this line:

target.addView(ntext);

Change ‘target’ and ‘ntext’ with the name you have given before.

Additionally, Before adding the TextView inside the TargetLayout, I mean using the line ‘target.addView(ntext);’, you can also add these attributions as per you need:
To Set Color Of The TextView:

ntext.setTextColor(Color.parseColor("#000000")); //Change '#000000' with your desired color code

To Set Font Size Of The TextView:

ntext.setTextSize(20); //Change '20' with your desired size (in SP)

To Set Background Color Of The TextView:

ntext..setBackgroundColor(Color.parseColor("#0000FF")); //Change '#0000FF' with your desired color code

And many more…

So, Our Final Codes Will Look Like This:

package com.zealtyro.example;

import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

LinearLayout target = (LinearLayout) findViewById(R.id.targetlayout);
TextView ntext = new TextView(getApplicationContext());

LayoutParams nlparams = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, // Width Of The TextView
LayoutParams.WRAP_CONTENT); // Height Of The TextView

ntext.setLayoutParams(nlparams);
ntext.setText("Your Texts Inside This Double Quotation");

ntext.setTextColor(Color.parseColor("#000000"));
ntext.setTextSize(20);
//You can add more attributions here

target.addView(ntext);
}
}

Congratulations!

You have successfully created a TextView inside a LinearLayout programmatically.

Thanks For Allowing Us To Help You 🙂
If you are confused or want to know something, then let us know in the comment box, we will reach you as soon as possible. Don’t Forget To Subscribe our Newsletter, YouTube Channel, and Like Our Facebook Page To Keep Updated With Awesome Things. Follow us on Twitter to stay updated with the latest news & changes.

Did you like it?