21

I am finding a tough time setting the textview with vertical scrolling text. In my dialog I have a textview with large text, since dialog is small window I want the textview text to scroll the vertically so that dialog looks good. Below is my xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#FFFFFF" >

    <ImageView
        android:id="@+id/fbcancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/fbcancel" />

    <EditText
        android:id="@+id/fbedittext"
        android:layout_width="230dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/imageView1"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="33dp"
        android:background="@drawable/roundcorners"
        android:ems="10"
        android:gravity="center"
        android:hint="@string/fbhint"
        android:lines="6"
        android:paddingRight="5dp"
        android:scrollHorizontally="true" />
    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="80dp"
        android:layout_height="250dp"
        android:layout_alignParentRight="true"
        android:layout_toRightOf="@+id/fbedittext" >

        <Spinner
            android:id="@+id/spinner"
            android:layout_width="50dp"
            android:layout_height="30dp"
            android:layout_alignLeft="@+id/fbshare"
            android:layout_alignRight="@+id/fbshare"
            android:layout_below="@+id/fbshare"
            android:layout_marginTop="16dp"
            android:drawSelectorOnTop="true"
            android:entries="@array/fblist" 

            android:visibility="gone"/>

        <ImageView
            android:id="@+id/fbpeople"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/fbshare"
            android:layout_alignRight="@+id/fbshare"
            android:layout_below="@+id/fbshare"
            android:layout_marginTop="16dp"
            android:background="@drawable/people"
            android:drawSelectorOnTop="true" />

        <ImageView
            android:id="@+id/fbshare"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="65dp"
            android:src="@drawable/newfb" />
    </RelativeLayout>



<ScrollView android:id="@+id/textAreaScroller" 
    android:layout_width="fill_parent" 
        android:layout_height="173px" 
        android:layout_x="0px" 
        android:layout_y="25px" 

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/fbedittext"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/fbedittext"
        android:layout_marginTop="22dp"
        android:lines="7"
        android:scrollbars="vertical"
        android:text="@string/fbtext" />
</ScrollView> 
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/fbcancel"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:paddingRight="2dp"
        android:src="@drawable/askabud" />

</RelativeLayout>

I tried placing the scroll view but the textview is going in wrong position. Any help is appreciated.

Bojan Kseneman
  • 14,663
  • 2
  • 50
  • 58
hari86
  • 629
  • 2
  • 16
  • 26

3 Answers3

78

1. Via XML

<ScrollView android:id="@+id/textAreaScroller" 
    android:layout_width="fill_parent" 
    android:layout_height="173px" 
    android:layout_x="0px" 
    android:layout_y="25px" 
    android:scrollbars="vertical">
<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/fbedittext"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/fbedittext"
    android:layout_marginTop="22dp"
    android:lines="7"        
    android:text="@string/fbtext" />
</ScrollView>

2.Via Code

ScrollView scroller = new ScrollView(this);
TextView tv=(TextView)findViewById(R.id.textView1);
scroller.addView(tv);

OR

Putting following properties inside your <TextView>

android:scrollbars = "vertical"

In the Code

tv.setMovementMethod(new ScrollingMovementMethod());
duggu
  • 35,841
  • 11
  • 112
  • 110
Vikalp Patel
  • 10,082
  • 6
  • 55
  • 92
2

Try setting maximum height in textview, it may work for you, like given below

<TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="100dip" // change dip accordingly
        android:layout_alignLeft="@+id/fbedittext"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/fbedittext"
        android:layout_marginTop="22dp"
        android:lines="7"
        android:scrollbars="vertical"
        android:text="@string/fbtext" />
AAnkit
  • 26,030
  • 10
  • 55
  • 68
-1

Inside your ScrollView give one more RelativeLayout and inside that RelativeLayout put your TextView.

PiyushMishra
  • 5,515
  • 6
  • 35
  • 57
  • You don't need a RelativeLayout in the middle if you will have only the textview inside it. Adding unnecessary layers may bring performance issues. Only the TextView inside the ScrollView should sufice. – IIRed-DeathII Dec 21 '16 at 17:11