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.jaspersoft.android.jaspermobile.dialog.PageDialogFragment.java

@NonNull
@Override/*  www .j  av a 2s  .c  o m*/
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
    ViewGroup customView = (ViewGroup) layoutInflater.inflate(R.layout.page_dialog_layout,
            (ViewGroup) getActivity().getWindow().getDecorView(), false);
    final EditText numberEditText = (EditText) customView.findViewById(R.id.customNumber);

    builder.setTitle(R.string.rv_select_page);
    builder.setView(customView);
    builder.setNegativeButton(android.R.string.cancel, null);
    builder.setPositiveButton(android.R.string.ok, null);

    final AlertDialog dialog = builder.create();
    dialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialogInterface) {
            numberEditText.requestFocus();
            inputMethodManager.showSoftInput(numberEditText, 0);

            dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (numberEditText.length() == 0) {
                        numberEditText.setError(getString(R.string.sp_error_field_required));
                    } else {
                        int page;
                        try {
                            page = Integer.valueOf(numberEditText.getText().toString());
                        } catch (NumberFormatException ex) {
                            page = Integer.MAX_VALUE;
                            numberEditText.setText(String.valueOf(page));
                        }

                        if (onPageSelectedListener != null) {
                            onPageSelectedListener.onPageSelected(page);
                            dismiss();
                        }
                    }
                }
            });
        }
    });
    return dialog;
}

From source file:org.projectbuendia.client.ui.dialogs.NewUserDialogFragment.java

@Override
public @NonNull Dialog onCreateDialog(Bundle savedInstanceState) {
    View fragment = mInflater.inflate(R.layout.new_user_dialog_fragment, null);
    ButterKnife.inject(this, fragment);

    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity()).setCancelable(false) // Disable auto-cancel.
            .setTitle(getResources().getString(R.string.title_new_user))
            .setPositiveButton(getResources().getString(R.string.ok), null)
            .setNegativeButton(getResources().getString(R.string.cancel), null).setView(fragment);

    final AlertDialog dialog = dialogBuilder.create();
    dialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override//from ww  w .  j  av a2s . co  m
        public void onShow(DialogInterface dialogInterface) {
            dialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    // Validate the user.
                    String givenName = mGivenName.getText() == null ? ""
                            : mGivenName.getText().toString().trim();
                    String familyName = mFamilyName.getText() == null ? ""
                            : mFamilyName.getText().toString().trim();
                    boolean valid = true;
                    if (givenName.isEmpty()) {
                        setError(mGivenName, R.string.given_name_cannot_be_null);
                        valid = false;
                    }
                    if (familyName.isEmpty()) {
                        setError(mFamilyName, R.string.family_name_cannot_be_null);
                        valid = false;
                    }
                    Utils.logUserAction("add_user_submitted", "valid", "" + valid, "given_name", givenName,
                            "family_name", familyName);
                    if (!valid)
                        return;

                    App.getUserManager().addUser(new JsonNewUser(givenName, familyName));
                    if (mActivityUi != null) {
                        mActivityUi.showSpinner(true);
                    }
                    dialog.dismiss();
                }
            });
        }
    });
    // Open the keyboard, ready to type into the given name field.
    dialog.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
    return dialog;
}

From source file:org.projectbuendia.client.ui.dialogs.AddNewUserDialogFragment.java

@NonNull
@Override//  ww w.  j a v  a  2 s. c o m
public Dialog onCreateDialog(Bundle savedInstanceState) {
    View fragment = mInflater.inflate(R.layout.dialog_fragment_add_new_user, null);
    ButterKnife.inject(this, fragment);

    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity()).setCancelable(false) // Disable auto-cancel.
            .setTitle(getResources().getString(R.string.title_add_new_user))
            .setPositiveButton(getResources().getString(R.string.ok), null)
            .setNegativeButton(getResources().getString(R.string.cancel), null).setView(fragment);

    final AlertDialog dialog = dialogBuilder.create();
    dialog.setOnShowListener(new DialogInterface.OnShowListener() {

        @Override
        public void onShow(DialogInterface dialogInterface) {
            dialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    // Validate the user.
                    String givenName = mGivenName.getText() == null ? ""
                            : mGivenName.getText().toString().trim();
                    String familyName = mFamilyName.getText() == null ? ""
                            : mFamilyName.getText().toString().trim();
                    boolean valid = true;
                    if (givenName.isEmpty()) {
                        setError(mGivenName, R.string.given_name_cannot_be_null);
                        valid = false;
                    }
                    if (familyName.isEmpty()) {
                        setError(mFamilyName, R.string.family_name_cannot_be_null);
                        valid = false;
                    }
                    Utils.logUserAction("add_user_submitted", "valid", "" + valid, "given_name", givenName,
                            "family_name", familyName);
                    if (!valid) {
                        return;
                    }

                    App.getUserManager().addUser(new NewUser(givenName, familyName));
                    if (mActivityUi != null) {
                        mActivityUi.showSpinner(true);
                    }
                    dialog.dismiss();
                }
            });
        }
    });

    return dialog;
}

From source file:io.jari.geenstijl.Dialogs.ReplyDialog.java

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = getActivity().getLayoutInflater();
    final View view = inflater.inflate(R.layout.dialog_reply, null);
    builder.setView(view).setPositiveButton(R.string.reply, null)
            .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    ReplyDialog.this.getDialog().cancel();
                }//from w ww.ja  va 2  s  .c om
            }).setTitle(R.string.reply);
    final AlertDialog alertDialog = builder.create();
    alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            final View error = view.findViewById(R.id.error);
            final Button positive = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
            final Button negative = alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE);
            positive.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    positive.setEnabled(false);
                    negative.setEnabled(false);
                    final TextView reply = (TextView) view.findViewById(R.id.reply);
                    final View loading = view.findViewById(R.id.loading);
                    reply.setVisibility(View.GONE);
                    loading.setVisibility(View.VISIBLE);
                    error.setVisibility(View.GONE);
                    alertDialog.setCancelable(false);
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            boolean success;
                            try {
                                success = API.reply(artikel, reply.getText().toString(), activity);
                            } catch (Exception e) {
                                e.printStackTrace();
                                success = false;
                            }
                            alertDialog.setCancelable(true);
                            if (!success)
                                activity.runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        reply.setVisibility(View.VISIBLE);
                                        error.setVisibility(View.VISIBLE);
                                        positive.setEnabled(true);
                                        negative.setEnabled(true);
                                        loading.setVisibility(View.GONE);
                                    }
                                });
                            else
                                activity.runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        alertDialog.dismiss();
                                        Crouton.makeText(activity, getString(R.string.reply_success),
                                                Style.CONFIRM).show();
                                    }
                                });
                        }
                    }).start();
                }
            });
        }
    });

    return alertDialog;
}

From source file:com.innoc.secureline.ui.UpgradeCallDialogFragment.java

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    final AlertDialog.Builder builder;
    if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.HONEYCOMB) {
        builder = new AlertDialog.Builder(
                new ContextThemeWrapper(getActivity(), R.style.RedPhone_Light_Dialog));
    } else {//  www .ja  va 2  s .  c  o m
        builder = new AlertDialog.Builder(getActivity(), R.style.RedPhone_Light_Dialog);
    }

    builder.setIcon(R.drawable.red_call);

    final String upgradeString = getActivity().getResources()
            .getString(R.string.RedPhoneChooser_upgrade_to_redphone);
    SpannableStringBuilder titleBuilder = new SpannableStringBuilder(upgradeString);
    titleBuilder.setSpan(new AbsoluteSizeSpan(20, true), 0, upgradeString.length(),
            Spanned.SPAN_INCLUSIVE_INCLUSIVE);
    builder.setTitle(titleBuilder);

    //builder.setMessage(R.string.RedPhoneChooser_this_contact_also_uses_redphone_would_you_like_to_upgrade_to_a_secure_call);

    builder.setPositiveButton(R.string.RedPhoneChooser_secure_call, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            Intent intent = new Intent(getActivity(), SecureLineService.class);
            intent.setAction(SecureLineService.ACTION_OUTGOING_CALL);
            intent.putExtra(Constants.REMOTE_NUMBER, number);
            getActivity().startService(intent);

            Intent activityIntent = new Intent();
            activityIntent.setClass(getActivity(), SecureLine.class);
            activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(activityIntent);

            getActivity().finish();
        }
    });

    builder.setNegativeButton(R.string.RedPhoneChooser_insecure_call, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            CallChooserCache.getInstance().addInsecureChoice(number);

            Intent intent = new Intent("android.intent.action.CALL",
                    Uri.fromParts("tel", number + CallListener.IGNORE_SUFFIX, null));
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            getActivity().finish();
        }
    });

    AlertDialog alert = builder.create();

    alert.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            ((AlertDialog) dialog).setOnCancelListener(new DialogInterface.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialogInterface) {
                    getActivity().finish();
                }
            });

            ((AlertDialog) dialog).setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialogInterface) {
                    getActivity().finish();
                }
            });
            Button positiveButton = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE);

            Button negativeButton = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_NEGATIVE);
        }
    });

    return alert;
}

From source file:com.mindprotectionkit.freephone.ui.UpgradeCallDialogFragment.java

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    final AlertDialog.Builder builder;
    if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.HONEYCOMB) {
        builder = new AlertDialog.Builder(
                new ContextThemeWrapper(getActivity(), R.style.RedPhone_Light_Dialog));
    } else {/*from w  w  w.j  a  va  2s.co  m*/
        builder = new AlertDialog.Builder(getActivity(), R.style.RedPhone_Light_Dialog);
    }

    builder.setIcon(R.drawable.red_call);

    final String upgradeString = getActivity().getResources()
            .getString(R.string.RedPhoneChooser_upgrade_to_redphone);
    SpannableStringBuilder titleBuilder = new SpannableStringBuilder(upgradeString);
    titleBuilder.setSpan(new AbsoluteSizeSpan(20, true), 0, upgradeString.length(),
            Spanned.SPAN_INCLUSIVE_INCLUSIVE);
    builder.setTitle(titleBuilder);

    //builder.setMessage(R.string.RedPhoneChooser_this_contact_also_uses_redphone_would_you_like_to_upgrade_to_a_secure_call);

    builder.setPositiveButton(R.string.RedPhoneChooser_secure_call, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            Intent intent = new Intent(getActivity(), RedPhoneService.class);
            intent.setAction(RedPhoneService.ACTION_OUTGOING_CALL);
            intent.putExtra(Constants.REMOTE_NUMBER, number);
            getActivity().startService(intent);

            Intent activityIntent = new Intent();
            activityIntent.setClass(getActivity(), RedPhone.class);
            activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(activityIntent);

            getActivity().finish();
        }
    });

    builder.setNegativeButton(R.string.RedPhoneChooser_insecure_call, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            CallChooserCache.getInstance().addInsecureChoice(number);

            Intent intent = new Intent("android.intent.action.CALL",
                    Uri.fromParts("tel", number + CallListener.IGNORE_SUFFIX, null));
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            getActivity().finish();
        }
    });

    AlertDialog alert = builder.create();

    alert.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            ((AlertDialog) dialog).setOnCancelListener(new DialogInterface.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialogInterface) {
                    getActivity().finish();
                }
            });

            ((AlertDialog) dialog).setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialogInterface) {
                    getActivity().finish();
                }
            });
            Button positiveButton = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE);

            Button negativeButton = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_NEGATIVE);
        }
    });

    return alert;
}

From source file:com.securecomcode.voice.ui.UpgradeCallDialogFragment.java

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    final AlertDialog.Builder builder;
    if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.HONEYCOMB) {
        builder = new AlertDialog.Builder(
                new ContextThemeWrapper(getActivity(), R.style.RedPhone_Light_Dialog));
    } else {//from  w  w  w.  j ava2  s . c o  m
        builder = new AlertDialog.Builder(getActivity(), R.style.RedPhone_Light_Dialog);
    }

    builder.setIcon(R.drawable.red_call);

    final String upgradeString = getActivity().getResources()
            .getString(R.string.RedPhoneChooser_upgrade_to_redphone);
    SpannableStringBuilder titleBuilder = new SpannableStringBuilder(upgradeString);
    titleBuilder.setSpan(new AbsoluteSizeSpan(20, true), 0, upgradeString.length(),
            Spanned.SPAN_INCLUSIVE_INCLUSIVE);
    builder.setTitle(titleBuilder);

    builder.setMessage(
            R.string.RedPhoneChooser_this_contact_also_uses_redphone_would_you_like_to_upgrade_to_a_secure_call);

    builder.setPositiveButton(R.string.RedPhoneChooser_secure_call, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            Intent intent = new Intent(getActivity(), RedPhoneService.class);
            intent.setAction(RedPhoneService.ACTION_OUTGOING_CALL);
            intent.putExtra(Constants.REMOTE_NUMBER, number);
            getActivity().startService(intent);

            Intent activityIntent = new Intent();
            activityIntent.setClass(getActivity(), RedPhone.class);
            activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(activityIntent);

            getActivity().finish();
        }
    });

    builder.setNegativeButton(R.string.RedPhoneChooser_insecure_call, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            CallChooserCache.getInstance().addInsecureChoice(number);

            Intent intent = new Intent("android.intent.action.CALL",
                    Uri.fromParts("tel", number + CallListener.IGNORE_SUFFIX, null));
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            getActivity().finish();
        }
    });

    AlertDialog alert = builder.create();

    alert.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            ((AlertDialog) dialog).setOnCancelListener(new DialogInterface.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialogInterface) {
                    getActivity().finish();
                }
            });

            ((AlertDialog) dialog).setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialogInterface) {
                    getActivity().finish();
                }
            });
            Button positiveButton = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE);

            Button negativeButton = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_NEGATIVE);
        }
    });

    return alert;
}

From source file:at.jclehner.androidutils.AlertDialogFragment.java

@Override
public final Dialog onCreateDialog(Bundle savedInstanceState) {
    final AlertDialog.Builder ab = new AlertDialog.Builder(getActivity());
    ab.setTitle(getTitle());/*from   w  ww .j av a2 s  .  c o m*/
    ab.setMessage(getMessage());
    ab.setIcon(getIcon());

    // listeners are null because we use View.OnClickListener instead
    // of DialogInterface.OnClickListener
    ab.setPositiveButton(getPositiveButtonText(), null);
    ab.setNeutralButton(getNeutralButtonText(), null);
    ab.setNegativeButton(getNegativeButtonText(), null);

    onPrepareDialogBuilder(ab);

    final AlertDialog dialog = ab.create();
    dialog.setOnShowListener(mOnShowListener);

    return dialog;
}

From source file:io.jari.geenstijl.Dialogs.LoginDialog.java

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    // Get the layout inflater
    LayoutInflater inflater = getActivity().getLayoutInflater();

    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog layout
    final View view = inflater.inflate(R.layout.dialog_login, null);
    builder.setView(view)// w  ww. jav  a 2s. c o  m
            // Add action buttons
            .setPositiveButton(R.string.signin, null)
            .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    LoginDialog.this.getDialog().cancel();
                }
            }).setTitle(R.string.signin);
    final AlertDialog alertDialog = builder.create();
    alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            //i wrote this code with a certain amount of alcohol in my blood, so i can't vouch for it's readability
            final View error = view.findViewById(R.id.error);
            final Button positive = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
            final Button negative = alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE);
            positive.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    positive.setEnabled(false);
                    negative.setEnabled(false);
                    final TextView username = (TextView) view.findViewById(R.id.username);
                    final TextView password = (TextView) view.findViewById(R.id.password);
                    final View loading = view.findViewById(R.id.loading);
                    username.setVisibility(View.GONE);
                    password.setVisibility(View.GONE);
                    loading.setVisibility(View.VISIBLE);
                    error.setVisibility(View.GONE);
                    alertDialog.setCancelable(false);
                    new Thread(new Runnable() {
                        public void run() {
                            boolean success;
                            try {
                                success = API.logIn(username.getText().toString(),
                                        password.getText().toString(), LoginDialog.this.getActivity());
                            } catch (Exception e) {
                                e.printStackTrace();
                                success = false;
                            }
                            alertDialog.setCancelable(true);
                            if (!success)
                                activity.runOnUiThread(new Runnable() {
                                    public void run() {
                                        username.setVisibility(View.VISIBLE);
                                        password.setVisibility(View.VISIBLE);
                                        error.setVisibility(View.VISIBLE);
                                        positive.setEnabled(true);
                                        negative.setEnabled(true);
                                        loading.setVisibility(View.GONE);
                                    }
                                });
                            else
                                activity.runOnUiThread(new Runnable() {
                                    public void run() {
                                        alertDialog.dismiss();
                                        forceOptionsReload();
                                        Crouton.makeText(activity,
                                                getString(R.string.loggedin, API.getUsername(activity)),
                                                Style.CONFIRM).show();
                                    }
                                });
                        }
                    }).start();
                }
            });
        }
    });

    return alertDialog;
}

From source file:de.vanita5.twittnuker.fragment.support.DataExportImportTypeSelectorDialogFragment.java

@Override
public final Dialog onCreateDialog(final Bundle savedInstanceState) {
    final Context context = getActivity();
    final int flags = getEnabledFlags();
    mAdapter = new TypeAdapter(context, flags);
    mListView = new ListView(context);
    mAdapter.add(new Type(R.string.settings, FLAG_PREFERENCES));
    mAdapter.add(new Type(R.string.nicknames, FLAG_NICKNAMES));
    mAdapter.add(new Type(R.string.user_colors, FLAG_USER_COLORS));
    mAdapter.add(new Type(R.string.custom_host_mapping, FLAG_HOST_MAPPING));
    mListView.setAdapter(mAdapter);/*from  ww w  . j a  v a2 s. c  om*/
    mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    mListView.setOnItemClickListener(this);
    for (int i = 0, j = mAdapter.getCount(); i < j; i++) {
        mListView.setItemChecked(i, mAdapter.isEnabled(i));
    }
    final AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setTitle(getTitle());
    builder.setView(mListView);
    builder.setPositiveButton(android.R.string.ok, this);
    builder.setNegativeButton(android.R.string.cancel, null);
    final AlertDialog dialog = builder.create();
    dialog.setOnShowListener(this);
    return dialog;
}