How to create multiline EditText in Android

Multiline edittext android. Allow multi-line in EditText view in Android?, By default all the EditText widgets in Android are multi-lined. Here is some sample code

activity_main.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    tools:context=".MainActivity"
    android:background="#f3f1eb"
    >
<EditText
        android:id="@+id/et"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:layout_marginBottom="10dp"
        android:hint="Multiline EditText by XML layout"
        android:fontFamily="sans-serif-condensed"
        android:background="#d3d7b6"
        android:minLines="2"
        android:maxLines="3"
        android:scrollbars="vertical"
        android:inputType="textMultiLine"
        />
    <EditText
        android:id="@+id/et2"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:fontFamily="sans-serif-condensed"
        android:hint="Multiline EditText programmatically"
        android:background="#d4d7a5"
        android:layout_below="@+id/et"
        />
</RelativeLayout>

MainActivity.java

package com.jigopost.multiedittext;

import android.os.Bundle;
import android.app.Activity;
import android.widget.EditText;
import android.widget.Scroller;


public class MainActivity extends Activity {

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

        // Get the widgets reference from XML layout
        final EditText et = (EditText) findViewById(R.id.et);
        final EditText et2 = (EditText) findViewById(R.id.et2);

        /*
            This will provide Scroll option for EditText,
            but it will not show the scroll bar on EditText
        */
        et2.setScroller(new Scroller(getApplicationContext()));
        et2.setVerticalScrollBarEnabled(true);

        /*
            setMinLines (int minlines)
                Makes the TextView at least this many lines tall.
                Setting this value overrides any other (minimum) height
                setting. A single line TextView will set this value to 1.
         */
        // Set the minimum lines to display on EditText
        et2.setMinLines(2);

        /*
            setMaxLines (int maxlines)
                Makes the TextView at most this many lines tall.
                Setting this value overrides any other (maximum) height setting.
         */
        // Set the maximum lines to display on EditText
        et2.setMaxLines(2);
   }
}

output

Leave a Reply