Example usage for android.app ProgressDialog setOnKeyListener

List of usage examples for android.app ProgressDialog setOnKeyListener

Introduction

In this page you can find the example usage for android.app ProgressDialog setOnKeyListener.

Prototype

public void setOnKeyListener(@Nullable OnKeyListener onKeyListener) 

Source Link

Document

Sets the callback that will be called if a key is dispatched to the dialog.

Usage

From source file:com.synox.android.ui.dialog.IndeterminateProgressDialog.java

/**
 * {@inheritDoc}// ww w  . j  a  v  a  2s . c  om
 */
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    /// create indeterminate progress dialog
    final ProgressDialog progressDialog = new ProgressDialog(getActivity(), R.style.ProgressDialogTheme);
    progressDialog.setIndeterminate(true);
    progressDialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            ProgressBar v = (ProgressBar) progressDialog.findViewById(android.R.id.progress);
            v.getIndeterminateDrawable().setColorFilter(getResources().getColor(R.color.accent),
                    android.graphics.PorterDuff.Mode.MULTIPLY);

        }
    });

    /// set message
    int messageId = getArguments().getInt(ARG_MESSAGE_ID, R.string.placeholder_sentence);
    progressDialog.setMessage(getString(messageId));

    /// set cancellation behavior
    boolean cancelable = getArguments().getBoolean(ARG_CANCELABLE, false);
    if (!cancelable) {
        progressDialog.setCancelable(false);
        // disable the back button
        OnKeyListener keyListener = new OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {

                return keyCode == KeyEvent.KEYCODE_BACK;
            }

        };
        progressDialog.setOnKeyListener(keyListener);
    }

    return progressDialog;
}

From source file:org.thialfihar.android.apg.ui.dialog.ProgressDialogFragment.java

/**
 * Creates dialog/*from w  w w .  j  a v a  2 s.  c  om*/
 */
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    final Activity activity = getActivity();

    ProgressDialog dialog = new ProgressDialog(activity);
    dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    dialog.setCancelable(false);
    dialog.setCanceledOnTouchOutside(false);

    String message = getArguments().getString(ARG_MESSAGE);
    int style = getArguments().getInt(ARG_STYLE);
    boolean cancelable = getArguments().getBoolean(ARG_CANCELABLE);

    dialog.setMessage(message);
    dialog.setProgressStyle(style);

    if (cancelable) {
        dialog.setButton(DialogInterface.BUTTON_NEGATIVE, activity.getString(R.string.progress_cancel),
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
    }

    // Disable the back button
    OnKeyListener keyListener = new OnKeyListener() {

        @Override
        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                return true;
            }
            return false;
        }

    };
    dialog.setOnKeyListener(keyListener);

    return dialog;
}

From source file:org.fossasia.phimpme.share.nextcloud.IndeterminateProgressDialog.java

/**
 * {@inheritDoc}/*  w w  w . j  av a  2 s  .  c  om*/
 */
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    /// create indeterminate progress dialog
    final ProgressDialog progressDialog = new ProgressDialog(getActivity(), R.style.ProgressDialogTheme);
    progressDialog.setIndeterminate(true);
    progressDialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            ProgressBar v = (ProgressBar) progressDialog.findViewById(android.R.id.progress);
            v.getIndeterminateDrawable().setColorFilter(getResources().getColor(R.color.colorAccent),
                    android.graphics.PorterDuff.Mode.MULTIPLY);

        }
    });

    /// set message
    int messageId = getArguments().getInt(ARG_MESSAGE_ID, R.string.placeholder_sentence);
    progressDialog.setMessage(getString(messageId));

    /// set cancellation behavior
    boolean cancelable = getArguments().getBoolean(ARG_CANCELABLE, false);
    if (!cancelable) {
        progressDialog.setCancelable(false);
        // disable the back button
        OnKeyListener keyListener = new OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {

                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    return true;
                }
                return false;
            }

        };
        progressDialog.setOnKeyListener(keyListener);
    }

    return progressDialog;
}

From source file:com.cerema.cloud2.ui.dialog.IndeterminateProgressDialog.java

/**
 * {@inheritDoc}//from  ww w .  ja v a 2s .  c  o m
 */
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    /// create indeterminate progress dialog
    final ProgressDialog progressDialog = new ProgressDialog(getActivity(), R.style.ProgressDialogTheme);
    progressDialog.setIndeterminate(true);
    progressDialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            ProgressBar v = (ProgressBar) progressDialog.findViewById(android.R.id.progress);
            v.getIndeterminateDrawable().setColorFilter(getResources().getColor(R.color.color_accent),
                    android.graphics.PorterDuff.Mode.MULTIPLY);

        }
    });

    /// set message
    int messageId = getArguments().getInt(ARG_MESSAGE_ID, R.string.placeholder_sentence);
    progressDialog.setMessage(getString(messageId));

    /// set cancellation behavior
    boolean cancelable = getArguments().getBoolean(ARG_CANCELABLE, false);
    if (!cancelable) {
        progressDialog.setCancelable(false);
        // disable the back button
        OnKeyListener keyListener = new OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {

                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    return true;
                }
                return false;
            }

        };
        progressDialog.setOnKeyListener(keyListener);
    }

    return progressDialog;
}

From source file:com.example.android.foodrecipes.app.RecipeDetailsFragment.java

private void removeCurrentRecipeFromFavorites() {
    String title = getActivity().getString(R.string.text_please_wait);
    String text = getActivity().getString(R.string.text_removing_from_favorites);

    final ProgressDialog dialog = ProgressDialog.show(getActivity(), title, text);
    dialog.show();/*from   w w w .  ja  va2  s . c  o  m*/

    final AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
        @Override
        protected Void doInBackground(Void... params) {
            String where = RecipeContract.RecipeEntry.COLUMN_EXTERNAL_ID + "=?";
            String[] selectionArgs = new String[] { String.valueOf(mRecipe.getExternalId()) };
            getActivity().getContentResolver().delete(CONTENT_URI, where, selectionArgs);
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            dialog.dismiss();
            Toast.makeText(getActivity(), getActivity().getString(R.string.recipe_removed_from_favorites),
                    Toast.LENGTH_SHORT).show();

            if (!mTwoPane) {
                // if we operate in the two pane mode we should not finish the activity as
                // we have only one activity, but with two fragments inside
                getActivity().finish();
            }
        }
    };

    dialog.setOnKeyListener(new Dialog.OnKeyListener() {
        @Override
        public boolean onKey(DialogInterface dialogInterface, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                task.cancel(true);
                dialog.dismiss();
            }
            return true;
        }
    });

    task.execute();
}

From source file:com.example.android.foodrecipes.app.RecipeDetailsFragment.java

private void saveCurrentRecipeInFavorites() {
    String title = getActivity().getString(R.string.text_please_wait);
    String text = getActivity().getString(R.string.text_saving_in_favorites);

    final ProgressDialog dialog = ProgressDialog.show(getActivity(), title, text);
    final BitmapDrawable bitmapDrawable = (BitmapDrawable) mRecipeImage.getDrawable();

    dialog.show();// w ww.  j a  va2  s  .  c o m

    final AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
        @Override
        protected Void doInBackground(Void... params) {
            ContentValues cv = new ContentValues();
            cv.put(COLUMN_EXTERNAL_ID, mRecipe.getExternalId());
            cv.put(COLUMN_TITLE, mRecipe.getTitle());
            cv.put(COLUMN_IMAGE_URL, mRecipe.getImageUrl());
            cv.put(COLUMN_SOCIAL_RANK, mRecipe.getSocialRank());
            cv.put(COLUMN_SOURCE_URL, mRecipe.getSourceUrl());
            cv.put(COLUMN_INGREDIENTS, buildIngredientsString(mRecipe.getIngredients()));

            if (PrefsUtil.shouldSaveRecipeImages(getActivity())) {
                Bitmap bitmap = bitmapDrawable.getBitmap();
                if (bitmap != null) {
                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
                    cv.put(COLUMN_IMAGE_CONTENT, stream.toByteArray());
                }
            }

            getActivity().getContentResolver().insert(CONTENT_URI, cv);

            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            dialog.dismiss();
            Toast.makeText(getActivity(), getActivity().getString(R.string.recipe_saved_in_favorites),
                    Toast.LENGTH_SHORT).show();

            mFavoritesActionButton.setEnabled(false);
        }
    };

    dialog.setOnKeyListener(new Dialog.OnKeyListener() {
        @Override
        public boolean onKey(DialogInterface dialogInterface, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                task.cancel(true);
                dialog.dismiss();
            }
            return true;
        }
    });

    task.execute();
}