Tuesday, 17 May 2011

How to Blink Text View in Every 1 second in Android


package com.android.textviewblink;

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

public class TextViewBlink extends Activity {
Handler handler;
TextView txtvw;
boolean blinkOn = true;
        @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txtvw = (TextView) findViewById(R.id.txtview);
Thread myThread = null;
Runnable runnable = new CountDownRunner();
myThread = new Thread(runnable);
myThread.start();

}

public void update(){
runOnUiThread(new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
if (blinkOn) {
 txtvw.setVisibility(View.VISIBLE);
//txtvw.setText("Himanshu");
Log.v("log_tag", "true is gone");
} else {
txtvw.setVisibility(View.INVISIBLE);
//txtvw.setText("Mistri");
Log.v("log_tag", "false is gone");
}
blinkOn = !blinkOn;
}
});
}
class CountDownRunner implements Runnable {

// @Override
public void run() {
while (!Thread.currentThread().isInterrupted()) {
try {
                                      Thread.sleep(1000); //here you can change the time of blink
update();
Log.v("log_tag", "here the thread come for every 1 second ");

} catch (InterruptedException e) {
// Thread.currentThread().interrupt();
Log.e("log_tag", "Error s " + e.toString());
} catch (Exception e) {
Log.e("log_tag", "Error is " + e.toString());
}
}
}
}
}

1 comment: