3

I have created an EditText object dynamically but I haven't been able to create a multi-line EditText. I have tried this:

EditText et1 = new EditText(this);
et1.setHint("Enter Your Address");
et1.setSingleLine(false);
et1.setHorizontalScrollBarEnabled(false);
et1.setInputType(android.text.InputType.TYPE_TEXT_VARIATION_POSTAL_ADDRESS);
et1.setLines(7);

Thanks.

Matt
  • 22,721
  • 17
  • 71
  • 112
Mahi Mali
  • 111
  • 3
  • 15

4 Answers4

3

Include this in your code:

    et1.setMaxLines(maxlines);

Or you can set the specific height for the edit text.

AndroGeek
  • 1,280
  • 3
  • 13
  • 25
  • 1
    If you comment out this line of code it works fine: et1.setInputType(android.text.InputType.TYPE_TEXT_VARIATION_POSTAL_ADDRESS); – AndroGeek Jun 25 '12 at 12:00
3

If you want the text to wrap to the next line, add TYPE_CLASS_TEXT to the MULTI_LINE flag:

textArea.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_FLAG_MULTI_LINE);
grebulon
  • 7,697
  • 5
  • 42
  • 66
1

It is the line:

et1.setInputType(android.text.InputType.TYPE_TEXT_VARIATION_POSTAL_ADDRESS);

that is the problem. Take that out.

setMaxLines doesn't matter much unless you want to set a max number of lines. You should also avoid setting the height to something specific. WRAP_CONTENT works great.

Even changing it to:

et1.setInputType(android.text.InputType.TYPE_TEXT_FLAG_MULTI_LINE);

forces it to a single line edit, which seems odd.
This doesn't work either:

et1.setInputType(android.text.InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);

which is really freakin irritating. Seems like an android bug...

You also might want to set the vertical scroll on and gravity so it can scroll up and down and starts in the top left.

et1.setGravity(Gravity.TOP|Gravity.LEFT);
et1.setVerticalScrollBarEnabled(true);
Praveenkumar
  • 24,084
  • 23
  • 95
  • 173
stacybro
  • 115
  • 1
  • 5
0

What worked for me is:

et1.setSingleLine(false);
et1.setHorizontalScrollBarEnabled(false);
et1.setVerticalScrollBarEnabled(true);
et1.setMinLines(minLines);    
Beshoy Fayez
  • 300
  • 5
  • 10