Friday, January 14, 2011

Android - Blinking TextView


BLINKING TEXTVIEW

SOURCE CODE [Blink.java]

package com.YourPackageName;

import android.os.Handler;

public class Blink
{

                private int _interval;

                public int getInterval()
{
                                return _interval;
                }

                public void setInterval(int delay)
{
                                _interval = delay;
                }

                private Handler handler;
                private Runnable _tickHandler;
                private Runnable delegate;
                private boolean ticking;

                public boolean getIsTicking()
{
                                return ticking;
                }

                public Blink(int interval)
{
                                _interval = interval;
                                handler = new Handler();
                }

                public Blink(int interval, Runnable onTickHandler)
{
                                _interval = interval;
                                setOnTickHandler(onTickHandler);
                                handler = new Handler();
                }

                public void start(int interval, Runnable onTickHandler)
{
                                if (ticking)
                                                return;
                                _interval = interval;
                                setOnTickHandler(onTickHandler);
                                handler.postDelayed(delegate, _interval);
                                ticking = true;
                }

                public void start()
{
                                if (ticking)
                                                return;
                                handler.postDelayed(delegate, _interval);
                                ticking = true;
                }

                public void stop()
{
                                handler.removeCallbacks(delegate);
                                ticking = false;
                }

                public void setOnTickHandler(Runnable onTickHandler)
{
                                if (onTickHandler == null)
                                                return;

                                _tickHandler = onTickHandler;

                                delegate = new Runnable()
{
                                                public void run()
{
                                                                if (_tickHandler == null)
                                                                                return;
                                                                _tickHandler.run();
                                                                handler.postDelayed(delegate, _interval);
                                                }
                                };
                }

}

SOURCE CODE [TextViewBlinkExample.java]

package com. YourPackageName;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class TextViewBlinkExample extends Activity
{
                boolean blinkOn = false;

public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
       
TextView tv = (TextView) findViewById (R.id.textview);       

                                Blink tmrBlink = new Blink(500, new Runnable()
{

public void run()
{
if (blinkOn)
{
tv.setVisibility(View.VISIBLE);
}
else
{
tv.setVisibility(View.INVISIBLE);
}
 blinkOn = !blinkOn;
}
});
tmrBlink.start();
}
}