Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide TextView after some time in Android

I want to hide TextView after some time interval say 3 seconds. I googled and found some code and I tried code as shown below but It is not working.

Please tell me what is the wrong with this ?

tvRQPoint.setText("+0");
tvRQPoint.postDelayed(new Runnable() {
    public void run() {
        tvRQPoint.setText("+0");
    }
}, 3000);

One more thing, how to remove timeout ? As I am using this on click event of ListView, If user clicks on one option and then clicks on second option, then as 3 seconds got over (after clicked on first option), It does not show second option for 3 seconds.

like image 339
Jeeten Parmar Avatar asked Mar 05 '14 10:03

Jeeten Parmar


5 Answers

try View INVISIBLE or GONE like:

tvRQPoint.postDelayed(new Runnable() {
public void run() {
    tvRQPoint.setVisibility(View.INVISIBLE);
}
}, 3000);

Set View visibility with view.setVisibility(View.INVISIBLE|View.VISIBLE|View.GONE);

like image 68
M D Avatar answered Nov 16 '22 11:11

M D


I simply animated the View from alpha 1 to 0, meaning visibility from 100% to 0%.

scrollUp = findViewById(R.id.lottieAnimationView);
    scrollUp.postDelayed(new Runnable() {
        @Override
        public void run() {
            AlphaAnimation alphaAnimation = new AlphaAnimation(1,0);
            alphaAnimation.setDuration(400);
            scrollUp.startAnimation(alphaAnimation);
            scrollUp.setVisibility(View.INVISIBLE);
        }
    },5000);

I am using the LottieAnimationView but same is applicable with any other view
just replace the id in R.id.{ID OF YOUR VIEW}
alphaAnimation.setDuration(400); This means the animation will take 4 seconds to complete i.e higher this number the slower the animation.
},5000); and the "5000" means 5 seconds delay.
For every second of delay you put it as 1000 ms or milliseconds of delay.

1 Seconds = 1000 ms

Hope it helps someone, all other answers are correct as well, this is just another way of doing it.

like image 26
Abhishek C. Gidde Avatar answered Sep 17 '22 14:09

Abhishek C. Gidde


How about hiding your text view with some animation?

  int delayMillis = 3000;
  Handler handler = new Handler();
  final View v = tvRQPoint; // your view
  handler.postDelayed(new Runnable() { 
    @Override
    public void run() {
       TranslateAnimation animate = new TranslateAnimation(0,-view.getWidth(),0,0);
       animate.setDuration(500);
       animate.setFillAfter(true);
       v.startAnimation(animate);
       v.setVisibility(View.GONE);

    }, delayMillis);
like image 7
Adnan Avatar answered Nov 16 '22 12:11

Adnan


What you are trying to do is ok but after three seconds you want to hide the textview so use setVisibility

tvRQPoint.setText("+0");
    tvRQPoint.postDelayed(new Runnable() {
        public void run() {
            tvRQPoint.setVisibility(View.INVISIBLE);
        }
    }, 3000);
like image 3
Prateek Avatar answered Nov 16 '22 10:11

Prateek


try this...

public class MainActivity extends Activity   {


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final TextView tv=(TextView)findViewById(R.id.tv);
    tv.setText("+0");
    tv.postDelayed(new Runnable() {
        public void run() {
            tv.setVisibility(View.INVISIBLE);
        }
    }, 3000);
}
 }
like image 2
rajshree Avatar answered Nov 16 '22 11:11

rajshree