Search code examples
androidandroid-animationtextview

How to make the textview blinking


Guys i have a textview which i need it to be blinking please help me with it.

<TextView 
       android:id="@+id/usage"
       android:layout_marginTop="220dip"
       android:layout_marginLeft="45dip"
       android:layout_marginRight="15dip"
       android:typeface="serif"            
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="Google "
       android:textColor="#030900"/>

I want the google text to be blinking


Solution

  • Edited

    It is a deprecated answer to Android before version 3.0 honeycomb, please uses SolArabehety's answer or look at this thread.

    The only reason I keep this answer is to historical reasons before android 3.0 Android animations had a lot of problems, this "bad" solution work at that time, nowadays it is unthinkable to use it, so just go for an animation solution, don't use this code.

    Original Answer

    package teste.blink;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.os.Handler;
    import android.view.View;
    import android.widget.TextView;
    
    public class TesteBlinkActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            blink();
    }
    
    private void blink(){
        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 txt = (TextView) findViewById(R.id.usage);
                        if(txt.getVisibility() == View.VISIBLE){
                            txt.setVisibility(View.INVISIBLE);
                        }else{
                            txt.setVisibility(View.VISIBLE);
                        }
                        blink();
                    }
                    });
                }
            }).start();
        }
    

    <TextView 
       android:id="@+id/usage"
       android:layout_marginTop="220dip"
       android:layout_marginLeft="45dip"
       android:layout_marginRight="15dip"
       android:typeface="serif"            
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="Google "
       android:textColor="#030900"/>