How to make TextView Blink in android

Sometimes , you should be need the textview blink like a digital clock. here is the simple code for you should make blink textview in android. I have discussed here , mainly 3 methods,

Method 1:

private void blinkTextView(){
    final Handler handler = new Handler();
    new Thread(new Runnable() {
        @Override
        public void run() {
        int timeToBlink = 1000;    //in milissegunds
        try{Thread.sleep(timeToBlink);}catch (Exception e) {}
            handler.post(new Runnable() {
                @Override
                    public void run() {
                    TextView tvBlink = (TextView) findViewById(R.id.textViewBlink);
                    if(tvBlink .getVisibility() == View.VISIBLE){
                        tvBlink .setVisibility(View.INVISIBLE);
                    }else{
                        tvBlink .setVisibility(View.VISIBLE);
                    }
                    blinkTextView();
                }
                });
            }
        }).start();
    }

call the blinkTextView in onCreate, if you want blink textview whenever load application.

Method 2:


TextView tvBlink = (TextView) findViewById(R.id.textViewBlink);

Animation anim = new AlphaAnimation(0.0f, 1.0f);
anim.setDuration(50); //You can manage the blinking time with this parameter
anim.setStartOffset(20);
anim.setRepeatMode(Animation.REVERSE);
anim.setRepeatCount(Animation.INFINITE);
tvBlink .startAnimation(anim);


Method 3:

final ObjectAnimator textColorAnim;

and in onCreate, there should be

TextView blinkText = (TextView) findViewById(R.id.blinkTextView);
blinkTextView(blinkText );

Now definition of above function:

private void blinkTextView(TextView blinkTextView){
       textColorAnim = ObjectAnimator.ofInt(blinkTextView, "textColor", Color.BLACK, Color.TRANSPARENT);
                textColorAnim.setDuration(1000);
                textColorAnim.setEvaluator(new ArgbEvaluator());  
                textColorAnim.setRepeatCount(ValueAnimator.INFINITE);
                textColorAnim.setRepeatMode(ValueAnimator.REVERSE);
                textColorAnim.start();
}


2 comments: