Create Multiline EditText in android programmatically

How to create an EditText that can get multiple lines input from user.

In this tutorial we are converting a single line edittext into multiline edittext with the use of setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE) function with its parameters in MainActivity.java programming file. So here is the complete step by step tutorial for Create Multiline EditText in android programmatically.

android-project-download-code-button

Create Multiline EditText in android programmatically.

Code for MainActivity.java file.

 package com.multilineedittextdynamically_android_examples.com;

import android.app.Activity;
import android.os.Bundle;
import android.text.InputType;
import android.widget.EditText;

public class MainActivity extends Activity {

 EditText editText;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 editText = (EditText)findViewById(R.id.editText1);
 
 
 editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE);
 }
}

Code for activity_main.xml layout file.

 <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.multilineedittextdynamically_android_examples.com.MainActivity" >

 <EditText
 android:id="@+id/editText1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:ems="10" >

 <requestFocus />
 </EditText>

</RelativeLayout>

Screenshot:

Create Multiline EditText in android programmatically

Click here to download Create Multiline EditText in android programmatically project with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *