4

Bueno como para que estemos en sintonia mi idea es crear un logger

ya logre hacer que tenga scroll vertical pero como hago que no se ajuste el texto en el TextView les comparto el código :

<?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);
            }
        });
    }
}

RESULTADO:

introducir la descripción de la imagen aquí

ya así funciona el scroll vertical pero no el horizontal


RESPUESTA MAS ACERTADA

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

pero: al ser wrap_content el tamaño del textview no es el mejor para facilitar el movimiento vertical

6
  • ¿Has probado a cambiar el android:scrollbars="vertical" por android:scrollbars="horizontal"? O añadir los dos. el 30 oct. 2018 a las 11:28
  • no funciona porque el texto se ajusta al contenedor mejor dicho tiene wrap
    – junior
    el 30 oct. 2018 a las 11:38
  • Te he puesto una posible solución que creo que funcionaría, estoy al tanto de si falla pero en principio sería como te he mandado el 30 oct. 2018 a las 12:53
  • Hola @junior estuve un poco ocupado para responder tu pregunta pero la única forma de habilitar el scroll vertical y horizontal en un TextView conocida es la que agrego en mi respuesta, agregué animaciones para que veas el funcionamiento, saludos!
    – Jorgesys
    el 1 nov. 2018 a las 18:34
  • Hola @junior no te sirvió mi respuesta? Lo que deseabas es usar ambos scroll.
    – Jorgesys
    el 6 nov. 2018 a las 16:42

2 respuestas 2

4

Al usar ScrollingMovementMethod() puedes tener la capacidad de habilitar el movimiento vertical,

Pero en realidad si deseas un ScrollView horizontal y al mismo tiempo el vertical, esto no sería natural puesto que el contenido de la vista siempre se intenta que sea mostrada dentro del área que comprende el ancho del dispositivo.


La única forma de agregar Scroll horizontal como vertical, es mediante el uso de la clase ScrollingMovementMethod() para habilitar el movimiento vertical:

 textView.setMovementMethod(new ScrollingMovementMethod());

y hacer uso de un HorizontalScrollView para habilitar el movimiento vertical.

Pero hay un punto importante a considerar, cuando agregas el texto a tu mediante setText(), este originalmente no tiene saltos de linea, todo el texto se encuentra en una linea, por lo tanto únicamente se mostraría el scrollview horizontal:

Jorgesys scrollview horizontal vertical

El secreto para que además del scrollview horizontal, el vertical sea activado, es agregar saltos de línea en tu texto definido en el EditText mediante \n, para que cuando este texto sea agregado a el TextView, el scrollview vertical también sea activado.

Ejemplo:

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

Jorgesys scrollview horizontal vertical

Este sería el 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>
2
+100

Creo que esta sería la opción mas sencilla, dejar que el ScrollView se adapte y que si requerimos de scrolls los implemente él sólo.

<?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>
2
  • no esta mala la idea la modifique un poco pero aun no me da el resultado esperado porque al ser wrap_content el tamaño del textview no es el mejor para facilitar el movimiento vertical
    – junior
    el 1 nov. 2018 a las 14:47
  • @junior Al realizarlo de esta forma en realidad solo se puede lograr el scroll del padre contenedor en este caso el HorizontalScrollView y no hay forma de sobreescribir otro scroll, eso depende del contenido.
    – Jorgesys
    el 1 nov. 2018 a las 18:45

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.