Search code examples
androidandroid-edittextheightparent

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


Solution

  • Add this line in your ScrollView.

    android:fillViewport="true"