How to completely prevent TextView scrolling?

I have a TextView with lots of text. This TextView has maxLines , so it only displays the first 8 rows. I also have a Read More button, so I process the TextView myself.

My problem is that sometimes the TextView scrolls a bit (only half the line at a time), although I have never specified a scrollbar. This problem is compounded by the fact that the TextView is inside the ListView, so when the user scrolls the main ListView, the TextView sometimes scrolls a bit, for example:

blahblah

How to prevent TextView scrolling?

+8
android android-layout android-textview
source share
5 answers

I have the same problem, My solution is to create a NoScrollTextView that extends a TextView like this

  public class NoScrollTextView extends TextView { public NoScrollTextView(Context context) { super(context); } public NoScrollTextView(Context context, AttributeSet attrs) { super(context, attrs); } public NoScrollTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @TargetApi(Build.VERSION_CODES.LOLLIPOP) public NoScrollTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } @Override public void scrollTo(int x, int y) { //do nothing } } 

set scrollTo do nothing

+12
source share

So, I did a little research, and I don’t think it is as simple as disabling scrolling, but there are a few things you can do / try.

The first is setEnabled (false), but this will disable links and change the color of the text.

The second one I suggest trying is using the scrollTo (int x, int y) method. Just scrollTo (0,0) after setting the TextView text, I assume that the large text is the only thing that causes scrolling, so it should be able to take care of this.

The third answer that I found that you can try is a little more complicated, not exactly your question, but it may work because you can find it here .

 public class LinkMovementMethodOverride implements View.OnTouchListener{ @Override public boolean onTouch(View v, MotionEvent event) { TextView widget = (TextView) v; Object text = widget.getText(); if (text instanceof Spanned) { Spanned buffer = (Spanned) text; int action = event.getAction(); if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN) { int x = (int) event.getX(); int y = (int) event.getY(); x -= widget.getTotalPaddingLeft(); y -= widget.getTotalPaddingTop(); x += widget.getScrollX(); y += widget.getScrollY(); Layout layout = widget.getLayout(); int line = layout.getLineForVertical(y); int off = layout.getOffsetForHorizontal(line, x); ClickableSpan[] link = buffer.getSpans(off, off, ClickableSpan.class); if (link.length != 0) { if (action == MotionEvent.ACTION_UP) { link[0].onClick(widget); } else if (action == MotionEvent.ACTION_DOWN) { // Selection only works on Spannable text. In our case setSelection doesn't work on spanned text //Selection.setSelection(buffer, buffer.getSpanStart(link[0]), buffer.getSpanEnd(link[0])); } return true; } } } return false; } } 

"After that, apply it to the target text box as a touch listener: -

textview.setOnTouchListener (new LinkMovementMethodOverride ()); "

+2
source share

Try these lines of code in xml

 android:isScrollContainer="false" android:ellipsize="end" 
0
source share

I need a function, for example, more in my project, and I used textview ellipsing from this publication. It works like a charm, and also provides an interface for checking if text has been ellipsized. That should do the trick.

0
source share

Just use setMinLines () to display all the text

0
source share

All Articles