Blinking Text in Android View

How to make the textview blinking

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"/>

How to make the text blinking in textview

Try this you can to use setText() to make blink effect in TextView

Try below code

<TextView
android:id="@+id/usage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimaryDark"
android:gravity="center"
android:textColor="#d92804" />

Java Code;

TextView textView;

textView = (TextView) findViewById(R.id.textView);

testBlink();

private void testBlink() {
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 (textView.getText().toString().equals("")) {
textView.setText("Nilesh");
} else {
textView.setText("");
}
testBlink();
}
});
}
}).start();
}

Android textview blinking after changing its visibility using animation

I tried your posted code and it seems to work just fine, except when animateLayoutChanges is turned on for the parent of the view you are trying to animate. If you have that enabled in the layout xml, turn it off and try again.

Text blinking in Android, with most concise Kotlin

For a bit of fun, you can define an extension function:

fun View.blink(
times: Int = Animation.INFINITE,
duration: Long = 50L,
offset: Long = 20L,
minAlpha: Float = 0.0f,
maxAlpha: Float = 1.0f,
repeatMode: Int = Animation.REVERSE
) {
startAnimation(AlphaAnimation(minAlpha, maxAlpha).also {
it.duration = duration
it.startOffset = offset
it.repeatMode = repeatMode
it.repeatCount = times
})
}

And use it like so (using the example in the question you linked):

myText.blink(3)  // Blink 3 times
yourText.blink() // Just keep blinking

Obviously you can change the parameters to suit your use case.

To stop the animation if needed, call clearAnimation() on the blinking view (yourText for the example above).

How to apply Blink animation on Checkbox text color

You can animate the textColor, it will not affect the box. Here is example to blink the red text

val colorAnimator = ObjectAnimator.ofInt(
myCheckBox,
"textColor",
Color.argb(255, 255, 0, 0), Color.argb(0, 255, 0, 0)
)
colorAnimator.setEvaluator(ArgbEvaluator())
colorAnimator.repeatCount = 3
colorAnimator.duration = 2000
colorAnimator.start()

Create a Blinking and Colour Changing TextView

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);
anim.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
// if you want to change color at start of animation
//textview.settextcolor("your color")
}

@Override
public void onAnimationEnd(Animation animation) {
// if you want to change color at end of animation
//textview.settextcolor("your color")
}

@Override
public void onAnimationRepeat(Animation animation) {

}
});
myText.startAnimation(anim);

Use setAnimationListener callback to change color of text in text view.

Blink Animation Count: how to blink a TextView only three times

Here is a xml that could be used to blink a view

blink.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<alpha
android:fromAlpha="1"
android:toAlpha="0"
android:repeatCount="3"
android:repeatMode="reverse"
android:interpolator="@android:interpolator/linear"
android:duration="150"
/>
</set>

You can call this animation like this:

MainScreenActivity.txtNext.startAnimation(AnimationUtils.loadAnimation(MainScreenActivity, R.anim.blink))


Related Topics



Leave a reply



Submit