Make textview scrollable in android programmatically

Create TextView with scrolling feature to insert multiple lines of text inside textview on single activity.

Making textview text is scrollable is very easy with the use of android:scrollbars=”vertical” attribute inside textview text widget this attribute will enable scrollbar display as vertically format and now we have to add setMovementMethod(new ScrollingMovementMethod()); on textview through MainActivity.java programming file. This method will enable scrolling on TextView. So here is the complete step by step tutorial for Make textview scrollable in android programmatically.

android-project-download-code-button

How to Make textview scrollable in android programmatically.

Code for MainActivity.java file.

 package com.textviewscrollable_android_examples.com;

import android.app.Activity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.widget.TextView;

public class MainActivity extends Activity {

 TextView scrollable;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 scrollable = (TextView)findViewById(R.id.textView1);
 
 //Enabling scrolling on TextView.
 scrollable.setMovementMethod(new ScrollingMovementMethod());
 }
}

Code for activity_main.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.textviewscrollable_android_examples.com.MainActivity" >

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentLeft="true"
 android:layout_alignParentTop="true"
 android:text="This is sample TextView Text with scrolling feature.This is sample TextView Text with scrolling feature.This is sample TextView Text with scrolling feature.This is sample TextView Text with scrolling feature.This is sample TextView Text with scrolling feature.This is sample TextView Text with scrolling feature.This is sample TextView Text with scrolling feature.This is sample TextView Text with scrolling feature.This is sample TextView Text with scrolling feature.This is sample TextView Text with scrolling feature.This is sample TextView Text with scrolling feature."
 android:textAppearance="?android:attr/textAppearanceLarge"
 android:scrollbars="vertical"
 />

</RelativeLayout>

Screenshot:

Make textview scrollable in android programmatically

Click here to download Make textview scrollable in android programmatically project.

Leave a Reply

Your email address will not be published. Required fields are marked *