JeffR
JeffR

Reputation: 313

TextView scrolling and animation in Android

I've successfully enabled scrolling for an Android TextView by either placing it within a ScrollView or, alternatively, using TextView's setMovementMethod (e.g., myTextView.setMovementMethod(new ScrollingMovementMethod()); ).

However, I would ideally like to see the TextView scroll in a fashion similar to the IPhone/IPod touch where the text overshoots and bounces back. In the emulator, a TextView simply scrolls to the beginning or end without any animation effect.

Is there an easy way to enable this scrolling behavior or some other approach that uses Android's animation capabilities and the OvershootInterpolator?

Upvotes: 2

Views: 5176

Answers (3)

Vinil Chandran
Vinil Chandran

Reputation: 1561

For full TextView Scrolling we can edit the code specified by #franta kocourek to the following

private void animateTextView() {
    int textWidth = getTextViewWidth(txtLastLocation);
    int displayWidth = Device.getDisplayWidth(getContext());

    if((displayWidth)<=textWidth) {
        textViewAnimation = new TranslateAnimation(
                textWidth, -textWidth,
                0, 0);
        textViewAnimation.setDuration(10000); // Set custom duration.
        textViewAnimation.setStartOffset(0);// Set custom offset.
        textViewAnimation.setRepeatMode(Animation.RESTART); // This will animate text back ater it reaches end.
        textViewAnimation.setRepeatCount(Animation.INFINITE); // Infinite animation.

        mTextView.startAnimation(textViewAnimation);
    }
}

Upvotes: 0

bee
bee

Reputation: 226

Sounds like the default ListView behavior might be what you want. Define the ListView in XML, and then set a custom adapter that feeds in the TextView.

<ListView android:id="@+id/ListView01" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content"/>

ListView docs: http://developer.android.com/reference/android/widget/ListView.html

You can see that there is an OnOverScrolled method. It should work by default.

Upvotes: 0

franta kocourek
franta kocourek

Reputation: 1337

I had a same problem as you. After hours of intensive research I made this:
In your xml layout, you have something like this:

<HorizontalScrollView 
        android:id="@+id/scroll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fadingEdgeLength="0dp"
        android:scrollbars="none" >

        <TextView
            android:id="@+id/textview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:maxLines="1" />

    </HorizontalScrollView>

And now the funniest part:

private TextView mTextView...
private Animation mAnimation...

private void animateTextView() {
    int textWidth = getTextViewWidth(mTextView);
    int displayWidth = getDisplayWidth(mContext);

    /* Start animation only when text is longer than dislay width. */
    if(displayWidth<textWidth) {
        mAnimation = new TranslateAnimation(
                0, displayWidth-textWidth,
                0, 0);
        mAnimation.setDuration(3000);    // Set custom duration.
        mAnimation.setStartOffset(500);    // Set custom offset.
        mAnimation.setRepeatMode(Animation.REVERSE);    // This will animate text back ater it reaches end.
        mAnimation.setRepeatCount(Animation.INFINITE);    // Infinite animation.

        mTextView.startAnimation(mAnimation);
    }
}

private int getDisplayWidth(Context context) {
    int displayWidth;

    WindowManager windowManager = (WindowManager)context.getSystemService(
            Context.WINDOW_SERVICE);
    Display display = windowManager.getDefaultDisplay();
    Point screenSize = new Point();

    if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB_MR2) {
        display.getSize(screenSize);
        displayWidth = screenSize.x;
    } else {
        displayWidth = display.getWidth();
    }

    return displayWidth;
}

private int getTextViewWidth(TextView textView) {
    textView.measure(0, 0);    // Need to set measure to (0, 0).
    return textView.getMeasuredWidth();
}

This animation is doing exactly what u want.

Upvotes: 2

Related Questions