Create An Android EditText Programmatically In Android

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.

Output

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.

<resources>
	<string name="app_name">DynamicEditText</string>
	<string name="enter_something">Enter Something...</string>
	<string name="show_text">Show Text</string>
</resources>

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"?>
<LinearLayout
	android:id="@+id/activity_main"
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="match_parent"
	android:layout_height="match_parent"
	android:gravity="center"
	android:orientation="vertical">

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

	<Button
		android:id="@+id/btnShow"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:text="@string/show_text">
	</Button>

</LinearLayout>

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/MainActivity.java 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.support.v7.app.AppCompatActivity;
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 {

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

		LinearLayout linearLayout = findViewById(R.id.editTextContainer);

		// Create EditText
		final EditText editText = new EditText(this);
		editText.setHint(R.string.enter_something);
		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) {
			linearLayout.addView(editText);
		}

		Button btnShow = findViewById(R.id.btnShow);
		if (btnShow != null) {
			btnShow.setOnClickListener(new View.OnClickListener() {
				@Override
				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"
		  xmlns:android="http://schemas.android.com/apk/res/android">

	<application
		android:allowBackup="true"
		android:icon="@mipmap/ic_launcher"
		android:label="@string/app_name"
		android:roundIcon="@mipmap/ic_launcher_round"
		android:supportsRtl="true"
		android:theme="@style/AppTheme">
		<activity android:name=".MainActivity">
			<intent-filter>
				<action android:name="android.intent.action.MAIN"/>

				<category android:name="android.intent.category.LAUNCHER"/>
			</intent-filter>
		</activity>
	</application>

</manifest>

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

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

Leave a Reply