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> 

4 Answers 4

5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

If I do so, all the components would be scrollable. I want just the EditText to be scrollable
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??
Usually ScrollView should be a parent widget-node in your XML file. So the inversion of ScrollView and EditText hierarchy cannot resolve a problem.
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.

Comments

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.

Comments

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> 

1 Comment

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.