Home » Android » How to make multiline edittext in android

How to make multiline edittext in android

Posted by: admin June 15, 2020 Leave a comment

Questions:

I try to create an edittext dynamically with LayoutParams.MATCHPARENT for its both width and height.
I have tried the following code, but it is still not working.

EditText editor = ....;
LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1);
editor.setLayoutParams(params);
editor.setGravity(Gravity.LEFT | Gravity.TOP);
editor.setBackgroundColor(Color.WHITE);
editor.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI);
editor.setTextColor(Color.BLACK);
//editor.setSingleLine(false);
editor.setFocusableInTouchMode(true);
editor.setInputType(EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE | EditorInfo.TYPE_TEXT_FLAG_IME_MULTI_LINE);
editor.setMaxLines(Integer.MAX_VALUE);
editor.setHorizontallyScrolling(false);
editor.setTransformationMethod(null);

The code above let me to enter several lines, and an ‘ENTER’ button is displayed in the keyboard, but as I type on the edittext, the edit text scrolls horizontally if the width is not enough to display the text.
Can anybody please help me how to make the edittext multiline?

How to&Answers:

Try this following code

editor.setSingleLine(false);
editor.setHorizontalScrollBarEnabled(false);

Answer:

Try this code

    txt.setSingleLine(false);
    txt.setLines(14);
    txt.setMinLines(13);
    txt.setMaxLines(15);
    txt.setGravity(Gravity.LEFT | Gravity.TOP);

Answer:

Try

android:scrollHorizontally="false"

for the edittext

Answer:

Add below line into your code, it may help you.

editor.setSingleLine(false);