0

I have a button that I want its text appear for 1 second then disappear for 1 second, loop for 6 seconds, here is what I tried:

for(int i = 0 ; i < 6 ; i++) {
    button.setTextSize(18);
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    button.setTextSize(0);
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
  • first is it appear that the button's text size became 36 instead of 18,

  • second is it is does not act as I expected, never show the text,

  • note: the text size from start is 0.

How can I achieve this?

Tomasz Dzieniak
  • 2,765
  • 3
  • 24
  • 42

3 Answers3

4

okey as you suggested you want to just blink your TextView, here what you can do.

create on folder under res called "anim", now create one xml file under that say

blink.xml and copy this code in it.

<?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:duration="1000" 
     android:repeatMode="reverse"
     android:repeatCount="infinite" /> 
</set>

Now in your Java class just apply this animation to your TextView

Animation anim = AnimationUtils.loadAnimation(getActivity(),
                    R.anim.blink);
yourtextview.startAnimation(anim);

Done.

V-rund Puro-hit
  • 5,518
  • 9
  • 31
  • 50
3

Instead of blocking thread using Thread.sleep(1000) you can use handler post delayed and after 1 second and make it visible and post another one to make it disappear .

new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        /* Make it visible here. */
        button.setVisibility(View.VISIBLE);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                /* Make it disappear here. */
                button.setVisibility(View.GONE);
            }
        }, 1000);      
    }
}, 1000);
Tomasz Dzieniak
  • 2,765
  • 3
  • 24
  • 42
Godfather
  • 833
  • 9
  • 14
3

This simple code can help you blink your textview:

final String text = yourTextView.getText().toString();
TimerTask timerTask = new TimerTask() {
    @Override
    public void run() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (yourTextView.getText().length() == 0) {
                    yourTextView.setText(text);
                } else {
                    yourTextView.setText("");
                }
            }
        });
    }
};
Timer timer = new Timer();
timer.scheduleAtFixedRate(timerTask,1000,1000);
Dhruv
  • 1,801
  • 1
  • 15
  • 27
Haven
  • 546
  • 5
  • 17