9

I want to scroll text down and up vertically, incase the text to appear in the textview is longer than the space. However, the below methods I tested, do not work.

1 - tv1.setMovementMethod(new ScrollingMovementMethod()); Moreover, upon using this method, calls to onFling() method stop to work.

2 - using <ScrollView> in layout XML. also, when using this method, calls to onFling() method stop to work.

<ScrollView
android:id="@+id/SCROLLER_ID"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:fillViewport="true">

    <TextView
    android:id="@+id/TEXT_STATUS_ID"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1.0"/>

</ScrollView>

my TextView in layout XML is the following.

<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="2.26"
    android:background="@drawable/green"
    android:gravity="center"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge" />
Marcel Gosselin
  • 4,610
  • 2
  • 31
  • 54
Dan Jurgen
  • 927
  • 1
  • 8
  • 13

3 Answers3

16

You can't have a scrollable View, like TextView or ListView, inside a ScrollView. So use your simple TextView inside a normal layout and add android:scrollbars property to it.

<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:lines="3"
    android:scrollbars="vertical"
    android:scrollbarStyle="insideOverlay"
    android:fadeScrollbars="true"
    android:fadingEdge="vertical" />

In the Activity side you must write something like:

tv1.setMovementMethod(new ScrollingMovementMethod());

(Basically the same thing o described at your first point)

yugidroid
  • 6,640
  • 2
  • 30
  • 45
  • 1
    hi @yugidroid , I retried, it did not work. and once i apply the tv1.setMovementMethod(new ScrollingMovementMethod()); my onFling() methods which I use to swipe left/right stops to work. – Dan Jurgen Dec 02 '12 at 20:25
3

you can try it as:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:fillViewport="true" >

    <LinearLayout

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:orientation="vertical" >

    <TextView
        android:id="@+id/TEXT_STATUS_ID"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1.0"/>
</LinearLayout>
</ScrollView>
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • do I have to use the – Dan Jurgen Dec 02 '12 at 20:09
  • @DanJurgen, you do. Make a scrollable `TextView` inside a `ScrollView` won't work. – yugidroid Dec 02 '12 at 20:12
  • @yugidroid . I tried it, and it WORKS. however, the text is getting way too up or way too low. Is there any method that I can make it between two fixed lengths ? and whenever I get a new text there, the location (lets say very up) still applies to the new text.. I want it to be centered.. – Dan Jurgen Dec 02 '12 at 20:29
  • the onFling() methods, and the gestures work when you override the dispatchTouchEvent @Override public boolean dispatchTouchEvent(MotionEvent ev) { super.dispatchTouchEvent(ev); return productGestureDetector.onTouchEvent(ev); } – Dan Jurgen Dec 02 '12 at 20:44
1
<ScrollView
android:id="@+id/SCROLLER_ID"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

    <TextView
    android:id="@+id/TEXT_STATUS_ID"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"

</ScrollView>
Danil Onyanov
  • 210
  • 1
  • 4
  • 16
  • hi @Danil. Unfortunately, it does not work. Long texts are not scrollable down and they are cut off. the onFling() method also does not work. – Dan Jurgen Dec 02 '12 at 19:56
  • whats ur android version and what is your handset ? – Dan Jurgen Dec 02 '12 at 20:09
  • i have several different devices. It doesn't important: its basic functionality. http://stackoverflow.com/questions/6674341/how-to-use-scrollview-in-android – Danil Onyanov Dec 02 '12 at 20:20