Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make EditText grow as it is filled

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

like image 907
adohertyd Avatar asked Oct 13 '12 23:10

adohertyd


1 Answers

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.

like image 112
Ryan Hoffman Avatar answered Sep 30 '22 04:09

Ryan Hoffman