write an application to blink the text view on the screen



Here we have developed an application to blink the text using the concept of threading. The xml file is as follows:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
android:id="@+id/tvBlink"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="185dp"
android:text="eitworld.com" />

</RelativeLayout>

In this xml file we have used the relative layout and use the text view for blinking purpose. The java file is as follows:

package com.example.blinktextview;

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

public class MainActivity extends Activity {

TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

txt = (TextView) findViewById(R.id.tvBlink);
blink();
}

private void blink()
{
final Handler handler = new Handler();
new Thread(new Runnable() {
@Override
public void run() {
int timeToBlink = 1000;
try{Thread.sleep(timeToBlink);}catch (Exception e) {}
handler.post(new Runnable() {
@Override
public void run() {

if(txt.getVisibility() == View.VISIBLE){
txt.setVisibility(View.INVISIBLE);
}else{
txt.setVisibility(View.VISIBLE);
}
blink();
}
});
}
}).start();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

In this java file we have used the following packages:

android

In this file we have declared the following variables:
android

Here we have declared one variable txt of type TextView. Now we have to register control inside the onCretae() method as follows:
android

Here we have registered the control using the id defined in the xml file. After that we have called the user define method blink() as follows:
android

Inside this method, we have declared the Handler object and after that we have used the anonyms Thread object that provides the run() method. Now we will overload this method, inside this method we declared int type variable to take the time in mili-seconds. After that in try block we have used the static method sleep() of Thread class. Here we have used the post() method of Handler class and used another anonyms method run() and inside this method we are checking the visibility of the text view; if it is visible then it will be hidden and then if it is hidden then displayed. It will create a loop because we have used the recursion concept here to call the blink() method inside the blink() method start() method will start the thread().
The output is as follows:

android