Create a blinking TextView

Blinking texts are very useful while trying to attract the user's attention towards something important in your app. Here's a very easy and simple example for creating a blinking TextView.

      boolean alpha = false;  
      @Override  
      protected void onCreate(Bundle savedInstanceState) {  
           super.onCreate(savedInstanceState);  
           setContentView(R.layout.activity_main);  
           final TextView blTV = (TextView) findViewById(R.id.bl_text);  
           final Handler h = new Handler();  
           h.postDelayed(new Runnable() {  
                @Override  
                public void run() {  
                     if (alpha) {  
                          blTV.animate()  
                                    .alpha(1)  
                                    .setDuration(400)  
                                    .setInterpolator(  
                                              new AccelerateDecelerateInterpolator())  
                                    .start();  
                          alpha = false;  
                     } else {  
                          blTV.animate()  
                                    .alpha(0)  
                                    .setDuration(400)  
                                    .setInterpolator(  
                                              new AccelerateDecelerateInterpolator())  
                                    .start();  
                          alpha = true;  
                     }  
                     h.postDelayed(this, 400);  
                }  
           }, 400);  
      }  


Comments