Reputation: 13
When setting the visibility of an ImageView in Java, only the result is displayed, not the intermediate states of the View. The result is just fine, but between start and end the progress bar isn't visible.
I already tried running the code on the UI-thread (as can be seen in the code). This doesn't change anything.
Reconnect is an ImageButton, which when it is clicked should hide the ImageView ConnectionStatus, show a ProgressBar named Connecting, sleep 1s and then again hide the Progressbard / show the ImageView.
Reconnect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
runOnUiThread(new Runnable() {
@Override
public void run() {
ConnectionStatus.setVisibility(View.INVISIBLE);
Connecting.setVisibility(View.VISIBLE);
try {
Thread.sleep(1000);
}
catch(InterruptedException e){}
Connecting.setVisibility(View.INVISIBLE);
ConnectionStatus.setImageResource(R.drawable.ic_baseline_check_24px);
ConnectionStatus.setVisibility(View.VISIBLE);
}
});
}
});
Upvotes: 1
Views: 195
Reputation: 152807
Thread.sleep()
blocks the current thread. Your UI thread cannot process any events such as redrawing views after their visibility has changed.
You can use Handler
postDelayed()
to post a Runnable
to execute later without blocking the thread from executing other stuff. All View
classes come with a Handler
out of the box, so you can just call postDelayed()
on one of your View
s.
Upvotes: 1