0

I have an EditText that I want to use so people can input a short bio.

So I am trying to make it so it's fixed at, for example, a box that is 4 lines high. I don't want it to "scale" or "shift" with the input -- I'm trying to make it a fixed box of a fixed number of lines with word-wrap.

I did try adding lines="4" and maxLines="4" and inputType="textCapSentences|textMultiLine" but it doesn't quite seem to be right. When I set text, it appears in the middle of the EditText (and not the upper left), and it seems to let me hit enter a whole lot so I can have a word in the first row and then a character like 20 rows down.

Current XML:

    <EditText
        android:background="#00ff00" 
        android:id="@+id/editTextId"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:lines="4"
        android:maxLines="4"
        android:inputType="textCapSentences|textMultiLine"
        android:gravity="left|top"/>

I'm using a background of green so I can more easily see it for now. Right now this lets you type as much as you want, but I want to limit it to the space as given.

fadden
  • 51,356
  • 5
  • 116
  • 166
AJJ
  • 2,004
  • 4
  • 28
  • 42

3 Answers3

1

to fix the centered text issue add:

android:gravity="top|left"

To prevent the user from inputting more then 4 lines, you'll need to do it by code. Add a TextWatcher to the EditText, and check the number of lines after each text-change:

TextWatcher textWatcher = new TextWatcher() {
   onTextChanged(CharSequence s, int start, int before, int count) {
      // Check number of lines here
   }
};
editText.addTextChangedListener(textWatcher);
marmor
  • 27,641
  • 11
  • 107
  • 150
1

There is in no built code to achieve what you need. But here is a workaround -

private String enteredText;

edtText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {

                if (edtText.getLineCount() > 4) {
                    edtText.setText(enteredText);
                    edtText.setSelection(edtText.getText().length()); //This statement is to move the cursor at the end of the text otherwise it'll be moved to the start of the text.
                }
                else {
                    enteredText = edtText.getText().toString();
                }
            }
        });

Hope this helps !!

Shadab Ansari
  • 7,022
  • 2
  • 27
  • 45
0

Just add this attribute to your edittext

 android:inputType="textMultiLine"
Basil Battikhi
  • 2,638
  • 1
  • 18
  • 34
  • It's already part of my edittext. It doesn't actually cap the amount of input you can give it. It just caps the immediate viewing window. – AJJ Mar 30 '16 at 21:57