Thursday, February 21, 2019

Android change text color follow time

We have a TextView and want it change color follow time.
Copy this line of code below.
TexView text = (TextView) findViewById(R.id.textview);
int RED = 0xffFF0000;
int BLUE = 0xff0000FF;
ValueAnimator colorAnim = ObjectAnimator.ofInt(tv, "textColor", RED, BLUE);
colorAnim.setDuration(1000);
colorAnim.setEvaluator(new ArgbEvaluator());
colorAnim.setRepeatCount(ValueAnimator.INFINITE);
colorAnim.setRepeatMode(ValueAnimator.REVERSE);
colorAnim.start();
Press Ctrl+Shift +O to import libraries.
Text color phases will look like this.

To change color faster, reduce value in the line colorAnim.setDuration(1000);
To make it blinking when changing color, edit the line int BLUE to
int BLUE = 0x0000FF;

You can do this for TextView and Button.

No comments:

Post a Comment