I'm writing an Android app, and ran into a problem while I was coding the main part of the app.
I need to have an ImageView disappear for a second or two, and then reappear when tapped. The tapping part works, I use XML to make it clickable and then call a method on click. The problem is in the method that I call. I need it to:
- Disappear
- Add one to the playerscore
- Re-display the playerscore
- Reappear with a delay of a few seconds
My problem is that it does not noticeably disappear no matter what I do. I prefer to stay away from Thread.sleep() because I want the user to be able to tap other things while it is invisible (If I am misunderstanding Thread.sleep(), let me know. If it is possible to have every method be its own thread, please show me how. I am still pretty new at Java.) However, I did try to use Thread.sleep(), and it paused the line of code BEFORE the sleep statement, but did not visibly affect the amount of time between disappear and reappear. (that probably sounds confusing seeing the code will help explain it). I also tried to use a while statement to count up to 100 and then make it reappear when the counter had hit 100, but that did not work. I thought about using a timer, but I would need a very condensed example, as this timer would need to go into 24 different methods. Here is the code, it should help explain it a bit.
This is the first thing I tried, which I preferably do not want to use
public void tb1(View view){
b1.setVisibility(View.INVISIBLE); //Make the ImageView Invisible
playerscore ++;
score.setText("SCORE: " + playerscore); //The sleep affected this line for some reason
Thread.sleep(1000)
b1.setVisibility(View.VISIBLE) //Make the ImageView Visible
}
This is the second thing I tried.
public void tb1(View view) throws InterruptedException{
int i = 0;
b1.setVisibility(View.INVISIBLE);
playerscore ++;
score.setText("SCORE: " + playerscore);
while (i < 100){
i++;
}
if(i == 100){
b1.setVisibility(View.VISIBLE);
}
}
For this one, it was like I had done nothing. Nothing was delayed.
I really don't know what try. Am I using something wrong, or just plain using the wrong thing? Please be patient with me, I am still new at Java and am not very experienced.
Thank you in advance for any/all help!
UPDATE I added the code from the answers but nothing has changed. It disappears, but does not reappear. Do I have any syntax or any of the values wrong?
public void tb1(View view){
b1.setImageAlpha(0);
b1.animate().alpha(1f).setDuration(0).setStartDelay(3000).start();
playerscore++;
score.setText("SCORE: " + playerscore);
}