3

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

EagerToLearn
  • 675
  • 7
  • 24

3 Answers3

6

Add this line in your ScrollView.

android:fillViewport="true"

Talha Bilal
  • 167
  • 7
3

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

tamtom
  • 2,474
  • 1
  • 20
  • 33
  • thanks for the comment, I know I can use the option `scrollBars` to make the EditText becomes scrollable, but this option doesn't not work as "smooth" as a scrollview. For example, if the scrollbar is long, with a ScrollView you can scroll to down/up only with one touch and the screen runs with it. But with this option only, you can’t、you need to keep swiping up/down till the top/bottom. – EagerToLearn Mar 27 '19 at 14:21
0

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.