How to make the textview blinking
You can use this:
TextView myText = (TextView) findViewById(R.id.myText );
Animation anim = new AlphaAnimation(0.0f, 1.0f);
anim.setDuration(50); //You can manage the blinking time with this parameter
anim.setStartOffset(20);
anim.setRepeatMode(Animation.REVERSE);
anim.setRepeatCount(Animation.INFINITE);
myText.startAnimation(anim);
It's the same answer I gave in this post Blinking Text in android view
Hope this helps!
Use XML Animations for this purpose :
R.anim.blink
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:fromAlpha="0.0"
android:toAlpha="1.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:duration="600"
android:repeatMode="reverse"
android:repeatCount="infinite"/>
</set>
Blink Activity: use it like this :-
public class BlinkActivity extends Activity implements AnimationListener {
TextView txtMessage;
Button btnStart;
// Animation
Animation animBlink;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_blink);
txtMessage = (TextView) findViewById(R.id.txtMessage);
btnStart = (Button) findViewById(R.id.btnStart);
// load the animation
animBlink = AnimationUtils.loadAnimation(this,
R.anim.blink);
// set animation listener
animBlink.setAnimationListener(this);
// button click event
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txtMessage.setVisibility(View.VISIBLE);
// start the animation
txtMessage.startAnimation(animBlink);
}
});
}
@Override
public void onAnimationEnd(Animation animation) {
// Take any action after completing the animation
// check for blink animation
if (animation == animBlink) {
}
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationStart(Animation animation) {
}
}
Let me know if you have any queries..
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"/>
Don't need set for this. Just alpha:
anim/flash_leave_now.xml
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="900"
android:fromAlpha="1.0"
android:repeatCount="infinite"
android:repeatMode="reverse"
android:toAlpha="0.2"/>
And in code:
mTextView.setAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.flash_leave_now));
Create an AlphaAnimation and apply it to the textview in the activity where you setup the textview. The blinking would be accomplished by repeating an animation from 1.0 alpha to 0.0 alpha to 1.0 alpha.
Edit: Google provideth.