Vitaly Zinchenko
Vitaly Zinchenko

Reputation: 4911

Scrollable vertically and horizontally EditText that doesn't automatically break text to the next line

I need an activity that look like a simple text editor:
full-screen EditText with submit button below it.

Important:
1. EditText has to scroll both vertically and horizontally
2. Input text goes to the next line ONLY when the user clicks nextline button
3. EditText fills the whole space between the top of the screen and the button

My code:

<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"
        android:fillViewport="true">

            <HorizontalScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fillViewport="true">

                    <EditText
                        android:id="@+id/some_edittext"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:gravity="top|left"
                        />

            </HorizontalScrollView>

    </ScrollView>

    <Button
        android:id="@+id/submit_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Submit"/>

</LinearLayout>

Why bad:
EditText AUTOMATICALLY breaks text to the next line as it reaches the end of the row

It worked fine before I set fillViewport to true. But otherwise EditText doesn't fill entire screen

Thanks in advance

Upvotes: 0

Views: 1028

Answers (2)

tachyonflux
tachyonflux

Reputation: 20211

EditText actually has scrolling already built in, so you don't have to use a ScrollView. The only thing is that it doesn't support flings.

<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">

    <EditText
        android:id="@+id/some_edittext"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="top|left"
        />

    <Button
        android:id="@+id/submit_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Submit"/>

</LinearLayout>

To stop the automatic wrapping, you can activate horizontal scrolling:

EditText editText = (EditText) findViewById(R.id.some_edittext);
editText.setHorizontallyScrolling(true);
editText.setMovementMethod(new ScrollingMovementMethod());

Upvotes: 3

Saehun Sean Oh
Saehun Sean Oh

Reputation: 2281

I think you could set your EditText attribute android:maxLines="1" initially.

And when "new line" button is clicked, you can increase that value programmatically using setMaxLines(int) method.

EditText.setMaxLines(int) 

here's the doc -> link

Hope this helped.

Upvotes: 0

Related Questions