EagerToLearn
EagerToLearn

Reputation: 675

EditText's height does not expand to its parent's height

I placed an edit text within a scroll view with height = match_parent and expected its height to be equal to the scroll view but it's not. Its height behaves like wrap_content which means if there are no text in the EditText, I will have to point the cursor at the first "line" for the soft keyboard to popup, what I want is I can touch anywhere in the screen (the scroll view) and the soft keyboard pops up.

Below is the layout, thanks for your helps.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
        <EditText
            android:id="@+id/editText"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/transparent"
            android:gravity="top"
            android:padding="10dp"
            android:textSize="16sp"
            android:textColor="@android:color/black">
        </EditText>
    </ScrollView>

    <Button
        android:id="@+id/btnPaste"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/paste"
        android:gravity="center"
        android:onClick="pasteData"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="6dp"
        android:layout_marginBottom="5dp"
        android:background="@drawable/rounded_corner_button"
        android:textColor="@android:color/white"/>
</LinearLayout>

Layout capture :

enter image description here

Upvotes: 3

Views: 1105

Answers (3)

arthurealike
arthurealike

Reputation: 31

Implement this to your EditText in xml file:

android:scrollbars="vertical"

Implement this in onCreate():

 editText.setMovementMethod(new ScrollingMovementMethod());

Then, you won't need scrollview if you only use it for textview, it simply makes scrollable only your textview.

Upvotes: 0

Talha Bilal
Talha Bilal

Reputation: 187

Add this line in your ScrollView.

android:fillViewport="true"

Upvotes: 8

tamtom
tamtom

Reputation: 2594

You can control the edit text by changing minLines attribute

for example :

<EditText
            android:id="@+id/ed_comment"
            style="@style/EditText.NoBackground"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="top"
            android:hint="@string/write_comment_hint"
            android:minLines="40"
            android:textSize="16sp"/>

and instead of wrapping the EditText by scrollView add this attribute to editText

android:scrollbars="vertical"

result:

enter image description here

Upvotes: 3

Related Questions