Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making TextView scrollable without knowing max lines

I want to make my TextView vertically scrollable. I have read "Making TextView Scrollable in Android" but since I don't know the size of the screen of the final device, I can't know which value should be set for maximum lines.

Is there any other way, so it gets the scrolling for any screen size?

My XML looks like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ScrollView
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:fillViewport="true"
        android:layout_weight="1.0">
        <TextView
            android:id="@+id/consola"
            android:layout_width="fill_parent" 
            android:layout_height="match_parent"/>
    </ScrollView>

    <EditText 
        android:id="@+id/comando"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.0"/>

    <LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.0">

        <Button
            android:text="Conectar"
            android:id="@+id/boton_conectar"
            android:layout_width="wrap_content"
            android:layout_weight="0.5"
            android:layout_height="wrap_content">
        </Button>
        <Button
            android:text="Enviar"
            android:id="@+id/boton_enviar"
            android:layout_width="wrap_content"
            android:layout_weight="0.5"
            android:layout_height="wrap_content">
        </Button>
    </LinearLayout>
</LinearLayout>
like image 972
Roman Rdgz Avatar asked Jun 02 '26 03:06

Roman Rdgz


2 Answers

Look, here the structure of my XML layout with scroll:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:scrollbars="vertical" 
        android:visibility="visible">

<LinearLayout 
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    .....
    //your sub-widgets
    .....
</LinearLayout>
</ScrollView>

As for me, works fine.

like image 165
Dimon Avatar answered Jun 05 '26 10:06

Dimon


If you do not know number of lines (moreover, it could vary if soft input method are shown or not), you should use RelativeLayout to place you widget like TextView. And then connect both (it's critical, not one but both) top and bottom edges of it to parent withandroid:layout_alignParentTop and android:layout_alignParentBottom attibutes, or neighbors with android:layout_above and android:layout_below attributes.

After both sides are connected to other widgets, android will recalculate size of TextView and scroller properly, even if you change orientation or show/hide soft keyboard.

like image 29
Sergei Pikalev Avatar answered Jun 05 '26 11:06

Sergei Pikalev