3

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>
Community
  • 1
  • 1
Roman Rdgz
  • 11,378
  • 36
  • 110
  • 192

4 Answers4

4

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.

Dimon
  • 740
  • 1
  • 10
  • 24
  • If I do so, all the components would be scrollable. I want just the EditText to be scrollable – Roman Rdgz Sep 23 '11 at 23:45
  • Oh, great. But I didn't see in your question that you want just to scroll EditText, moreover the word 'EditText' is absent in question description.. so the question is incorrect. Give more details. And how you imagine the scroll of EditText?? – Dimon Sep 24 '11 at 00:02
  • Usually ScrollView should be a parent widget-node in your XML file. So the inversion of ScrollView and EditText hierarchy cannot resolve a problem. – Dimon Sep 24 '11 at 00:06
1

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.

Sergei Pikalev
  • 506
  • 4
  • 5
0

Put the TextView inside ScrollView with height = MATCH_PARENT

try:

<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="0dp" 
        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="1"
       />

    <LinearLayout
    android:orientation="horizontal"
    android:layout_width="wrap_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1">

        <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>
Rodrigo
  • 5,104
  • 5
  • 37
  • 72
  • Doesn't work. Changes you made at the Buttons Linearlayout weight="1" makes them dissapear, it's unnecesary. About the TextView, with 0dp width of the scrollview no text is seen. If i erase this line all works as before, no scroll at all – Roman Rdgz Sep 23 '11 at 23:44
0

ScrollView is only allowed to have one sub widget.

You should put the linearlayout inside of the scroll view. and set the textview inside of the linear layout.

coder_For_Life22
  • 24,973
  • 20
  • 82
  • 117