Android Word-Wrap EditText text

AndroidXmlAndroid EdittextWordWord Wrap

Android Problem Overview


I have been trying to get my EditText box to word wrap, but can't seem to do it.

I have dealt with much more complicated issues while developing Android applications, and this seems like it should be a straightforward process.

However, the issue remains, and I have a large text box that is only allowing me to enter text on one line, continuing straight across, scrolling horizontally as I enter text.

Here is the XML code for the EditText object from my layout file.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/myWidget48"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    >
 <ScrollView
    android:id="@+id/myScrollView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    >
    <LinearLayout
        android:id="@+id/widget37"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
    >

<EditText
        android:id="@+id/txtNotes"
        android:layout_width="300px"
        android:layout_height="120px"
        android:scrollbars="vertical"
        android:textSize="18sp"
        android:gravity="left"
        android:layout_marginTop="10dip"
        android:inputType="textCapSentences"
        >
    </EditText>
</LinearLayout>
</ScrollView>
</LinearLayout>

Android Solutions


Solution 1 - Android

Besides finding the source of the issue, I found the solution. If android:inputType is used, then textMultiLine must be used to enable multi-line support. Also, using inputType supersedes the code android:singleLine="false". If using inputType, then, to reiterate, textMultiLine must be used or the EditText object will only consist of one line, without word-wrapping.

Edit: Thank you Jacob Malliet for providing further good advice on this. He suggested to set the boolean scrollHorizontally property to false, 'android:scrollHorizontally="false"'.

Example XML code:

<EditText
    android:id ="@+id/edtInput"
    android:layout_width ="0dip" 
    android:layout_height ="wrap_content" 
    android:layout_weight ="1" 
    android:inputType="textCapSentences|textMultiLine"
    android:maxLines ="4" 
    android:maxLength ="2000" 
    android:hint ="@string/compose_hint"
    android:scrollHorizontally="false" />

Solution 2 - Android

Ok i figured it out, you must set android:scrollHorizontally="false" for your EditText in your xml. I'm pretty sure this should work.

Solution 3 - Android

Assume that you just want to have one line at first then expand it to 5 lines and you want to have maximum 10 lines.

<EditText
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:id="@+id/etMessageBox"
   android:layout_alignParentLeft="true"
   android:layout_centerVertical="true"
   android:autoLink="all"
   android:hint="@string/message_edit_text_hint"
   android:lines="5"
   android:minLines="1"
   android:gravity="top|left"
   android:maxLines="10"
   android:scrollbars="none"
   android:inputType="textMultiLine|textCapSentences"/>

By increasing android:lines you can define expand it how many lines.

Solution 4 - Android

you must add the following attribute to your edittext.

android:inputType="textMultiLine"

Also if you set the height or set the maxlines of edittext to a specific value, it will scroll when you typed much character.

Solution 5 - Android

I figured it out. The line,

android:inputType="textCapSentences"

was the issue. Adding this line to an EditText object will make the object be a single line EditText, and will not let words wrap, or allow a user to press 'Enter' to insert a line feed or carriage return in the EditText.

Solution 6 - Android

Enclosing the entire activity in scroll view and placing the following edit text with in linear layout worked like a charm for me.

The edit text would scroll itself vertically pressing enter, code as follows

<EditText
    android:id="@+id/editText1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:ems="10"
    android:hint="Enter somehting"
    android:inputType="textMultiLine"
    android:maxLength="2000"
    android:maxLines="6"
    android:scrollHorizontally="false"
    android:scrollbars="vertical" > 

Solution 7 - Android

Try this, changing gravity to top|left worked for me, along with input type textmultiline

<EditText
    android:layout_width="200dp"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:background="@color/color_red"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:gravity="top|left"
    android:inputType="textMultiLine|textCapSentences"
    android:lines="10"
    android:maxWidth="200dp"
    android:maxLength="200"
    android:maxLines="10"
    android:singleLine="false"
    android:textColor="@android:color/white"
    android:textSize="26sp" />

Solution 8 - Android

For those doing it programmatically, you might want to look at this question Android EditText Multiline does not work as it should to double check you are setting the input type correctly. That was my issue

Solution 9 - Android

Also set the parent layout width, (Layout_width property) . For example layout_width=250sp

otherwise , EditText or TextView dont wrap to next line till it reach the end of mobile border. So define the Layout_width

Solution 10 - Android

Try this. It works in my edittext.

 android:gravity="top|start"

Solution 11 - Android

Note: step 1 - create a custom class for EditText wordWrap.

Android hasn't this property. But you can replace all breaking characters with ReplacementTransformationMethod.

public class WordBreakTransformationMethod extends ReplacementTransformationMethod
{
    private static WordBreakTransformationMethod instance;
    private WordBreakTransformationMethod() {}
    
    public static WordBreakTransformationMethod getInstance()
    {
        if (instance == null)
        {
            instance = new WordBreakTransformationMethod();
        }
        return instance;
    }
    
    private static char[] dash = new char[] {'-', '\u2011'};
    private static char[] space = new char[] {' ', '\u00A0'};
    
    private static char[] original = new char[] {dash[0], space[0]};
    private static char[] replacement = new char[] {dash[1], space[1]};
    
    @Override
    protected char[] getOriginal()
    {
        return original;
    }
    
    @Override
    protected char[] getReplacement()
    {
        return replacement;
    }
}
    

Step 2 - In Activity , write below code,

In Android :

myEditText.setTransformationMethod(WordBreakTransformationMethod.getInstance());
        

In Kotlin:

myEditText.setTransformationMethod= WordBreakTransformationMethod.getInstance

In Xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <EditText
        android:id="@+id/myEditText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:inputType="textCapSentences|textMultiLine"
        android:scrollHorizontally="false" />
</LinearLayout>

Solution 12 - Android

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:id="@+id/table"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:stretchColumns="1"
    >  
     <TableRow
    	android:id="@+id/newRow">
    	<LinearLayout
    	     android:layout_width="fill_parent"
    		 android:orientation="vertical"
             android:layout_height="wrap_content"
             android:gravity="left"
             android:paddingBottom="10dip">
            <TextView  
    			android:layout_width="fill_parent" 
    			android:layout_height="wrap_content" 
    			android:textAppearance="?android:attr/textAppearanceLarge"
    			android:text="Some Text"
    		/>
        </LinearLayout>
        </TableRow>
        
        <View
        android:layout_height="2dip"
        android:background="#FF909090" />
        
    <TableRow>
   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingTop="10dip">

    <TextView  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"         
        android:text="Some Text"
        android:paddingBottom="5dip"
        />     
    <EditText 
        android:id="@+id/editbox"  
        android:layout_width="wrap_content" 
        android:layout_height="150px"
        android:gravity="top"   
        android:inputType="textFilter"
        android:scrollHorizontally="false"      
        />  
    
</RelativeLayout>
</TableRow>
<TableRow>
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <Button 
        android:id="@+id/btnone"  
        android:layout_width="3dip" 
        android:layout_height="wrap_content"
        android:layout_margin="2dip"
        android:layout_weight="1"
        android:text="Btn"
        />          
    <Button 
        android:id="@+id/btntwo"  
        android:layout_width="3dip" 
        android:layout_height="wrap_content"
        android:layout_margin="2dip"
        android:layout_weight="1"
        android:text="Btn"
        />    
 </LinearLayout>
 </TableRow>
 </TableLayout>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBryanView Question on Stackoverflow
Solution 1 - AndroidBryanView Answer on Stackoverflow
Solution 2 - AndroidJacob MallietView Answer on Stackoverflow
Solution 3 - AndroidHesamView Answer on Stackoverflow
Solution 4 - AndroidÖzer ÖzcanView Answer on Stackoverflow
Solution 5 - AndroidBryanView Answer on Stackoverflow
Solution 6 - AndroidSanView Answer on Stackoverflow
Solution 7 - AndroidEeshdaya UmarikarView Answer on Stackoverflow
Solution 8 - AndroidJerry ShaView Answer on Stackoverflow
Solution 9 - AndroidVictorView Answer on Stackoverflow
Solution 10 - AndroidMicView Answer on Stackoverflow
Solution 11 - AndroidShraddha PatelView Answer on Stackoverflow
Solution 12 - AndroidJoel ParkerView Answer on Stackoverflow