Making TextView scrollable on Android

659,172

Solution 1

You don't need to use a ScrollView actually.

Just set the

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!

Solution 2

This is how I did it purely in XML:

<?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: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>
</LinearLayout>

NOTES:

  1. android:fillViewport="true" combined with android:layout_weight="1.0" will make the textview take up all available space.

  2. When defining the Scrollview, DO NOT specify android:layout_height="fill_parent" otherwise the scrollview doesn't work! (this has caused me to waste an hour just now! FFS).

PRO TIP:

To programmatically scroll to the bottom after appending text, use this:

mTextStatus = (TextView) findViewById(R.id.TEXT_STATUS_ID);
mScrollView = (ScrollView) findViewById(R.id.SCROLLER_ID);

private void scrollToBottom()
{
    mScrollView.post(new Runnable()
    {
        public void run()
        {
            mScrollView.smoothScrollTo(0, mTextStatus.getBottom());
        }
    });
}

Solution 3

All that is really necessary is the setMovementMethod(). Here's an example using a LinearLayout.

File main.xml

<?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"
    >
<TextView
    android:id="@+id/tv1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:text="@string/hello"
    />
</LinearLayout>

File WordExtractTest.java

public class WordExtractTest extends Activity {

    TextView tv1;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        tv1 = (TextView)findViewById(R.id.tv1);

        loadDoc();
    }

    private void loadDoc() {

        String s = "";

        for(int x=0; x<=100; x++) {
            s += "Line: " + String.valueOf(x) + "\n";
        }

        tv1.setMovementMethod(new ScrollingMovementMethod());

        tv1.setText(s);
    }
}

Solution 4

Make your textview just adding this

TextView textview= (TextView) findViewById(R.id.your_textview_id);
textview.setMovementMethod(new ScrollingMovementMethod());

Solution 5

It is not necessary to put in

android:Maxlines="AN_INTEGER"`

You can do your work by simply adding:

android:scrollbars = "vertical"

And, put this code in your Java class:

textview.setMovementMethod(new ScrollingMovementMethod());
Share:
659,172
Muhammad Maqsoodur Rehman
Author by

Muhammad Maqsoodur Rehman

In brief, I have experience in both technical and business domains and like to seek a challenging professional role that utilizes both technical and communication skills. Understanding and being curious about business processes, domains and effectively collaborating with major stakeholders are my professional traits and I have always been passionate about being a keen problem solver. I simply believe that no software solution can be engineered without proper business analysis and project management and therefore I pursue to grow further in these valuable areas as well. As a technology consultant and tech evangelist, I am familiar with latest and trending technologies and business possesses and help businesses to succeed. Being an avid lifelong self-learner, I have a firm belief that hard skills and soft skills, both go hand in hand. Here are some of the core soft skills that are my fortes: Technology Consulting Business Analysis Software Usability Review Project Management Problem Solving Sales &amp; Negotiation Customer Support Note: I am also flexible to relocate myself geographically anywhere in the world. Contact Details: Email: [email protected] WhatsApp: +92 332 7999 729

Updated on July 20, 2021

Comments

  • Muhammad Maqsoodur Rehman
    Muhammad Maqsoodur Rehman almost 3 years

    I am displaying text in a TextView that appears to be too long to fit into one screen. I need to make my TextView scrollable. How can I do that?

    Here is the code:

    final TextView tv = new TextView(this);
    tv.setBackgroundResource(R.drawable.splash);
    tv.setTypeface(face);
    tv.setTextSize(18);
    tv.setTextColor(R.color.BROWN);
    tv.setGravity(Gravity.CENTER_VERTICAL| Gravity.CENTER_HORIZONTAL);
    tv.setOnTouchListener(new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent e) {
            Random r = new Random();
            int i = r.nextInt(101);
            if (e.getAction() == e.ACTION_DOWN) {
                tv.setText(tips[i]);
                tv.setBackgroundResource(R.drawable.inner);
            }
            return true;
        }
    });
    setContentView(tv);