30

I am trying to create a multiline EditText by code. This is what I use:

EditText txt = new EditText(this);    
lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 1.0f);
txt.setLayoutParams(lp);
txt.setSingleLine(false); 
txt.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);

But it is still in one single line.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Ton
  • 9,235
  • 15
  • 59
  • 103

8 Answers8

64

This should do it

txt.setSingleLine(false);
txt.setImeOptions(EditorInfo.IME_FLAG_NO_ENTER_ACTION);
Daniel López Lacalle
  • 6,715
  • 3
  • 25
  • 23
42

You may also use this:

txt.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_FLAG_MULTI_LINE);
waqaslam
  • 67,549
  • 16
  • 165
  • 178
23

Combining all above answers was the correct answer so here it is:

texInput.setSingleLine(false);
texInput.setImeOptions(EditorInfo.IME_FLAG_NO_ENTER_ACTION);
texInput.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE);
texInput.setLines(5);
texInput.setMaxLines(10);
texInput.setVerticalScrollBarEnabled(true);
texInput.setMovementMethod(ScrollingMovementMethod.getInstance());
texInput.setScrollBarStyle(View.SCROLLBARS_INSIDE_INSET);
ahmadalibaloch
  • 5,851
  • 2
  • 50
  • 59
  • Thank you, the Android API is horrible in this regard. The scrollbars are not visible for me but that doesn't matter since at least the text is finally able to be inputted in a multi-line fashion (previously there was no Enter button, or the Enter button inserted space instead of a new line - how retarded is that). – Martin Vysny Apr 19 '17 at 07:20
  • Thanks, but now the scrollbars are not visible, and the text is not selectable for some reason. – Martin Vysny Apr 19 '17 at 07:40
  • 1
    The only thing missing here is: textInput..setGravity(Gravity.TOP | Gravity.LEFT); – David_E Oct 12 '17 at 17:08
3

Include this in your code :

txt.setLines(maxlines);

maxlines will be the maximum number of lines you want to allow in your EditText.

Abhishek Sabbarwal
  • 3,758
  • 1
  • 26
  • 41
2

The combination that finally worked for me was:

answerView.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_LONG_MESSAGE);
answerView.setSingleLine(false);

At least partially I was using setInputType for configuring several different options so it made more sense than some of the other possibilities.

David Berry
  • 40,941
  • 12
  • 84
  • 95
1

On my phone, changing imeOptions and inputType does nothing; setting movementMethod and/or scrollBarStyle will screw the component so badly so that the user will be unable to select any text; isVerticalScrollBarEnabled and isHorizontalScrollBarEnabled does nothing; the best I could do is to use

txt.setSingleLine(false);

(txt.setImeOptions(EditorInfo.IME_FLAG_NO_ENTER_ACTION); seems to be unnecessary) but that won't display any scrollbars in the god-damn EditText. Ultimately I gave up and I'm inflating a layout snippet anytime I need a multi-line EditText. Here's the layout:

<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:inputType="textMultiLine"
android:lines="4"
android:minLines="4"
android:gravity="top|start"
android:maxLines="4"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:scrollbars="vertical"
/>

And here's Anko snippet:

fun ViewManager.multiLineEditText(@StyleRes theme: Int = 0, init: EditText.() -> Unit): EditText = ankoView(
    { ctx -> ctx.layoutInflater.inflate(R.layout.util_multilineedittext, this@multiLineEditText as ViewGroup, false) as EditText }, theme, init)

Development on Android sucks so hard.

Martin Vysny
  • 3,088
  • 28
  • 39
0

Try this

txt.setLines(number of lines);
A--C
  • 36,351
  • 10
  • 106
  • 92
Ariel Magbanua
  • 3,083
  • 7
  • 37
  • 48
0

In addition to the above suggestions - you might want to set the below additional parameters if you are not able to get this working

texInput.setHorizontallyScrolling(false);
texInput.setHorizontalScrollBarEnabled(false);

If you are changing the height of the edittext programmatically then you might want to request a layout too by calling requestLayout().

Raju
  • 82
  • 1
  • 5