[android] Allow multi-line in EditText view in Android?

How to allow multi-line in Android's EditText view?

This question is related to android android-edittext multiline

The answer is


You may find it better to use:

<EditText 
...
android:inputType="textMultiLine"
/>

This is because android:singleLine is deprecated.


 <EditText
           android:id="@id/editText" //id of editText
           android:gravity="start"   // Where to start Typing
           android:inputType="textMultiLine" // multiline
           android:imeOptions="actionDone" // Keyboard done button
           android:minLines="5" // Min Line of editText
           android:hint="@string/Enter Data" // Hint in editText
           android:layout_width="match_parent" //width editText
           android:layout_height="wrap_content" //height editText
           /> 

if some one is using AppCompatEditText then you need to set inputType="textMultiLine" here is the code you can copy


   <androidx.appcompat.widget.AppCompatEditText
        android:padding="@dimen/_8sdp"
        android:id="@+id/etNote"
        android:minLines="6"
        android:singleLine="false"
        android:lines="8"
        android:scrollbars="vertical"
        android:background="@drawable/rounded_border"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/_25sdp"
        android:layout_marginTop="32dp"
        android:layout_marginEnd="@dimen/_25sdp"
        android:autofillHints=""

        android:gravity="start|top"
        android:inputType="textMultiLine"
         />

for background

   <?xml version="1.0" encoding="utf-8"?>
   <shape android:shape="rectangle" 
    xmlns:android="http://schemas.android.com/apk/res/android">
   <corners android:radius="@dimen/_5sdp"/>
   <stroke android:width="1dp" android:color="#C8C8C8"/>
   </shape>

Just add this android:inputType="textMultiLine" in XML file on relevant field.


Try this, add these lines to your edit text view, i'll add mine. make sure you understand it

android:overScrollMode="always"
android:scrollbarStyle="insideInset"
android:scrollbars="vertical"

<EditText
    android:inputType="textMultiLine"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/editText_newprom_description"
    android:padding="10dp"
    android:lines="5"
    android:overScrollMode="always"
    android:scrollbarStyle="insideInset"
    android:minLines="5"
    android:gravity="top|left"
    android:scrollbars="vertical"
    android:layout_marginBottom="20dp"/>

and on your java class make on click listner to this edit text as follows, i'll add mine, chane names according to yours.

EditText description;
description = (EditText)findViewById(R.id.editText_newprom_description);

description.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {

                view.getParent().requestDisallowInterceptTouchEvent(true);
                switch (motionEvent.getAction() & MotionEvent.ACTION_MASK){
                    case MotionEvent.ACTION_UP:
                        view.getParent().requestDisallowInterceptTouchEvent(false);
                        break;
                }

                return false;
            }

        });

this works fine for me


android:inputType="textMultiLine"

This code line work for me. Add this code you edittext in your xml file.


Another neat thing to do is to use android:minHeight along with android:layout_height="wrap_content". This way you can set the size of the edit when it's empty, and when the user inputs stuff, it stretches according to how much the user inputs, including multiple lines.


All of these are nice but will not work in case you have your edittext inside upper level scroll view :) Perhaps most common example is "Settings" view that has so many items that the they go beyond of visible area. In this case you put them all into scroll view to make settings scrollable. In case that you need multiline scrollable edit text in your settings, its scroll will not work.


<EditText
                android:hint="Address"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:inputType="textMultiLine|textNoSuggestions"
                android:id="@+id/edittxtAddress"
                android:layout_marginLeft="@dimen/textsize2"
                android:textColor="@android:color/white"
                android:scrollbars="vertical"
                android:minLines="2"
            android:textStyle="normal" />

and in Activity to remove "\n" is user press nextline

String editStr= edittxtAddress.getText().toString().trim();

 MyString= editStr.replaceAll("\n"," ");

This is how I applied the code snippet below and it's working fine. Hope, this would help somebody.

<EditText 
    android:id="@+id/EditText02"
    android:gravity="top|left" 
    android:inputType="textMultiLine"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:lines="5" 
    android:scrollHorizontally="false" 
/>

Cheers! ...Thanks.


To disable number of lines that was previously assigned in theme use xml attribute: android:lines="@null"


EditText has singleLine property. You can set in the XML or by calling setSingleLine(false); http://developer.android.com/reference/android/widget/TextView.html#setSingleLine%28%29


I learned this from http://www.pcsalt.com/android/edittext-with-single-line-line-wrapping-and-done-action-in-android/, though I don't like the website myself. If you want multiline BUT want to retain the enter button as a post button, set the listview's "horizontally scrolling" to false.

android:scrollHorizontally="false"

If it doesn't work in xml, doing it programmatically weirdly works.

listView.setHorizontallyScrolling(false);


This works for me, actually these 2 attributes are important: inputType and lines. Besides, you may need a scrollbar, the code below shows how to make one:

 <EditText
        android:id="@+id/addr_edittext"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="top|left"
        android:inputType="textEmailAddress|textMultiLine"
        android:lines="20"
        android:minLines="5"
        android:scrollHorizontally="false"
        android:scrollbars="vertical" />

Examples related to android

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How to implement a simple scenario the OO way My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error java doesn't run if structure inside of onclick listener Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable how to put image in a bundle and pass it to another activity FragmentActivity to Fragment A failure occurred while executing com.android.build.gradle.internal.tasks

Examples related to android-edittext

This view is not constrained vertically. At runtime it will jump to the left unless you add a vertical constraint Design Android EditText to show error message as described by google Change EditText hint color when using TextInputLayout How to change the floating label color of TextInputLayout The specified child already has a parent. You must call removeView() on the child's parent first (Android) Edittext change border color with shape.xml Changing EditText bottom line color with appcompat v7 Soft keyboard open and close listener in an activity in Android EditText underline below text property Custom designing EditText

Examples related to multiline

Way to create multiline comments in Bash? How to get multiline input from user R: "Unary operator error" from multiline ggplot2 command Can a JSON value contain a multiline string PHP multiline string with PHP Pythonic way to create a long multi-line string How do I create a multiline Python string with inline variables? How do you write multiline strings in Go? Multiline TextView in Android? What is the proper way to format a multi-line dict in Python?