35

How do I display Blinking Text in android.

Thank you all.

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
David Prun
  • 8,203
  • 16
  • 60
  • 86
  • Have you tried changing the color of the text every second for instance? because I don't think you can do that with the framework. or maybe you can use a webview with a blink tag... – Sephy Aug 10 '10 at 16:01
  • you could create a thread that toggles the visibility of the textView between View.VISIBLE and View.INVISIBLE – Ryan Conrad Aug 10 '10 at 16:06
  • Actually, I don't think you can do a WebView like Sephy suggested; Webkit doesn't render the tag (at least in Chrome). Because annoyingly blinking text at your users is a really bad way to get their attention, and they won't like you for it. – Yoni Samlan Aug 10 '10 at 16:46
  • It's disturbing how nobody downvoted this question. It doesn't show any research effort NOR any form of code to show what has been attempted. The answers are useful, and should be upvoted... but the question should be downvoted to oblivion. – STT LCU Sep 26 '13 at 07:12
  • @STTLCU , this question was asked 3 years ago! when there was no much of online google documentation for the feature. After I exhausted all options, I came to the last resort that is you experts. Please don't think otherwise. Thanks. – David Prun Oct 07 '13 at 05:40

7 Answers7

29

Actually there is an Easter egg blink tag for this in ICS! :) I don't actually recommend using it - was REALLY amused to find it in the source, though!

<blink xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="I'm blinking"
        />
</blink>
wvdz
  • 16,251
  • 4
  • 53
  • 90
Bill Phillips
  • 7,687
  • 1
  • 25
  • 13
  • Didn't work for me: `E/AndroidRuntime(19645): Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class blink` – Jay Sidri Apr 17 '12 at 06:31
  • It's not documented - I found it by source diving ICS. Looks like the change was introduced on 05-17-2011. Not sure which SDK number that maps too. – Bill Phillips May 09 '12 at 02:34
  • 1
    Oh, and if this weren't obvious - I'm pretty sure the "blink" tag is an easter egg. I'm not seriously suggesting it's the right solution! :) – Bill Phillips May 10 '12 at 19:47
  • FC on Gingerbread (I guess also below. Same message as Jahufar's. So test it on different versions if you do want to use this. – fangmobile Nov 18 '12 at 08:21
  • 2
    Wow! This one works well. Any way to use this programmatically? – WideFide Mar 20 '16 at 19:35
11

Create a view animation for it. You can do an alpha fade from 100% to 0% in 0 seconds and back again on a cycle. That way, Android handles it inteligently and you don't have to mess arround with threading and waste CPU.

More on animations here:
http://developer.android.com/reference/android/view/animation/package-summary.html

Tutorial:
http://developerlife.com/tutorials/?p=343

CodeFusionMobile
  • 14,812
  • 25
  • 102
  • 140
8

Using Threads in your code always wastes CPU time and decreases performance of the application. You should not use threads all the time. Use if wherever neccessary.

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(getApplicationContext(),
                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..

Gaurav Arora
  • 8,282
  • 21
  • 88
  • 143
  • @We'reAllMadHere I would suggest you to use Value Animator for this task, It is very easy to use. http://stackoverflow.com/questions/15582434/using-a-valueanimator-to-make-a-textview-blink-different-colors – Gaurav Arora Dec 18 '13 at 05:07
  • This is a very clear way to do it but it animates the whole TextView including its background color if not transparent. Is there a way to blink only the `text` inside `TextView` – Buddy Aug 09 '15 at 17:36
5

It can be done by adding a ViewFlipper that alternates two TextViews and Fadein and Fadeout animation can be applied when they switch.

Layout File:

<ViewFlipper android:id="@+id/flipper" android:layout_width="fill_parent" android:layout_height="wrap_content" android:flipInterval="1000" >

<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="TEXT THAT WILL BLINK"/>

<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="" />

</ViewFlipper>

Activity Code:

private ViewFlipper mFlipper;
mFlipper = ((ViewFlipper)findViewById(R.id.flipper));
mFlipper.startFlipping();
mFlipper.setInAnimation(AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.fade_in));
mFlipper.setOutAnimation(AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.fade_out));
Anenth
  • 99
  • 2
  • 8
2
 public static void blinkText(TextView textView) {
    Animation animation = new AlphaAnimation(1, 0);         // Change alpha from fully visible to invisible
    animation.setDuration(300);                             // duration - half a second
    animation.setInterpolator(new LinearInterpolator());    // do not alter animation rate
    animation.setRepeatCount(-1);                            // Repeat animation infinitely
    animation.setRepeatMode(Animation.REVERSE);             // Reverse animation at the end so the button will fade back in
    textView.startAnimation(animation);
}

I can't remember where I found this but it is by far the best I've seen

Palmeta
  • 61
  • 5
0
If you want to make text blink on canvas in bitmap image so you can use this code
we have to create two methods 
So first, let's declare a blink duration and a holder for our last update time: 
private static final  int BLINK_DURATION = 350; // 350 ms
private long lastUpdateTime = 0; private long blinkStart=0;

now create method

    public void render(Canvas canvas) {
    Paint paint = new Paint();
    paint.setTextSize(40);
    paint.setColor(Color.RED);
    canvas.drawBitmap(back, width / 2 - back.getWidth() / 2, height / 2
            - back.getHeight() / 2, null);
    if (blink)
        canvas.drawText(blinkText, width / 2, height / 2, paint);
}

Now create update method to blink

public void update() {
if (System.currentTimeMillis() - lastUpdateTime >= BLINK_DURATION
        && !blink) {
    blink = true;
    blinkStart = System.currentTimeMillis();
}
if (System.currentTimeMillis() - blinkStart >= 150 && blink) {
    blink = false;
    lastUpdateTime = System.currentTimeMillis();
 }

}

Now it work fine

  • I don't think this will do anything, unless you just call update() continuously (such as from a thread). – Sasquatch Jan 11 '21 at 14:54
-1
    private bool _blink; 
    private string _initialtext;
    private async void Blink()
    {
        _initialtext = _txtView.Text;
        _txtView.Text = Resources.GetString(Resource.String.Speak_now);
        while (VoiceRecognizerActive)
        {
            await Task.Delay(200);
            _blink = !_blink;
            Activity.RunOnUiThread(() =>
            {
                if (_blink) 
                    _txtView.Visibility = ViewStates.Invisible; 
                else 
                    _txtView.Visibility = ViewStates.Visible; 
            }); 
        }
        _txtView.Text = _initialtext;
        _txtView.Visibility = ViewStates.Visible;
    }

//this is for Xamarin android