3

I'm trying to have a auto scroll TextView when my text is too long. I followed every tutorial or tips posted here on stackoverflow (yes I readed a bunch of topics similar to this but no one fixed it).

So I have my activity layout like that:

<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=".MainActivity" >

....

<TextView
    android:id="@+id/music_name"
    android:layout_width="fill_parent"
    android:layout_height="60dp"
    android:gravity="center"
    android:text="Music Name"
    android:textSize="24sp"
    android:layout_centerHorizontal="true"
    android:singleLine="true"
    android:scrollHorizontally="true"
    android:ellipsize="marquee"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:marqueeRepeatLimit="marquee_forever" />

....

</RelativeLayout>

And in OnCreate( ) I do that:

music_name.setSelected(true);
music_name.setMovementMethod(new ScrollingMovementMethod());

music_name is the TextView.

So even doing all that my TextView doesn't scroll. What could be wrong?

EDIT[NEW]: I noticed that it works only for the first time. The other times I change the text on the TextView it doesnt works anymore. What it could be doing that? I have thread (Timer) that wakes up every 50ms to update an SeekBar and other two TextViews, this could be blocking something?

2
  • Do you want it to scroll Horizontally or Vertically?
    – nKn
    Feb 11, 2014 at 20:01
  • it would be better to scroll horizontally, but if it can only scroll vertically it would be ok Feb 11, 2014 at 20:02

4 Answers 4

2

TextView sometimes comes tricky when handling scrolling. The main attributes you've to set into your layout are the following:

android:scrollHorizontally="false"
android:scrollbars="vertical"
android:gravity="bottom"

The gravity set to bottom will make your scrollbar descend to the bottom of your TextView everytime you append a line. So use that only if you want it to be that way.

The music_name.setMovementMethod(new ScrollingMovementMethod()); statement is also needed.

This way it should work.

6
  • I changed that on my XML and it doesn't work. It just kept a static text missing part of text. Feb 11, 2014 at 21:47
  • Is that TextView singleLine on purpose? If it has just one line, why do you need scrolling?
    – nKn
    Feb 11, 2014 at 21:50
  • It was there because the original idea was to create a horizontal scroll with only one line. But deleting it from XML doesn't solves the problem too. It just makes the text change orientation because of the gravity="bottom" but the text doesn't auto scroll. Feb 11, 2014 at 22:48
  • Just to clarify: By 'autoscroll' you mean that the textview automatically scrolls to the bottom of the content each time you call append on it? Are you adding enough text to make the scroll fire up? The way you've defined it, it just fires when it has to.
    – nKn
    Feb 11, 2014 at 22:58
  • I was thinking of something that the text on the TextView keeps scrolling on infinite loop to show the entire text. I dont know if this is what TextView do, but Im trying to find this effect. Feb 12, 2014 at 18:05
0

You need to use some animation, something like this scroll_textview_animation.xml in anim folder in the res:

<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="3000"
android:fromXDelta="200"
android:interpolator="@android:anim/linear_interpolator"
android:repeatCount="infinite"
android:repeatMode="restart"
android:toXDelta="-200" />

and onCreate or after setting the text:

music_name.startAnimation((Animation)AnimationUtils.loadAnimation(Context,R.anim.scroll_textview_animation));

or you can start the animation on click:

music_name.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            music_name.startAnimation((Animation)AnimationUtils.loadAnimation(Context,R.anim.scroll_textview_animation));
        }
    });
0
<TextView
    android:id="@+id/mytext"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="very long text to srollll abcdefghijklmnopqrstdaghlfj'ogjpsroshsrfjhrdjdjkdjdkjdk" 
    android:singleLine="true" 
    android:marqueeRepeatLimit="marquee_forever" 
    android:ellipsize="marquee"
    android:focusable="true" 
    android:focusableInTouchMode="true"/>
1
  • Full text is not visible , i mean it exceeds by " ... " Aug 13, 2016 at 10:25
0
<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="0dp">

<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

</ScrollView>

And define addTextChangedListener

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {

        //some code here

    ScrollView scrollView1 = (ScrollView) findViewById(R.id.scrollView1);
    TextView textView1 = (TextView) findViewById(R.id.textView1);
    textView1.addTextChangedListener(new TextWatcher() {

    @Override
    public void afterTextChanged(Editable arg0) {
        scrollView1.fullScroll(ScrollView.FOCUS_DOWN);
        // you can add a toast or whatever you want here
    }

    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,     int arg3) {
        //override stub
    }

    @Override
    public void onTextChanged(CharSequence arg0, int arg1, int arg2, int     arg3) {
        //override stub
    }

}) }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.