Create An Android EditText Programmatically In Android

We have recently published 100+ articles on android tutorials with kotlin and java. If you need, you may visit Android Tutorial for beginners page. You can also check Kotlin Tutorial for beginners. Also, if you are interested in content writing, you can mail us at

Hello Readers! In this post, we will learn about how to create android editText programmatically. We will go through different steps that explains how to create EditText dynamically and use it in any android application.

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


Android Dynamic EditText Tutorial Create Android EditText Programmatically Create Android EditText Dynamically

Android Dynamic EditText Output

Video Output

At first, we will create a new project. After that, we will use EditText dynamically. 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 DynamicEditText. 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 create android EditText dynamically in java file and add it in LinearLayout. Similarly, you can add it in any ViewGroup. 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">DynamicEditText</string>
	<string name="enter_something">Enter Something...</string>
	<string name="show_text">Show Text</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 and add below code into it.

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




Notice that there is a LinearLayout, having id editTextContainer, that will act as container for editText created dynamically.

Now, we will create EditText programmatically and add it into this LinearLayout.

4. Create Android EditText Programmatically / Dynamically

Open app/src/main/java/com.tutorialwing.dynamicedittext/ file and add below code into it. You may also visit post to use EditText widget in xml file in android

package com.tutorialwing.dynamicedittext;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

	protected void onCreate(Bundle savedInstanceState) {

		LinearLayout linearLayout = findViewById(;

		// Create EditText
		final EditText editText = new EditText(this);
		editText.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
		editText.setPadding(20, 20, 20, 20);

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

		Button btnShow = findViewById(;
		if (btnShow != null) {
			btnShow.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					Toast.makeText(MainActivity.this, editText.getText(), Toast.LENGTH_LONG).show();

Here, we are creating EditText programmatically. Then, added this EditText into LinearLayout, having id editTextContainer. After that, we have added code to show text, entered by user in EditText, as Toast message.

AndroidManifest.xml file

Also, AndroidManifest.xml file would look like below –

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

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

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


Now, run the application. We will get output as shown above.

That’s the end of tutorial on Creating Android EditText programmatically / Dynamically.

Support Us

If you like Tutorialwing and would like to contribute, you can email an article on any educational topic at We would love to publish your article. See your article on Tutorialwing and help others with your knowledge. Follow Facebook, LinkedIn, Google+, Twitter, Youtube for latest updates.
We have recently published 100+ articles on android tutorials with kotlin and java. If you need, you may visit Android Tutorial for beginners page. You can also check Kotlin Tutorial for beginners