Amt87
Amt87

Reputation: 5607

How can I animate an Android TextView without refreshing it, and fill it each time with new text from an array?

I am willing to create a news bar in android application and I used the animation to move TextView from left to right and fill it each round with a new text from array of strings. I faced a lot of problems and I used more than 5 ways but each one has a bad behaviour. the main problem is that when the animation ends it refreshes the textView and it appears like flashing which is not a user-friendly behaviour.

Here is a code snippet:

     TextView my_text;
     Animation slideRight;      
     Animation slideLeft;
     String [] text = {"TwoOneOneOneOneOneOneOneOneOneTwo","OneTwoTwoTwoTwoTwoTwoTwoTwoTwoTwoOne",
        "OneThreeThreeThreeThreeThreeThreeThreeOne","OneFourFourFourFourFourFourFourOne",
        "OneFiveFiveFiveFiveFiveFiveFiveOne","OneSixSixSixSixSixSixSixOne",
        "OneSevenSevenSevenSevenSevenSevenSeveOne","OneEightEightEightEightEightEightEightOne",
        "OneNineNineNineNineNineNineNineOne"};

     int arr_length;
     int count = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    arr_length = text.length;

    slideRight = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right);

    slideRight.setDuration(2000);
    slideRight.setRepeatCount(Animation.INFINITE);
    slideRight.setAnimationListener(slideRightListener);
    slideLeft = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);
    slideLeft.setDuration(2000);
    slideLeft.setAnimationListener(slideLeftListener);

    my_text = (TextView) findViewById(R.id.textView1);
    my_text.setVisibility(TextView.VISIBLE);
    my_text.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {       
            Toast.makeText(getApplicationContext(), my_text.getText(), Toast.LENGTH_SHORT).show();
        }
    });

    slideLeft.setAnimationListener(slideLeftListener);
    my_text.startAnimation(slideLeft);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

AnimationListener slideLeftListener = new AnimationListener() {

    @Override
    public void onAnimationStart(Animation animation) {
        my_text.setVisibility(TextView.VISIBLE);

    }

    @Override
    public void onAnimationRepeat(Animation animation) {
        my_text.setVisibility(TextView.VISIBLE);

    }

    @Override
    public void onAnimationEnd(Animation animation) {
        my_text.setVisibility(TextView.VISIBLE);
        my_text.startAnimation(slideRight);
    }
}; 

AnimationListener slideRightListener = new AnimationListener() {

    @Override
    public void onAnimationStart(Animation animation) {
        my_text.setVisibility(TextView.VISIBLE);
    }

    @Override
    public void onAnimationRepeat(Animation animation) {
        my_text.setVisibility(TextView.VISIBLE);
    }

    @Override
    public void onAnimationEnd(Animation animation) {
        my_text.setVisibility(TextView.VISIBLE);
        if(count<arr_length)
        {               
            my_text.setText(text[count]);
            count+=1;
            my_text.startAnimation(slideLeft);
        }
        else
        {
            count=0;
            my_text.setText(text[count]);
            my_text.startAnimation(slideLeft);
        }

    }
}; 
 }

Upvotes: 0

Views: 1070

Answers (4)

Amt87
Amt87

Reputation: 5607

I've got this link and I found it great to me and can be customized as my requirements. It uses HorizonalSlideshow.

Upvotes: 0

Navid
Navid

Reputation: 921

I think ViewFlipper is more suitable for you.

Upvotes: 1

Amt87
Amt87

Reputation: 5607

As I commented on @Bill above I realized that the refresh issue related to Android version.

I have tested the same code above on devices with Android Ice-Cream-Sandwich and it didn't "refresh".

If there is any solution on non-ICS I will accept it.

Upvotes: 0

Bill Mote
Bill Mote

Reputation: 12823

Try adding setFillAfter(true) as that might stop the "refresh" behavior at the end of the Animation.

Upvotes: 0

Related Questions