TextView Multiline with horizontal and vertical scroll

4

Well, so that we are in tune, my idea is to create a logger

I've already managed to make it scroll vertically but as I do not fit the text in TextView I share the code:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <EditText
        android:id="@+id/edit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:hint="texto" />
    <Button
        android:id="@+id/send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="enviar"
        app:layout_constraintTop_toBottomOf="@id/edit"/>
    <TextView
        android:id="@+id/test1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:lines="4"
        android:scrollbars="vertical|horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

JAVA:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity);

        final TextView textView = (TextView) findViewById(R.id.test1);
        Button but=(Button) findViewById(R.id.send);
        final EditText edit=(EditText) findViewById(R.id.edit);

        textView.setMovementMethod(new ScrollingMovementMethod());
        but.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text=edit.getText().toString();
                textView.append("\n"+text);
            }
        });
    }
}

RESULT:

and that's how vertical scroll works but not horizontal

SUCCESSFUL ANSWER

<HorizontalScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toTopOf="parent">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/test1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:lines="4"
            android:scrollbars="vertical" />
    </LinearLayout>
</HorizontalScrollView>

but : being wrap_content the size of the textview is not the best to facilitate vertical movement

    
asked by junior 27.10.2018 в 21:07
source

2 answers

2

I think that this would be the simplest option, let the ScrollView adapt and that if we require scrolls, he only implements them.

<?xml version="1.0" encoding="utf-8"?>  
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="verticual" >
            <HorizontalScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:padding="10px" >
                <ScrollView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:padding="10px" >
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" >
                            <TextView
                                 android:id="@+id/test1"
                                 android:layout_width="wrap_content"
                                 android:layout_height="wrap_content"
                                 android:lines="4" />
                    </LinearLayout>
                </ScrollView>
            </HorizontalScrollView>
        </LinearLayout>
    
answered by 30.10.2018 / 13:52
source
4

By using ScrollingMovementMethod () you can have the ability to enable vertical movement, p>

But actually if you want a horizontal ScrollView and at the same time vertical, this would not be natural since the content of the view is always intended to be displayed within the area that comprises the width of the device.

The only way to add horizontal Scroll as vertical, is by using the class ScrollingMovementMethod () to enable vertical movement:

 textView.setMovementMethod(new ScrollingMovementMethod());

and make use of a HorizontalScrollView to enable vertical movement.

But there is an important point to consider, when you add the text to your using setText () , this originally does not have line breaks, all the text is in a line, therefore only the horizontal scrollview would be shown:

The secret so that in addition to the horizontal scrollview, the vertical is activated, it is adding line breaks in your text defined in the EditText by \n , so that when this text is added to the TextView , the vertical scrollview is also activated.

Example:

edit.setText("Lorem Ipsum is simply dummy text of the printing and typesetting industry.\n Lorem Ipsum has been, when an unknown printer took a galley of type and scrambled it to make a type specimen book.\n It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.\n It was popularised in the 1960s with the release of Letraset sheets containing Lorem software like Aldus PageMaker including versions of Lorem Ipsum.\n There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable.\n If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text.\n All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.\n");

This would be the layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/edit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="texto"
        android:textSize="10sp" />

    <Button
        android:id="@+id/send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="enviar"
        app:layout_constraintTop_toBottomOf="@id/edit" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scrollHorizontally="true"

            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <HorizontalScrollView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

            <TextView
                android:id="@+id/test1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"

                android:scrollbars="horizontal|vertical"
                android:lines="4"/>

            </HorizontalScrollView>

        </LinearLayout>


</android.support.constraint.ConstraintLayout>
    
answered by 30.10.2018 в 19:41