Programmer
Programmer

Reputation: 5400

Disable user interaction for few seconds android

what i am looking for is how to disable user interaction for few seconds, i have tried many methods but none of them is working here is one method.

   final LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout1);  
   ll.setEnabled(false);


    new Handler().postDelayed(new Runnable(){
        public void run() {
            ll.setEnabled(true);
        } 
    }, 3000); 

Upvotes: 0

Views: 1569

Answers (1)

Boris Strandjev
Boris Strandjev

Reputation: 46943

I think for your task using postDelayed thread is not the way to go. Use AsyncTask

One possible problem is that with your current approach you just approximate the waiting time to 3 secs, but with AsyncTask you can continue on immediately after the task finishes. Async tasks basically can execute something on the background, without blocking the UI. However you can also configure them to show progress dialog, which will block any user interactions with your application until the task finishes.

Here is an example of Async task that executes something in the background and shows progress dialog:

public class ProgressTask extends AsyncTask<Void, Void, Boolean> {
    /** progress dialog to show user that the backup is processing. */
    private ProgressDialog dialog;
    /** application context. */
    private Activity activity;

    public ProgressTask(Activity activity) {
        this.activity = activity;
        dialog = new ProgressDialog(context);
    }

    protected void onPreExecute() {
        this.dialog.setMessage("Please wait");
        this.dialog.show();
    }

    @Override
    protected void onPostExecute(final Boolean success) {
        if (dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    protected Boolean doInBackground(final Void... args) {
         // HERE GOES YOUR BACKGROUND WORK 
    }
}

I suggest you place your post delayed work in the place of // HERE GOES YOUR BACKGROUND WORK.

Upvotes: 2

Related Questions