11

I know various questions have been asked about this before but I can't find an answer for my issue. I am working on an android application and I want an EditText field to expand as the user types into it. I want it to be a minimum size first and expand if the user types more than what the minimum allows. Here is my code so far:

<ScrollView
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:fillViewport="true" >

    <linearLayout
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        android:orientation="vertical"
        android:padding="5dp" >

        <linearLayout>
            ...content...
        </linearLayout>

        <linearLayout>
            ...content...
        </linearLayout>

        <EditText 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textMultiLine"
        android:lines="7"
        android:gravity="top" />

    </linearLayout>
</ScrollView>

The EditText field is inside the parent linearLayout, inside the parent ScrollView. It looks fine on screen but when I go past the 7th line in the edit text field I have to use a trackball to go back up through the text field. Scrolling the screen scrolls the entire screen. Can anyone give me some tips? I've tried implementing different suggestions to similar questions but none have given me the correct answer. Thanks

adohertyd
  • 2,689
  • 19
  • 58
  • 78
  • 1
    I'm not clear on your question, if you want it to keep growing remove: `android:lines="7"`. – Sam Oct 13 '12 at 23:46
  • @Sam Then how do I keep the editText box at a particular size? I don't want it to be a tiny box I want it to be a large box to start with – adohertyd Oct 13 '12 at 23:48
  • Have you tried `android:minHeight` and `android:minWidth`? – Sam Oct 13 '12 at 23:50
  • 8
    Actually use `android:minLines="7"` instead `android:lines`. This will keep it at least 7 lines and auto-resize when there is need. – tozka Oct 13 '12 at 23:54
  • @Sam now I feel like a complete idiot. That worked of course. Apologies for the stupid question so, I'm a complete beginner. Thanks for the help appreciate it – adohertyd Oct 13 '12 at 23:55
  • Glad I could help. No one is perfect: it seems tozka thought of something I didn't. – Sam Oct 13 '12 at 23:56

1 Answers1

12

As @tozka has stated in the comments, in a layout file, specify: android:minLines="" instead of android:lines="".

In Java: editText.setMinLines(int) instead of editText.setLines(int).

In C# (Mono for Android) editText.SetMinLines(int) instead of editText.SetLines(int).

From the Android docs:

public void setMinLines (int minlines)

Makes the TextView at least this many lines tall. Setting this value overrides any other (minimum) height setting.

Ryan Hoffman
  • 1,146
  • 1
  • 10
  • 22