Reputation: 173
I want to add ScrollView in TextView.
Like when TextView exceeds one particular height then ScrollView perform its task.
Thank you in advance.
Upvotes: 0
Views: 1009
Reputation: 134
You don't need to use a ScrollView actually.
Just set the
android:maxLines = "AN_INTEGER"
android:scrollbars = "vertical"
properties of your TextView in your layout's xml file.
Then use:
yourTextView.setMovementMethod(new ScrollingMovementMethod());
in your code.
Bingo, it scrolls!
Upvotes: 0
Reputation: 1001
I have problem like and do something like:
fragment_chat.xml
<ScrollView
android:id="@+id/messages_scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/send_panel"
android:fillViewport="true"
android:padding="5dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/messages"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:animateLayoutChanges="true"
android:gravity="center_horizontal|bottom"
android:orientation="vertical">
</LinearLayout>
</RelativeLayout>
</ScrollView>
And i declare textView with other files like:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/chat_msg_user"
android:padding="5dp">
<TextView
android:id="@+id/user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:textStyle="italic"/>
<TextView
android:id="@+id/msg_body"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/user_name"
android:textColor="@android:color/black"/>
and everything works fine :D
Upvotes: 0
Reputation: 1096
You can't add ScrollView inside a TextView. Instead you can apply scrolling feature to a TextView. To apply scrolling behaviour do the following.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hyderabad"
android:id="@+id/sample"
android:layout_gravity="right"
android:textSize="30sp"
android:maxLines="40"
android:scrollbars="vertical"/>
In Java code use the following
TextView tv = (TextView)findViewById(R.id.sample);
tv.setText("Long long ..................add more text here........text");
tv.setMovementMethod(new ScrollingMovementMethod());
Upvotes: 3
Reputation: 101
You have to put your textView in a LinearLayout or anykind of Layout and then you have to wrap it with your scrollView in your XML like this.
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/scrollView1">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</ScrollView>
ScrollView only takes Layouts (Linear, Grid...)
Hope it helps :)
Upvotes: 0