Hi stackoverflow community,
I'm using Android API 14 on a droid 4.0.3 device.
In the Activity I've set a Button to display a TextView on the page while it is performing an action. After the action is performed, I want the TextView to disappear again.
button1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// make textview visible
textView1.setVisibility(View.VISIBLE);
// perform action
System.out.println("perform action");
// make textview disappear
textView1.setVisibility(View.GONE);
}
});
If I remove the part that makes the TextView disappear, the TextView appears at the top of the window as expected, but I want the TextView to appear for 1-2 seconds and then disappear.
At first I wondered if I needed to do more work than just performing a small action, so I tried adding a wait and printing out text, but none of that worked. The wait always called the exception, ending the activity, and when I printed out numbers 1-1000 the view was still permanently gone.
Is there a better way to make a TextView appear and disappear on an OnClick action?
Thanks for your help!
Those commands are executed back-to-back. So in a technical sense, it may be visible but only for a millisecond or two. You need to distinguish when to make the view visible and when to hide it...
You said that you would like the TextView to "blink" in a sense, so let's use the Handler that is a part of every View to call a Runnable. This Runnable will simply hide the TextView a few moments later:
1) Set up a class variable:
Runnable hide = new Runnable() {
@Override
public void run() {
textView1.setVisibility(View.GONE);
}
};
2) Call the runnable in a few moments:
button1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// make textview visible for 1 second (1000 milliseconds)
textView1.setVisibility(View.VISIBLE);
textView1.postDelayed(hide, 1000);
}
});
(A Handler and Runnable does not block the UI thread.)