Example usage for android.app AlertDialog setOnShowListener

List of usage examples for android.app AlertDialog setOnShowListener

Introduction

In this page you can find the example usage for android.app AlertDialog setOnShowListener.

Prototype

public void setOnShowListener(@Nullable OnShowListener listener) 

Source Link

Document

Sets a listener to be invoked when the dialog is shown.

Usage

From source file:com.otaupdater.utils.UserUtils.java

public static void showLoginDialog(final Context ctx, String defUsername, final DialogCallback dlgCallback,
        final LoginCallback loginCallback) {
    @SuppressLint("InflateParams")
    View view = LayoutInflater.from(ctx).inflate(R.layout.login_dialog, null);
    if (view == null)
        return;/*from w w w.  j  ava  2  s .  c o m*/
    final EditText inputUsername = (EditText) view.findViewById(R.id.auth_username);
    final EditText inputPassword = (EditText) view.findViewById(R.id.auth_password);

    if (defUsername != null)
        inputUsername.setText(defUsername);

    AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
    builder.setTitle(R.string.alert_login_title);
    builder.setView(view);
    builder.setPositiveButton(R.string.login, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            /* set below */ }
    });
    builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
            if (loginCallback != null)
                loginCallback.onCancel();
        }
    });

    final AlertDialog dlg = builder.create();
    dlg.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            if (dlgCallback != null)
                dlgCallback.onDialogShown(dlg);
            Button button = dlg.getButton(DialogInterface.BUTTON_POSITIVE);
            if (button == null)
                return;
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final String username = inputUsername.getText().toString();
                    final String password = inputPassword.getText().toString();

                    if (username.length() == 0 || password.length() == 0) {
                        Toast.makeText(ctx, R.string.toast_blank_userpass_error, Toast.LENGTH_LONG).show();
                        return;
                    }

                    dlg.dismiss();

                    APIUtils.userLogin(ctx, username, password, new APIUtils.ProgressDialogAPICallback(ctx,
                            ctx.getString(R.string.alert_logging_in), dlgCallback) {
                        @Override
                        public void onSuccess(String message, JSONObject respObj) {
                            try {
                                String realUsername = respObj.getString("username");
                                String hmacKey = respObj.getString("key");
                                Config.getInstance(ctx).storeLogin(realUsername, hmacKey);
                                if (loginCallback != null)
                                    loginCallback.onLoggedIn(realUsername);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onError(String message, JSONObject respObj) {
                            //TODO show some error
                        }
                    });
                }
            });
        }
    });
    dlg.setOnDismissListener(new DialogInterface.OnDismissListener() {
        @Override
        public void onDismiss(DialogInterface dialog) {
            if (dlgCallback != null)
                dlgCallback.onDialogClosed(dlg);
        }
    });
    dlg.show();
}

From source file:com.otaupdater.utils.Utils.java

public static void showProKeyOnlyFeatureDialog(final Context ctx, final DialogCallback callback) {
    AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
    builder.setTitle(R.string.prokey_only_feature_title);
    builder.setMessage(R.string.prokey_only_feature_message);
    builder.setPositiveButton(R.string.prokey_only_get, new DialogInterface.OnClickListener() {
        @Override//from   w ww  .java2s  .co m
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
            Intent i = new Intent(ctx, SettingsActivity.class);
            i.setAction(SettingsActivity.EXTRA_SHOW_GET_PROKEY_DLG);
            i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
            ctx.startActivity(i);
        }
    });
    builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });

    final AlertDialog dlg = builder.create();
    dlg.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            if (callback != null)
                callback.onDialogShown(dlg);
        }
    });
    dlg.setOnDismissListener(new DialogInterface.OnDismissListener() {
        @Override
        public void onDismiss(DialogInterface dialog) {
            if (callback != null)
                callback.onDialogClosed(dlg);
        }
    });
    dlg.show();
}

From source file:it.mb.whatshare.Dialogs.java

/**
 * Shows a dialog to get a name for the inbound device being paired and
 * starts a new {@link CallGooGlInbound} action if the chosen name is valid.
 * //  w  ww . j  a va 2s.c  o  m
 * @param deviceType
 *            the model of the device being paired as suggested by the
 *            device itself
 * @param sharedSecret
 *            the keys used when encrypting the message between devices
 * @param activity
 *            the caller activity
 */
public static void promptForInboundName(final String deviceType, final int[] sharedSecret,
        final MainActivity activity) {
    Handler handler = new Handler(Looper.getMainLooper());
    handler.post(new Runnable() {

        @Override
        public void run() {
            DialogFragment prompt = new PatchedDialogFragment() {
                public Dialog onCreateDialog(Bundle savedInstanceState) {
                    AlertDialog.Builder builder = getBuilder(activity);
                    final EditText input = new EditText(getContext());
                    input.setInputType(InputType.TYPE_CLASS_TEXT);
                    input.setText(deviceType);
                    input.setSelection(deviceType.length());
                    // @formatter:off
                    builder.setTitle(R.string.device_name_chooser_title).setView(input)
                            .setPositiveButton(android.R.string.ok, null);
                    // @formatter:on
                    final AlertDialog alertDialog = builder.create();
                    alertDialog.setOnShowListener(
                            new DeviceNameChooserPrompt(alertDialog, input, activity, new Callback<String>() {

                                @Override
                                public void run() {
                                    // @formatter:off
                                    new CallGooGlInbound(activity, getParam(), deviceType)
                                            .execute(sharedSecret);

                                    ((InputMethodManager) activity
                                            .getSystemService(Context.INPUT_METHOD_SERVICE))
                                                    .hideSoftInputFromWindow(input.getWindowToken(), 0);
                                    // @formatter:on
                                }
                            }));
                    return alertDialog;
                }
            };
            prompt.show(activity.getSupportFragmentManager(), "chooseName");
        }
    });
}

From source file:it.mb.whatshare.Dialogs.java

/**
 * Shows a prompt to the user to rename the argument <tt>device</tt>.
 * //from   w  ww. j ava2s . co  m
 * @param device
 *            the device to be renamed
 * @param activity
 *            the parent activity
 */
public static void promptForNewDeviceName(final PairedDevice device, final MainActivity activity) {
    Handler handler = new Handler(Looper.getMainLooper());
    handler.post(new Runnable() {

        @Override
        public void run() {
            DialogFragment prompt = new PatchedDialogFragment() {
                public Dialog onCreateDialog(Bundle savedInstanceState) {
                    AlertDialog.Builder builder = getBuilder(activity);

                    final EditText input = new EditText(getContext());
                    input.setInputType(InputType.TYPE_CLASS_TEXT);
                    input.setText(device.name);
                    input.setSelection(device.name.length());

                    // @formatter:off
                    builder.setTitle(R.string.device_name_chooser_title).setView(input)
                            .setPositiveButton(android.R.string.ok, null);
                    // @formatter:on

                    final AlertDialog alertDialog = builder.create();
                    alertDialog.setOnShowListener(
                            new DeviceNameChooserPrompt(alertDialog, input, activity, new Callback<String>() {

                                @Override
                                public void run() {
                                    // @formatter:off
                                    device.rename(getParam());
                                    activity.onSelectedDeviceRenamed();

                                    ((InputMethodManager) activity
                                            .getSystemService(Context.INPUT_METHOD_SERVICE))
                                                    .hideSoftInputFromWindow(input.getWindowToken(), 0);
                                    // @formatter:on
                                }
                            }));
                    return alertDialog;
                }
            };
            prompt.show(activity.getSupportFragmentManager(), "chooseName");
        }
    });
}

From source file:com.mobicage.rogerthat.util.ui.UIUtils.java

public static AlertDialog showDialog(final Context ctx, String title, String message, String positiveCaption,
        SafeDialogClick onPositiveButtonClick, String negativeCaption, SafeDialogClick onNegativeButtonClick,
        View view, boolean show) {
    AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
    if (title != null) {
        builder.setTitle(title);// w  w w  .  j  a  v a  2  s . c  o  m
    }
    if (message != null) {
        builder.setMessage(message);
    }
    builder.setPositiveButton(positiveCaption, onPositiveButtonClick);
    if (negativeCaption != null) {
        builder.setNegativeButton(negativeCaption, onNegativeButtonClick);
    }
    if (view != null) {
        builder.setView(view);
    }
    final AlertDialog dialog = builder.create();
    dialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialogInterface) {
            UIUtils.setDialogButtonColors(ctx, dialog);
        }
    });
    if (show) {
        dialog.show();
    }
    return dialog;
}

From source file:com.nikhilnayak.games.octoshootar.ui.dialogfragments.CraftNotEnoughResourcesDialogFragment.java

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setMessage(String.format(getString(R.string.craft_dialog_fragment_not_enough_resources),
            getArguments().getString(EXTRA_ADDITIONAL_MESSAGE)));
    builder.setNegativeButton(R.string.craft_dialog_fragment_ok_response, null);
    AlertDialog alertDialog = builder.create();
    alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override//from  w ww .  j  a va  2 s .c om
        public void onShow(DialogInterface dialog) {
            Button negativeButton = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_NEGATIVE);
            negativeButton.setBackgroundResource(R.drawable.button_dialog);
        }
    });
    return alertDialog;
}

From source file:org.cvasilak.jboss.mobile.app.fragments.dialogs.ParameterizedDialogFragment.java

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog dialog = builder.create();

    if (showListener != null)
        dialog.setOnShowListener(showListener);

    return dialog;
}

From source file:com.otaupdater.utils.UserUtils.java

public static void showAccountDialog(final Context ctx, final DialogCallback dlgCallback) {
    final Config cfg = Config.getInstance(ctx);

    final AlertDialog dlg = new AlertDialog.Builder(ctx).setTitle("OTA Update Center Account")
            .setMessage("Logged in as: " + cfg.getUsername())
            .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                @Override/*from   w ww  .j  a v a 2  s  .  com*/
                public void onClick(DialogInterface dialogInterface, int i) {
                }
            }).setNegativeButton("Sign Out", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    APIUtils.userLogout(ctx, new APIUtils.APIAdapter() {
                        @Override
                        public void onSuccess(String message, JSONObject respObj) {
                            cfg.clearLogin();
                        }
                    });
                }
            }).create();

    dlg.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialogInterface) {
            if (dlgCallback != null)
                dlgCallback.onDialogShown(dlg);
        }
    });
    dlg.setOnDismissListener(new DialogInterface.OnDismissListener() {
        @Override
        public void onDismiss(DialogInterface dialog) {
            if (dlgCallback != null)
                dlgCallback.onDialogClosed(dlg);
        }
    });
    dlg.show();
}

From source file:com.nikhilnayak.games.octoshootar.ui.dialogfragments.InventoryItemEntryDetailDialogFragment.java

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    mInventoryItemEntry = getArguments().getParcelable(EXTRA_INVENTORY_ITEM_ENTRY);
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    mInventoryItemEntryViewDetailView = new InventoryItemEntryDetailView(getActivity());
    mInventoryItemEntryViewDetailView.setModel(mInventoryItemEntry);
    mInventoryItemEntryViewDetailView.setCraftRequestListener(this);
    builder.setView(mInventoryItemEntryViewDetailView);

    builder.setCancelable(true);//from w w  w .  j  a  v  a2  s .c om
    builder.setPositiveButton(R.string.craft_dialog_fragment_ok_response, null);

    AlertDialog alertDialog = builder.create();
    alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            Button positiveButton = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE);
            positiveButton.setBackgroundResource(R.drawable.button_dialog);
        }
    });
    return alertDialog;
}

From source file:com.nikhilnayak.games.octoshootar.ui.dialogfragments.CraftRequestDialogFragment.java

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    mInventoryItemEntry = getArguments().getParcelable(EXTRA_INVENTORY_ITEM_ENTRY);
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setMessage(String.format(getString(R.string.craft_dialog_fragment_request),
            mInventoryItemEntry.getRecipe().toString(getActivity()),
            getResources().getQuantityString(mInventoryItemEntry.getTitleResourceId(), 1)));
    builder.setPositiveButton(R.string.craft_dialog_fragment_yes_response,
            new DialogInterface.OnClickListener() {
                @Override/*from  w  w  w. ja  v  a 2 s  . c o m*/
                public void onClick(DialogInterface dialog, int which) {
                    mListener.onCraftValidated(mInventoryItemEntry);
                }
            });

    builder.setNegativeButton(R.string.craft_dialog_fragment_no_response, null);

    AlertDialog alertDialog = builder.create();
    alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            Button negativeButton = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_NEGATIVE);
            negativeButton.setBackgroundResource(R.drawable.button_dialog);
            Button positiveButton = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE);
            positiveButton.setBackgroundResource(R.drawable.button_dialog);
        }
    });
    return alertDialog;
}