829

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);
hata
  • 8,429
  • 5
  • 32
  • 57
Muhammad Maqsoodur Rehman
  • 31,355
  • 34
  • 80
  • 123

30 Answers30

1842

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!

Amit Chintawar
  • 18,898
  • 1
  • 14
  • 16
  • 104
    But surely `maxLines` requires you to enter an arbitrary number; this isn't something that will work for every screen size and font size? I find it simpler to just wrap it with a `ScrollView`, meaning I don't have to add any further XML attributes or code (like setting the movement method). – Christopher Orr Dec 19 '10 at 22:57
  • 13
    @Christopher, ScrollView requires 'android:layout_height' too. – Regex Rookie Mar 04 '11 at 16:39
  • 12
    @Regex: Well yes. Either you let the `ScrollView` take up as much space as it needs (e.g. via the `android:fillViewport` attribute), or you set the specific size you want. Using `maxLines` should not be used to set sizes of UI elements, so that is no solution. – Christopher Orr Mar 05 '11 at 12:18
  • 2
    Hello. For me this is not a solution (as I thought it is). I have to create all the layout programatically here and when using just a TextView with ScrollMovementMethod(), it scrolls like a 3 px/scroll and very jerky. This TextView is in LineraLayout which is within a ScrollView. When I try to surround thhis TextView with another ScrollView - how can I set the height for it??? Remember - I'm not using any XML here...and ScrollView does not have a `.setHeight()` method. Any suggestion? – shadyyx May 14 '11 at 15:24
  • 64
    you dont need to define maxlines. just the rest. – Stevanicus Jun 08 '11 at 13:38
  • 2
    You don't even have to do the TextView.setMovementMethod(..) stuff ;), setting android:scrollbars is actually enough. – ubuntudroid Sep 21 '11 at 13:50
  • 6
    @ubuntudroid it is necessary to add this method. If your text is larger then the textview if will not scroll. – Sunny Apr 09 '12 at 17:07
  • @Sunny Well in my experience it does without any problems. Tried on stock Android 2.3.6. May depend on the Android version though. – ubuntudroid Apr 10 '12 at 09:21
  • This solution didn't Scroll scrolls automatically for me. i get the scroll with tap/click but that didnt scroll automatically. how to make it scroll automatically just like vertical mrquee. – Zeeshan Chaudhry Aug 27 '12 at 05:40
  • 2
    @Christopher: Setting `android:maxLines` isn't necessary. – caw Dec 01 '12 at 20:30
  • 1
    thanks thats really worked, and I have used it without mentioning "maxLines", but in my case I has to put yourTextView.setMovementMethod(new ScrollingMovementMethod()), without it, in my case not worked. – KumailR Apr 18 '13 at 08:31
  • 4
    It seems like to you have to surround the `TextView` with a `ScrollView` for it to scroll smoothly. – Matt Logan Oct 21 '13 at 02:00
  • 1
    I found out that [LinkMovementMethod](http://developer.android.com/reference/android/text/method/LinkMovementMethod.html) already "traverses links in the text buffer and scrolls if necessary." Thanks for the idea about maxLines and scrollBars. I spent hours figuring about this, and this saved me from wasting more time! – KarenAnne Oct 23 '13 at 03:19
  • I found a way to do this without XML. I've posted it here: http://stackoverflow.com/questions/19649334/how-to-show-scrollbars-on-a-textview-programmatically/19649335#19649335 – Peter Carpenter Oct 29 '13 at 04:42
  • 1
    I don't know what you all are talking about, I have to actually scroll manually to see the text. My idea of auto-scrolling is a TextView that scrolls back and forth without user intervention. Anybody any Idea please? – Behnam Jan 14 '14 at 08:34
  • 4
    This is actually the best and the easiest answer for this :) This is the equivalent programmatically: text.setMaxLines(4); text.setVerticalScrollBarEnabled(true); text.setHorizontalScrollBarEnabled(true); text.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); text.setMovementMethod(new ScrollingMovementMethod()); – Ollie Strevel Jan 20 '14 at 17:20
  • 1
    Where is "Smoothness" of scroll? – Deb Jan 08 '15 at 19:34
  • 1
    doesn't need to set max-lines – Murtaza Khursheed Hussain Jan 22 '15 at 07:08
  • 2
    scrolling is not smooth. how to achieve that? – Nishit Shah Jun 21 '15 at 13:57
  • 19
    To clarify what everyone is saying about "smooth" scrolling: If you use this method scrolling is actually "smooth" (in that you can scroll pixel by pixel), however it is *not* kinetic. As soon as you remove your finger it stops scrolling instantly - there is no flicking. This is pretty unacceptable for a modern UI so I'm going to edit the answer to make sure other people don't waste time on this "solution". – Timmmm Nov 09 '15 at 15:39
  • 4
    @Timmmm the answer has been accepted 804 times already. So the behavior is acceptable to most devs. And nowhere in the question and answer there is mention of kinetic scrolling. You should not edit an answer just because it does not fulfil your requirements. I think a comment that you made is good enough. – Amit Chintawar Nov 10 '15 at 08:52
  • 2
    Kinetic scrolling is an implicit requirement. And I edited the question because you have to scroll down through many ambiguously-worded comments to find out that it doesn't support kinetic scrolling. Shadyyx, Matt Logan, Pratik Butani, Deb, Nishit Shah and Simas have all noted that this doesn't work without issues - I'm sure they would have appreciated my edit. Someone Somewhere's answer is the best (and it has many upvotes) but for some reason is way way down the page). – Timmmm Nov 10 '15 at 14:25
  • 2
    Scroller of TextView is not that much fast as it is in ScrollView. I tried this solution and then shifted to the solution of @Someone Somewhere. – Usman Rana May 30 '16 at 10:30
  • According to https://stackoverflow.com/questions/1748977/making-textview-scrollable-on-android/37638516#37638516 maxlines is not needed. – reducing activity Mar 11 '18 at 16:04
  • Once I put my `TextView` in a `FrameView` I couldn't work out why it stopped working, it looks a dirty hack but `TextView.setMovementMethod(..)` is totally necessary. – John May 29 '18 at 21:22
  • whenever you need to use the ScrollView as parent, And you also use the scroll movement method with TextView. And When you portrait to landscape your device that time occur some issue. (like) entire page is scrollable but scroll movement method can't work. if you still need to use ScrollView as parent or scroll movement method then click on link https://stackoverflow.com/questions/1748977/making-textview-scrollable-on-android/51306133#51306133 – Prince Dholakiya Jul 12 '18 at 12:49
  • I just tried this on an API 29 emulator, and my `TextView` wouldn't scroll without wrapping it in a `ScrollView`. – Heath Borders Nov 19 '19 at 19:41
322

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());
        }
    });
}
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Someone Somewhere
  • 22,369
  • 11
  • 111
  • 155
  • 13
    There's no need to use `mTextView.getBottom()`, just use the method `mScrollView.fullScroll(View.FOCUS_DOWN)` as it is a part of the ScrollView API. See [here](http://developer.android.com/reference/android/widget/ScrollView.html#fullScroll%28int%29) and [here](http://developer.android.com/reference/android/view/View.html#FOCUS_DOWN) for more info. – ChuongPham Sep 11 '13 at 19:59
  • 8
    Android warns that either the `LinearLayout` or the `ScrollView` may be useless in this example (I removed the `LinearLayout` completely). It also warns that for the `TextView`, it should be `android:layout_height="wrap_content"` to match the `ScrollView`. These warnings may be due to the 3 years that have passed since this answer was posted. Regardless, this answer supports smooth scrolling and flicking...+1 – skia.heliou Dec 30 '14 at 23:43
  • Adding `` above the `ScrollView` in the xml file will take care of those warnings. – Louie Bertoncin Aug 12 '15 at 20:10
  • The "pro tip" doesn't work for me, in Android API level 9. The textview stays scrolled to the top after appending text. – LarsH Apr 21 '16 at 19:14
  • This approach is much better using the accepted answer approach will not produce a smooth user experience. – seyedrezafar Oct 12 '19 at 07:13
121

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);
    }
}
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
EddieB
  • 4,865
  • 3
  • 20
  • 17
49

Make your textview just adding this

TextView textview= (TextView) findViewById(R.id.your_textview_id);
textview.setMovementMethod(new ScrollingMovementMethod());
Keith Smiley
  • 54,143
  • 12
  • 89
  • 105
matasoy
  • 640
  • 1
  • 6
  • 7
44

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());
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Ahsan Raza
  • 451
  • 4
  • 5
28

You can either

  1. surround the TextView by a ScrollView; or
  2. set the Movement method to ScrollingMovementMethod.getInstance();.
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Samuh
  • 35,513
  • 26
  • 104
  • 116
23

The best way I found:

Replace the TextView with an EditText with these extra attributes:

android:background="@null"
android:editable="false"
android:cursorVisible="false"

There is no need for wrapping in a ScrollView.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Valentin Galea
  • 988
  • 6
  • 18
  • 2
    Easily the best and most appropriate answer, especially considering that EditText is a TextView subclass and the question is how to make the textview scrollable, not how to work around the problem. Would vote up twice if I could :) – Justin Buser Jul 08 '12 at 15:38
  • 4
    @JustinBuser Couldn't disagree more. :) It's purely serendipitous that this works, and there's no guarantee it will in future versions of Android. If you want scrolling, use the scrolling tools that Android provides. `ScrollView` is the correct way to go. – Madbreaks Oct 24 '12 at 00:53
  • 1
    @Madbreaks There is nothing "serendipitous" about it, EditText IS a TextView with a few added methods and overrides that is specifically tailored to accommodate varying length text. In particular EditText overrides the return value for the TextView getDefaultMovementMethod function. The default implementation returns NULL, EditText basically does the same thing all these other answers are suggesting. As far as using the tools that Android provides is concerned, the EditText class is far less likely to become deprecated than any of the other methods found in these answers. – Justin Buser Oct 27 '12 at 14:32
  • 4
    @JustinBuser My problem with that though is you're depending on the default behavior of EditText, *that* is what I meant may very well change down the road (not at all that EditText would be deprecated). You can argue that, but again, the idea is to use components and features that are appropriate for the task at hand. ScrollView was explicitly designed to encompass scrolling. I always try to, and always suggest others use the right tool for the job. There are almost always multiple approaches to any given programming problem. Anyway, thanks for the thoughtful response. – Madbreaks Oct 29 '12 at 17:57
16

Simple. This is how I did it:

  1. XML Side:

    <?xml version="1.0" encoding="utf-8"?>
    <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:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        tools:context="com.mbh.usbcom.MainActivity">
        <TextView
            android:id="@+id/tv_log"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="vertical"
            android:text="Log:" />
    </RelativeLayout>
    
  2. Java side:

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

Bonus:

To let the text view scroll down as the text fill it, you have to add:

    android:gravity="bottom"

to the TextView xml file. It will scroll down automatically as more text comes in.

Of course you need to add the text using the append function instead of set text:

    tv_log.append("\n" + text);

I used it for Log purpose.

I hope this helps ;)

MBH
  • 15,020
  • 18
  • 91
  • 140
14

This is "How to apply ScrollBar to your TextView", using only XML.

First, you need to take a Textview control in the main.xml file and write some text into it ... like this:

<TextView
    android:id="@+id/TEXT"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:text="@string/long_text"/>

Next, place the text view control in between the scrollview to display the scroll bar for this text:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent">

    <ScrollView
        android:id="@+id/ScrollView01"
        android:layout_height="150px"
        android:layout_width="fill_parent">

        <TextView
            android:id="@+id/TEXT"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="@string/long_text"/>

    </ScrollView>
</RelativeLayout>

That's it...

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
mOmO
  • 172
  • 2
  • 12
  • 7
    Why even write this? This is an almost identical, albeit less correct, version of an answer that was posted over 6 months before you wrote it. – Justin Buser Jul 08 '12 at 14:44
11

This will provide smooth scrolling text with a scroll bar.

ScrollView scroller = new ScrollView(this);
TextView tv = new TextView(this);
tv.setText(R.string.my_text);
scroller.addView(tv);
IT-Dan
  • 555
  • 5
  • 15
11

The "pro tip" above from Someone Somewhere (Making TextView scrollable on Android) works great, however, what if you're dynamically adding text to the ScrollView and would like to automatically scroll to the bottom after an append only when the user is at the bottom of the ScrollView? (Perhaps because if the user has scrolled up to read something you don't want to automatically reset to the bottom during an append, which would be annoying.)

Anyway, here it is:

if ((mTextStatus.getMeasuredHeight() - mScrollView.getScrollY()) <=
        (mScrollView.getHeight() + mTextStatus.getLineHeight())) {
    scrollToBottom();
}

The mTextStatus.getLineHeight() will make it so that you don't scrollToBottom() if the user is within one line from the end of the ScrollView.

Community
  • 1
  • 1
Mark Cramer
  • 2,029
  • 3
  • 26
  • 50
10

If you want text to be scrolled within the textview, then you can follow the following:

First you should have to subclass textview.

And then use that.

Following is an example of a subclassed textview.

public class AutoScrollableTextView extends TextView {

    public AutoScrollableTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setEllipsize(TruncateAt.MARQUEE);
        setMarqueeRepeatLimit(-1);
        setSingleLine();
        setHorizontallyScrolling(true);
    }

    public AutoScrollableTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setEllipsize(TruncateAt.MARQUEE);
        setMarqueeRepeatLimit(-1);
        setSingleLine();
        setHorizontallyScrolling(true);
    }

    public AutoScrollableTextView(Context context) {
        super(context);
        setEllipsize(TruncateAt.MARQUEE);
        setMarqueeRepeatLimit(-1);
        setSingleLine();
        setHorizontallyScrolling(true);
    }

    @Override
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
        if(focused)
            super.onFocusChanged(focused, direction, previouslyFocusedRect);
    }

    @Override
    public void onWindowFocusChanged(boolean focused) {
        if(focused)
            super.onWindowFocusChanged(focused);
    }

    @Override
    public boolean isFocused() {
        return true;
    }
}

Now, you have to use that in the XML in this way:

 <com.yourpackagename.AutoScrollableTextView
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:text="This is very very long text to be scrolled"
 />

That's it.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Dipendra
  • 1,539
  • 16
  • 32
9

Add the following in the textview in XML.

android:scrollbars="vertical"

And finally, add

textView.setMovementMethod(new ScrollingMovementMethod());

in the Java file.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
user3921740
  • 300
  • 4
  • 6
8

In kotlin for making the textview scrollable

myTextView.movementMethod= ScrollingMovementMethod()

and also add in xml this property

    android:scrollbars = "vertical"
Raluca Lucaci
  • 1,868
  • 2
  • 17
  • 35
6

I didn't find TextView scrolling to support the 'fling' gesture, where it continues scrolling after a flick up or down. I ended up implementing that myself because I didn't want to use a ScrollView for various reasons, and there didn't seem to be a MovementMethod that both allowed me to select text and click on links.

android.weasel
  • 2,985
  • 1
  • 23
  • 37
5

When you are done with scrollable, add this line to the view's last line when you enter anything in the view:

((ScrollView) findViewById(R.id.TableScroller)).fullScroll(View.FOCUS_DOWN);
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Rahul Baradia
  • 11,312
  • 16
  • 73
  • 118
  • dude,tablescroller is a view that was identified by the id attribute from the XML that was processed in .. u just try it.. it ll work fine.. its works fr me.. – Rahul Baradia Jul 09 '12 at 04:15
4

If you don't want to use the EditText solution then you might have better luck with:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.yourLayout);
    (TextView)findViewById(R.id.yourTextViewId).setMovementMethod(ArrowKeyMovementMethod.getInstance());
}
Justin Buser
  • 2,757
  • 22
  • 31
4

Add this to your XML layout:

android:ellipsize="marquee"
android:focusable="false"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:singleLine="true"
android:text="To Make An textView Scrollable Inside The TextView Using Marquee"

And in code you have to write the following lines:

textview.setSelected(true);
textView.setMovementMethod(new ScrollingMovementMethod());
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Ritesh
  • 105
  • 4
2

The code below creates an automatic horizontal scrolling textview:

While adding TextView to xml use

<TextView android:maxLines="1" 
          android:ellipsize="marquee"
          android:scrollHorizontally="true"/>

Set the following properties of TextView in onCreate()

tv.setSelected(true);
tv.setHorizontallyScrolling(true); 
whitepearl
  • 624
  • 2
  • 7
  • 16
2

I had this problem when I was using TextView inside the ScrollView. This solution has worked for me.

scrollView.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                description.getParent().requestDisallowInterceptTouchEvent(false);

                return false;
            }
        });

        description.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                description.getParent().requestDisallowInterceptTouchEvent(true);

                return false;
            }
        });
Rohit Mandiwal
  • 9,466
  • 4
  • 61
  • 75
2
yourtextView.setMovementMethod(new ScrollingMovementMethod());

you can scroll it now.

slfan
  • 8,209
  • 115
  • 61
  • 73
Exutic
  • 71
  • 4
  • 1
    This is what [the accepted answer](https://stackoverflow.com/a/3256305/1364007) already says. [From Review](https://stackoverflow.com/review/low-quality-posts/25026535). – Wai Ha Lee Jan 07 '20 at 07:01
1

Use it like this

<TextView  
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:maxLines = "AN_INTEGER"
    android:scrollbars = "vertical"
/>
Linh
  • 43,513
  • 18
  • 206
  • 227
Kamil Ibadov
  • 1,985
  • 1
  • 17
  • 40
1

whenever you need to use the ScrollView as parent, And you also use the scroll movement method with TextView.

And When you portrait to landscape your device that time occur some issue. (like) entire page is scrollable but scroll movement method can't work.

if you still need to use ScrollView as parent or scroll movement method then you also use below desc.

If you do not have any problems then you use EditText instead of TextView

see below :

<EditText
     android:id="@+id/description_text_question"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:background="@null"
     android:editable="false"
     android:cursorVisible="false"
     android:maxLines="6"/>

Here, the EditText behaves like TextView

And your issue will be resolved

Prince Dholakiya
  • 3,017
  • 23
  • 38
0

I struggled with this for over a week and finally figured out how to make this work!

My issue was that everything would scroll as a 'block'. The text itself was scrolling, but as a chunk rather than line by line. This obviously didn't work for me, because it would cut off lines at the bottom. All of the previous solutions did not work for me, so I crafted my own.

Here is the easiest solution by far:

Make a class file called: 'PerfectScrollableTextView' inside a package, then copy and paste this code in:

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.TextView;

public class PerfectScrollableTextView extends TextView {

    public PerfectScrollableTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setVerticalScrollBarEnabled(true);
        setHorizontallyScrolling(false);
    }

    public PerfectScrollableTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setVerticalScrollBarEnabled(true);
        setHorizontallyScrolling(false);
    }

    public PerfectScrollableTextView(Context context) {
        super(context);
        setVerticalScrollBarEnabled(true);
        setHorizontallyScrolling(false);
    }

    @Override
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
        if(focused)
            super.onFocusChanged(focused, direction, previouslyFocusedRect);
    }

    @Override
    public void onWindowFocusChanged(boolean focused) {
        if(focused)
            super.onWindowFocusChanged(focused);
    }

    @Override
    public boolean isFocused() {
        return true;
    }
}

Finally change your 'TextView' in XML:

From: <TextView

To: <com.your_app_goes_here.PerfectScrollableTextView

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Petro
  • 2,741
  • 3
  • 20
  • 46
  • 1
    100% Perfect answer. I faced the same issue as you mention in the description. apply many solutions but not work perfectly. but your solution working perfectly in every case. save my time man :) Thanks – Daxesh Vekariya Jan 02 '20 at 12:27
0

In my case.Constraint Layout.AS 2.3.

Code implementation:

YOUR_TEXTVIEW.setMovementMethod(new ScrollingMovementMethod());

XML:

android:scrollbars="vertical"
android:scrollIndicators="right|end"
rll
  • 4,932
  • 3
  • 26
  • 45
Sasha_NY
  • 1
  • 1
0

Put maxLines and scrollbars inside TextView in xml.

<TextView android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scrollbars="vertical"
    android:maxLines="5" // any number of max line here.
    />

Then in java code.

textView.setMovementMethod(new ScrollingMovementMethod());

Khemraj Sharma
  • 46,529
  • 18
  • 168
  • 182
0

XML - You can use android:scrollHorizontally Attribute

Whether the text is allowed to be wider than the view (and therefore can be scrolled horizontally).

May be a boolean value, such as "true" or "false".

Prigramacaly - setHorizontallyScrolling(boolean)

hash
  • 4,936
  • 7
  • 31
  • 50
0

Try this:

android:scrollbars = "vertical"
mohamad sheikhi
  • 304
  • 3
  • 14
0

For a vertically or horizontally scrollable TextView some of the other answers help, but I needed to be able to scroll both ways.

What finally worked is a ScrollView with a HorizontalScrollView inside of it, and a TextView inside the HSV. It's very smooth and can easily go side to side or top to bottom in one swipe. It also only allows scrolling in one direction at a time so there's none of the jumping side to side while scrolling up or down.

An EditText with editing and cursor disabled works, but it feels terrible. Each attempt to scroll moves the cursor and it requires many swipes to go top to bottom or side to side in even a ~100-line file.

Using setHorizontallyScrolling(true) can work, but there's no similar method to allow vertical scrolling, and it doesn't work inside of a ScrollView as far as I can tell (just learning though, could be wrong).

l3l_aze
  • 284
  • 3
  • 12
0

If you use Kotlin , in this way : XML

 <TextView
            android:id="@+id/tvMore"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:maxLines="3"
            android:scrollbars="vertical" />

Activity

tvMore.movementMethod = ScrollingMovementMethod()
Mori
  • 87
  • 5