I'm trying to animate text from left to right in Andriod. Here is my code
<TextView
android:id="@+id/scrolling_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#777777"
android:singleLine="true"
android:textSize="48sp" >
</TextView>
animation = new TranslateAnimation(-100.0f, screenWidth+300,
0.0f, 0.0f); // new TranslateAnimation(xFrom,xTo, yFrom,yTo)
animation.setDuration(Constants.ANIMATION_SPEED); // animation duration
animation.setAnimationListener(this);
tv.startAnimation(animation); // start animation
It animates but it trims the text. I tried to put in in Horizontal ScrollView but it trims the text from the opposite side (right).
I need to make the animation in Java because After animation finishes I need to change the text and start another one.
Here's a quick 'n dirty way to loop an array of strings imitating a marquee. Since it's not 100% that your strings are longer than the screen width & you want to loop them 1 by 1, the only way I know of is animating each string in TextView.
This loops them 1 by 1. After each iteration it sets the next string, recalculates the width and creates a new animation accordingly.
package com.example.scrolling_text;
import android.app.Activity;
import android.graphics.Point;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;
/**
* Created by Simon on 14.7.4.
*/
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
TextView textView;
int screenWidth, currentMsg;
String[] msgArray = new String[] {"Message 1", "Hello i'm message 2", "This is message 3"};
Animation.AnimationListener myAnimationListener;
@Override
protected void onStart() {
super.onStart();
textView = (TextView) findViewById(R.id.scrolling_text);
// Get the screen width
Point size = new Point();
getWindowManager().getDefaultDisplay().getSize(size);
screenWidth = size.x;
// Set the first message
textView.setText(msgArray[0]);
// Measure the size of textView
textView.measure(0, 0);
// Get textView width
int textWidth = textView.getMeasuredWidth();
// Create the animation
Animation animation = new TranslateAnimation(-textWidth, screenWidth, 0, 0);
animation.setDuration(5000);
animation.setRepeatMode(Animation.RESTART);
animation.setRepeatCount(Animation.INFINITE);
// Create the animation listener
myAnimationListener = new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
// If out of messages loop from start
if (++currentMsg >= msgArray.length)
currentMsg = 0;
// Set the next msg
textView.setText(msgArray[currentMsg]);
// Measure the size of textView // this is important
textView.measure(0, 0);
// Get textView width
int textWidth = textView.getMeasuredWidth();
// Create the animation
animation = new TranslateAnimation(-textWidth, screenWidth, 0, 0);
animation.setDuration(5000);
animation.setRepeatMode(Animation.RESTART);
animation.setRepeatCount(Animation.INFINITE);
animation.setAnimationListener(myAnimationListener);
textView.setAnimation(animation);
}
};
animation.setAnimationListener(myAnimationListener);
textView.setAnimation(animation);
}
}
EDIT-NOTE:
If your strings can't fit in the screen then use a relative layout with a huge layout_width
.