Erik
Erik

Reputation: 67

Android - disable button while thread is running

SO, i basically want to disable a button until a thread that handles some downloads is finished, how do i approach this in the best way? at first I thought of a quick fix:

while(thread.alive())
{
button.disable();
}

I don't feel like this is a good way of solving this problem, Also i cant use the "AsyncTask" solution since the operation is going to handle pretty large files over slow connections. Is the proper approach to use a listener?

Edit: My question is not about what function to use in order to disable the button, but rather about how I know when the thread is finished and how I can keep the button disabled until the thread is finished.

Upvotes: 1

Views: 1939

Answers (2)

David Medenjak
David Medenjak

Reputation: 34542

The first part is easy: disable the button in onClicked

button.setEnabled(false);

This will provide proper feedback to the user.

The second part can be handled in many ways. You will need callback of some sorts in most of them.

  • If you are using an AsyncTask you should enable the button again in onPostExecute.

  • Callbacks with libraries like Retrofit or Volley would also just enable the button again on finish.

  • If you use DownloadManager you can and should listen for the broadcasts sent. You would need to store the id of the download to identify your files though.

All in all you need to provide methods of feedback and need to ensure that the callbacks are triggered, so that the button will not stay in its disabled state.

Upvotes: 1

Rohit5k2
Rohit5k2

Reputation: 18112

Use

button.setClickable(false);

rather than

button.disable();

Upvotes: 0

Related Questions