0

In my app, I would like to have a quite long text scrolling below an image. I looked in StackOverflow examples and came to the following layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >

<ImageView
    android:id="@+id/mImageView"
    android:contentDescription="@string/description"
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight=".9" 
    android:background="#000000"
    android:layout_gravity="center"
    android:layout_alignParentRight="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_alignParentBottom="true" />

   <vds.playlistmanager.ScrollingTextView
            android:id="@+id/scrollingtextView1"
            android:layout_width="wrap_content"
            android:layout_height="0dip"
            android:layout_weight=".1" 
            android:background="#ff0000"
            android:paddingLeft="10dip"
            android:paddingRight="10dip"
            android:textSize="25dip"
            android:textStyle="bold"
            android:focusable="true"
            android:focusableInTouchMode="true"
         android:text="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"                
            android:singleLine="true"
            android:scrollHorizontally="true"
            android:marqueeRepeatLimit="marquee_forever"
            />  

And defined the class

public class ScrollingTextView extends TextView implements Runnable {
private static final float DEFAULT_SPEED = 15.0f;
private Scroller scroller;
private float speed = DEFAULT_SPEED;
private boolean continuousScrolling = true;

public ScrollingTextView(Context context) {
    super(context);
    setup(context);
}
public ScrollingTextView(Context context, AttributeSet attributes) {
    super(context, attributes);
    setup(context);
}
private void setup(Context context) {
    scroller = new Scroller(context, new LinearInterpolator());
    setScroller(scroller);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    super.onLayout(changed, left, top, right, bottom);
    if (scroller.isFinished()) {
        scroll();
    }
}

@Override
 public boolean isFocused() {
    return true;
 }  
 private void scroll() {
    int viewHeight = getHeight();
    int viewWidth = getWidth();
    int visibleHeight = viewHeight - getPaddingBottom() - getPaddingTop();
    int visibleWidth = viewWidth - getPaddingLeft() - getPaddingRight();
    int lineHeight = getLineHeight();
    //int offsetY = -1 * visibleHeight;
    int offsetY = 0;
    //int distanceY = visibleHeight + getLineCount() * lineHeight;
    int distanceY = 0;
    int distanceX = visibleWidth / 2;
    int duration = (int) (distanceX * speed);
    scroller.startScroll(0, offsetY, distanceX, distanceY, duration);
    if (continuousScrolling) {
        post(this);
    }
}
@Override
public void run() {
    if (scroller.isFinished()) {
        scroll();
    } else {
        post(this);
    }
}
public void setSpeed(float speed) {
    this.speed = speed;
}
public float getSpeed() {
    return speed;
}
public void setContinuousScrolling(boolean continuousScrolling) {
    this.continuousScrolling = continuousScrolling;
}
public boolean isContinuousScrolling() {
    return continuousScrolling;
}
}

The problem is in the text scrolls, but only text that fits inside TextView is shown, not the whole text, what I expected.

Sudar Nimalan
  • 3,962
  • 2
  • 20
  • 28
Fabio
  • 31
  • 5

1 Answers1

0

Why you are using custom Textview instead of using Textview inbuilt methods. You can done it very easily.just try below code.

In your xml

<TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:lines="1"
        android:ellipsize="marquee"
        android:fadingEdge="horizontal"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
        android:textColor="#ff4500"
        android:singleLine="true"
        android:text="Simple application that shows how to use marquee, with a long text  like Once upon a time there was a king in an old kingdom.He was very brave and kind." />

In your class just add below code

        TextView tv = (TextView) findViewById(R.id.textView1);
        tv.setSelected(true); 
Chandra Sharma
  • 1,339
  • 1
  • 12
  • 26
  • But I want to have a single line text that scrolls horizontally, with no scrollbars, like e.g. news scrolling on bottom of TV screen during television news. – Fabio Jan 09 '15 at 12:37
  • I have updated the code as you explained.hope it will help you. – Chandra Sharma Jan 09 '15 at 13:10
  • Thank you very much, Chandra, it works perfectly! I have just another question: is it possible to change the scrolling speed? – Fabio Jan 09 '15 at 15:52
  • If you want to set the marquee speed by xml then answer is NO.you cannot do it by xml.you have to write your custom class.check this reference. http://stackoverflow.com/a/8971920/3912847 Accept and up vote the answer so that it will help others. – Chandra Sharma Jan 11 '15 at 04:14