androiddevjedi
androiddevjedi

Reputation: 143

Disable Buttons during AsyncTask

I programmed an quiz-app and if I touch one of the answers in an AsyncTask if the answer is correct I set the color to green or if it is false to red.

But now during the time the AsyncTask runs I can press other buttons like the "change question" button or on another one of the answers. This is then done after the AsyncTask has finished it's work. So the next question is loaded and it automatically answers the next question or uses one of the jokers what ever.

I tried to setEnabled(false) the Buttons but they are still bugging.

How do I prevent this?

private void disableOrDisableButtons(boolean boo) {
        buttonAnswer1.setEnabled(boo);
        buttonAnswer2.setEnabled(boo);
        buttonAnswer3.setEnabled(boo);
        buttonAnswer4.setEnabled(boo);
    }

and here I start the AsyncTask

disableOrDisableButtons(false);
new PrepareAdapter().execute(null, null, null);

in my AsyncTask

@Override
        protected void onPreExecute() {
            disableOrDisableButtons(false);
            if (correctAnswerAtButton != buttonClicked) {
                switch (buttonClicked) {
                case 1:
                    buttonAnswer1.setTextColor(Color.RED);
                    break;
                case 2:
                    buttonAnswer2.setTextColor(Color.RED);
                    break;
                case 3:
                    buttonAnswer3.setTextColor(Color.RED);
                    break;
                case 4:
                    buttonAnswer4.setTextColor(Color.RED);
                    break;
                }
                if (buttonClicked != 0) { // 0.. if second chance joker used
                    wrongAnswer = true;
                }
            }
            switch (correctAnswerAtButton) {
            case 1:
                buttonAnswer1.setTextColor(Color.GREEN);
                return;
            case 2:
                buttonAnswer2.setTextColor(Color.GREEN);
                return;
            case 3:
                buttonAnswer3.setTextColor(Color.GREEN);
                return;
            case 4:
                buttonAnswer4.setTextColor(Color.GREEN);
                return;
            }
        }

Upvotes: 3

Views: 3653

Answers (4)

androiddevjedi
androiddevjedi

Reputation: 143

This is the way i use it now to lock my screen during the AsyncTask. For me it is perfect now. Hope it can help u.

private class PrepareAdapter extends AsyncTask<Void, Void, Integer>
            implements DialogInterface.OnCancelListener {
        private Dialog dialog = null;

        @Override
        protected void onPreExecute() {
            // To disable the whole screen --> setCancelable(false);
            dialog = new Dialog(WerWeissWasQuizActivity.this, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
            dialog.setCancelable(false);
            dialog.show();
        }

        @Override
        protected Integer doInBackground(Void... params) {
            dialog.dismiss();
            return 0;
        }

        @Override
        protected void onPostExecute(Integer result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            //do other stuff...
        }

        @Override
        public void onCancel(DialogInterface dialog) {
            // TODO Auto-generated method stub
            cancel(true);
        }
    }

Upvotes: 0

Philip Sheard
Philip Sheard

Reputation: 5825

Your problem is not related to threads at all. Try setTextColor(#ff0000) and settextColor(#00ff00), instead of settextColor(Color.RED) and setTextColor(Color.GREEN).

Upvotes: 0

Alexandru Cristescu
Alexandru Cristescu

Reputation: 3948

I you want to disable the whole interface while the AsyncTask runs, you can use code such as the following to display a dialog:

public abstract class BaseAsyncTask<Param, Result> extends AsyncTask<Param, Void, Result> implements DialogInterface.OnCancelListener {
    private static final String TAG = "BaseAsyncTask";
    private ProgressDialog dialog = null;
    protected Context ctx = null;
    protected Exception exception = null;

    public BaseAsyncTask(Context ctx) {
        this.ctx = ctx;
    }

    @Override
    protected void onPreExecute() {
        dialog = ProgressDialog.show(ctx, WLConstants.MSG_TITLE_LOADING_DIALOG, WLConstants.MSG_LOADING_DIALOG, true);
        dialog.setCancelable(true);
        dialog.setOnCancelListener(this);
        if (ctx instanceof WozzonActivity) {
            ((WozzonActivity) ctx).setCurrentDialog(dialog);
        }
    }

    @Override
    protected Result doInBackground(Param... parameters) {
        try {
            return inBackground(parameters);
        } catch (Exception ex) {
            exception = ex;
            Log.e(TAG, ex.getClass().getName(), ex);
            return null;
        }
    };

    @Override
    protected void onPostExecute(Result result) {
        try {
            dialog.dismiss();
        } catch (Exception ex) {
        }// TODO:
        if (result == null) {
            onException(exception);
        } else {
            onResult(result);
        }
    }

    protected void onException(Exception ex) {
        if (ex != null && ex instanceof WozzonException) {
            Toast.makeText(ctx, ex.getMessage(), Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(ctx, WLConstants._ERROR_MSG, Toast.LENGTH_SHORT).show();
        }

    }

    public abstract void onResult(Result result);
    public abstract Result inBackground(Param... parameters) throws Exception;

    @Override
    public void onCancel(DialogInterface theDialog) {
        cancel(true);
    }
}

Upvotes: 1

You need to use the onPreExecute() method of the ASyncTask().

Upvotes: 0

Related Questions