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.wordpress.ebc81.rtl_ais_android.tools.DialogManager.java

/**
 * Add new dialogs here!/*from   w w w. j  a v a 2  s  . co  m*/
 * @param id
 * @param args
 * @return
 */
private Dialog createDialog(final dialogs id, final String[] args) {
    switch (id) {
    case DIAG_LIST_USB:
        return genUSBDeviceDialog();
    case DIAG_ABOUT:
        final AlertDialog addd = new AlertDialog.Builder(getActivity()).setTitle(R.string.help)
                .setPositiveButton(R.string.btn_ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).setMessage(Html.fromHtml(getString(R.string.help_info))).create();
        try {
            addd.setOnShowListener(new DialogInterface.OnShowListener() {

                @Override
                public void onShow(DialogInterface paramDialogInterface) {
                    try {
                        final TextView tv = (TextView) addd.getWindow().findViewById(android.R.id.message);
                        if (tv != null)
                            tv.setMovementMethod(LinkMovementMethod.getInstance());

                    } catch (Exception e) {
                    }
                }
            });
        } catch (Exception e) {
        }

        return addd;
    case DIAG_LICENSE:
        return new AlertDialog.Builder(getActivity()).setTitle("COPYING")
                .setPositiveButton(R.string.btn_ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).setMessage(readWholeStream("COPYING")).create();
    }
    return null;
}

From source file:hku.fyp14017.blencode.ui.dialogs.LoginRegisterDialog.java

@Override
public Dialog onCreateDialog(Bundle bundle) {
    View rootView = LayoutInflater.from(getActivity())
            .inflate(hku.fyp14017.blencode.R.layout.dialog_login_register, null);

    usernameEditText = (EditText) rootView.findViewById(hku.fyp14017.blencode.R.id.username);
    passwordEditText = (EditText) rootView.findViewById(hku.fyp14017.blencode.R.id.password);
    termsOfUseLinkTextView = (TextView) rootView.findViewById(hku.fyp14017.blencode.R.id.register_terms_link);

    String termsOfUseUrl = getString(hku.fyp14017.blencode.R.string.about_link_template,
            Constants.CATROBAT_TERMS_OF_USE_URL,
            getString(hku.fyp14017.blencode.R.string.register_pocketcode_terms_of_use_text));
    termsOfUseLinkTextView.setMovementMethod(LinkMovementMethod.getInstance());
    termsOfUseLinkTextView.setText(Html.fromHtml(termsOfUseUrl));

    usernameEditText.setText("");
    passwordEditText.setText("");

    final AlertDialog loginRegisterDialog = new AlertDialog.Builder(getActivity()).setView(rootView)
            .setTitle(hku.fyp14017.blencode.R.string.login_register_dialog_title)
            .setPositiveButton(hku.fyp14017.blencode.R.string.login_or_register, null)
            .setNeutralButton(hku.fyp14017.blencode.R.string.password_forgotten, null).create();
    loginRegisterDialog.setCanceledOnTouchOutside(true);
    loginRegisterDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

    loginRegisterDialog.setOnShowListener(new OnShowListener() {
        @Override/*from  w w w  .j a v  a  2  s.  co m*/
        public void onShow(DialogInterface dialog) {
            InputMethodManager inputManager = (InputMethodManager) getActivity()
                    .getSystemService(Context.INPUT_METHOD_SERVICE);
            inputManager.showSoftInput(usernameEditText, InputMethodManager.SHOW_IMPLICIT);

            Button loginRegisterButton = loginRegisterDialog.getButton(AlertDialog.BUTTON_POSITIVE);
            loginRegisterButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    handleLoginRegisterButtonClick();
                }
            });

            Button passwordFhkuottenButton = loginRegisterDialog.getButton(AlertDialog.BUTTON_NEUTRAL);
            passwordFhkuottenButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    handlePasswordForgottenButtonClick();
                }
            });
        }
    });

    return loginRegisterDialog;
}

From source file:com.fitme.MainActivity.java

/**
 * A fragment representing a exercises managment section of the app
 *//*from w w  w. j  a  v  a 2s .c  o m*/

private void onAddProgramRequested(final ProgramListAdapter pda) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(R.string.title_add_program_dialog);
    builder.setView(this.getLayoutInflater().inflate(R.layout.dialog_add_program, null));
    builder.setPositiveButton(android.R.string.ok, null);
    builder.setNegativeButton(android.R.string.cancel, null);
    final AlertDialog programDialog = builder.create();
    programDialog.setOnShowListener(new DialogInterface.OnShowListener() {

        @Override
        public void onShow(DialogInterface dialog) {
            Button positive = programDialog.getButton(AlertDialog.BUTTON_POSITIVE);
            positive.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Program p = new Program();
                    EditText etName = (EditText) programDialog.findViewById(R.id.edittext_program_name);
                    EditText etDesc = (EditText) programDialog.findViewById(R.id.edittext_program_desc);
                    String programName = etName.getText().toString();
                    String programDesc = etDesc.getText().toString();
                    // Check if form filled correct
                    // Check if program name is empty
                    if (programName.isEmpty()) {
                        Toast.makeText(MainActivity.this, getString(R.string.toast_err_program_empty),
                                Toast.LENGTH_SHORT).show();
                        return;
                    }
                    // Check if program already exists
                    ProgramDAO pd = new ProgramDAO(MainActivity.this);
                    if (pd.getProgramByName(programName).getId() != ProgramDAO.ID_PROGRAM_NOT_FOUND) {
                        Toast.makeText(MainActivity.this, getString(R.string.toast_err_program_exists),
                                Toast.LENGTH_SHORT).show();
                        return;
                    }
                    // If everything is ok - then add new program
                    p.setName(programName);
                    p.setDescription(programDesc);
                    pd.createProgram(p);
                    Toast.makeText(MainActivity.this, getText(R.string.toast_add_program_ok),
                            Toast.LENGTH_SHORT).show();
                    pda.addProgram(p);
                    programDialog.dismiss();
                }
            });
        }
    });
    programDialog.show();

}

From source file:com.fitme.MainActivity.java

public void onTrainingEditRequested(final TrainingRow oldTrain, final TrainingsListAdapter tla) {
    final Activity activity = MainActivity.this;
    AlertDialog.Builder builder = new AlertDialog.Builder(activity);
    builder.setTitle(getString(R.string.title_upd_training_dialog));
    builder.setView(getLayoutInflater().inflate(R.layout.dialog_add_training, null));
    builder.setPositiveButton(android.R.string.ok, null);
    builder.setNegativeButton(android.R.string.cancel, null);
    final AlertDialog trainDialog = builder.create();
    trainDialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override/*w ww . j ava 2s  .  c  o  m*/
        public void onShow(DialogInterface dialog) {
            Button positive = trainDialog.getButton(AlertDialog.BUTTON_POSITIVE);
            positive.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    EditText etName = (EditText) trainDialog.findViewById(R.id.edittext_training_name);
                    String trainName = etName.getText().toString();
                    // Validating form fields
                    // Check if program name changed
                    if (!trainName.equals(oldTrain.getName())) {
                        // If chanded - check it
                        // Check if empty
                        if (trainName.isEmpty()) {
                            Toast.makeText(MainActivity.this, getString(R.string.toast_err_training_empty),
                                    Toast.LENGTH_SHORT).show();
                            return;
                        }
                        // Check if training already exists

                        TrainingDAO td = new TrainingDAO(MainActivity.this);
                        long programId = getActiveProgramId();
                        if (td.getTrainingByName(programId, trainName)
                                .getId() != TrainingDAO.ID_TRAINING_NOT_FOUND) {
                            Toast.makeText(MainActivity.this, getString(R.string.toast_err_training_exists),
                                    Toast.LENGTH_SHORT).show();
                            return;
                        }
                    } else {
                        // if program name stays the same - do nothing
                        trainDialog.dismiss();
                        return;

                    }

                    // if everything is ok - update program record
                    TrainingDAO td = new TrainingDAO(activity);
                    Training t = new Training();
                    long programId = getActiveProgramId();
                    t.setName(trainName);
                    t.setProgramId(programId);
                    t.setId(oldTrain.getId());
                    td.updateTraining(t);
                    Toast.makeText(activity, getText(R.string.toast_upd_training_ok), Toast.LENGTH_SHORT)
                            .show();
                    tla.updateTraining(t);
                    trainDialog.dismiss();
                }
            });

        }
    });
    trainDialog.show();
    // Initiate data with old program entry
    EditText etName = (EditText) trainDialog.findViewById(R.id.edittext_training_name);
    etName.setText(oldTrain.getName());
}

From source file:com.fitme.MainActivity.java

public void onProgramEditRequested(final ProgramRow oldProg, final ProgramListAdapter pda) {
    final Activity activity = MainActivity.this;
    AlertDialog.Builder builder = new AlertDialog.Builder(activity);
    builder.setTitle(getString(R.string.title_upd_program_dialog));
    builder.setView(getLayoutInflater().inflate(R.layout.dialog_add_program, null));
    builder.setPositiveButton(android.R.string.ok, null);
    builder.setNegativeButton(android.R.string.cancel, null);
    final AlertDialog programDialog = builder.create();
    programDialog.setOnShowListener(new DialogInterface.OnShowListener() {

        @Override/*from   ww w.j  a  va 2s . c  o m*/
        public void onShow(DialogInterface dialog) {
            Button positive = programDialog.getButton(AlertDialog.BUTTON_POSITIVE);
            positive.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    EditText etName = (EditText) programDialog.findViewById(R.id.edittext_program_name);
                    EditText etDesc = (EditText) programDialog.findViewById(R.id.edittext_program_desc);
                    String programName = etName.getText().toString();
                    String programDesc = etDesc.getText().toString();
                    // Validating form fields
                    // Check if program name changed
                    if (!programName.equals(oldProg.getName())) {
                        // If chanded - check it
                        // Check if empty
                        if (programName.isEmpty()) {
                            Toast.makeText(MainActivity.this, getString(R.string.toast_err_program_empty),
                                    Toast.LENGTH_SHORT).show();
                            return;
                        }
                        // Check if program already exists
                        ProgramDAO pd = new ProgramDAO(MainActivity.this);
                        if (pd.getProgramByName(programName).getId() != ProgramDAO.ID_PROGRAM_NOT_FOUND) {
                            Toast.makeText(MainActivity.this, getString(R.string.toast_err_program_exists),
                                    Toast.LENGTH_SHORT).show();
                            return;
                        }
                    } else {
                        // if program name stays the same - check description
                        // if description is the same - nothing happens
                        if (programDesc.equals(oldProg.getDescription())) {
                            programDialog.dismiss();
                            return;
                        }
                    }

                    // if everything is ok - update program record
                    Program p = new Program();
                    p.setName(programName);
                    p.setDescription(programDesc);
                    ProgramDAO pd = new ProgramDAO(activity);
                    pd.updateByName(oldProg.getName(), p);
                    Toast.makeText(activity, getText(R.string.toast_upd_program_ok), Toast.LENGTH_SHORT).show();
                    pda.updateProgram(oldProg.getName(), p);
                    programDialog.dismiss();
                }
            });

        }
    });
    programDialog.show();
    // Initiate data with old program entry
    EditText etName = (EditText) programDialog.findViewById(R.id.edittext_program_name);
    EditText etDesc = (EditText) programDialog.findViewById(R.id.edittext_program_desc);
    etName.setText(oldProg.getName());
    etDesc.setText(oldProg.getDescription());
}

From source file:com.fitme.MainActivity.java

public void onTrainingCreationRequested(final TrainingsListAdapter tla) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(R.string.title_add_training_dialog);
    View addTrainDialogView = getLayoutInflater().inflate(R.layout.dialog_add_training, null);
    builder.setView(addTrainDialogView);

    builder.setPositiveButton(android.R.string.ok, null);
    builder.setNegativeButton(android.R.string.cancel, null);

    final AlertDialog trainingDialog = builder.create();

    trainingDialog.setOnShowListener(new DialogInterface.OnShowListener() {

        @Override//  w  w w  .  j  ava  2s .  co m
        public void onShow(DialogInterface dialog) {
            Button positive = trainingDialog.getButton(AlertDialog.BUTTON_POSITIVE);
            positive.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // handle OK

                    EditText etName = (EditText) trainingDialog.findViewById(R.id.edittext_training_name);
                    String trainingName = etName.getText().toString();

                    // Check if form filled correct
                    // Check if training name is empty
                    if (trainingName.isEmpty()) {
                        Toast.makeText(MainActivity.this, getString(R.string.toast_err_training_empty),
                                Toast.LENGTH_SHORT).show();
                        return;
                    }
                    // Check if training already exists
                    long programId = getActiveProgramId();
                    TrainingDAO td = new TrainingDAO(MainActivity.this);
                    if (td.getTrainingByName(programId, trainingName)
                            .getId() != TrainingDAO.ID_TRAINING_NOT_FOUND) {
                        Toast.makeText(MainActivity.this, getString(R.string.toast_err_training_exists),
                                Toast.LENGTH_SHORT).show();
                        return;
                    }
                    // If everything is ok - then add new program
                    Training t = new Training();
                    t.setName(trainingName);
                    t.setProgramId(programId);
                    //Insert entry to db
                    td.createTraining(t);
                    Toast.makeText(MainActivity.this, getText(R.string.toast_add_training_ok),
                            Toast.LENGTH_SHORT).show();
                    tla.addTraining(t);
                    trainingDialog.dismiss();
                }
            });

            Button negative = trainingDialog.getButton(AlertDialog.BUTTON_NEGATIVE);
            negative.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    trainingDialog.dismiss();

                }
            });

        }
    });
    trainingDialog.show();
}

From source file:com.fitme.MainActivity.java

public void onExerciseCreationRequested(final String trainName, final TrainingsListAdapter tla) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(R.string.title_add_exercise_dialog);
    View view = getLayoutInflater().inflate(R.layout.dialog_add_exercise, null);
    builder.setView(view);/*from   w ww  . ja  v  a 2 s. c o  m*/
    builder.setPositiveButton(android.R.string.ok, null);
    builder.setNegativeButton(android.R.string.cancel, null);
    final AlertDialog dialogEx = builder.create();
    dialogEx.setOnShowListener(new DialogInterface.OnShowListener() {

        @Override
        public void onShow(DialogInterface dialog) {
            Button positive = dialogEx.getButton(AlertDialog.BUTTON_POSITIVE);
            positive.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Spinner spinMuscleGroup = (Spinner) dialogEx.findViewById(R.id.spinner_select_mg_ex);
                    Spinner spinExName = (Spinner) dialogEx.findViewById(R.id.spinner_select_ex);
                    NumberPicker numpickRepeats = (NumberPicker) dialogEx
                            .findViewById(R.id.numberpicker_repeats_ex);
                    //Since we decide to have all spinners pre-populated no need to check data
                    Exercise e = new Exercise();
                    e.setName(spinExName.getSelectedItem().toString());
                    e.setMuscleGroup(spinMuscleGroup.getSelectedItem().toString());
                    e.setRepeats(numpickRepeats.getValue());
                    //Identifying program and training
                    long programId = MainActivity.this.getActiveProgramId();
                    TrainingDAO td = new TrainingDAO(MainActivity.this);
                    long trId = td.getTrainingByName(programId, trainName).getId();
                    e.setTrainId(trId);

                    //Insert new entry to db
                    ExerciseDAO ed = new ExerciseDAO(MainActivity.this);
                    long exId = ed.createExercise(e).getId();
                    e.setId(exId);
                    Toast.makeText(MainActivity.this, getText(R.string.toast_add_exercise_ok),
                            Toast.LENGTH_SHORT).show();
                    tla.addExercise(e, trainName);
                    dialogEx.dismiss();
                }
            });

        }
    });
    dialogEx.show();
    // Populate dialog fields
    NumberPicker np = (NumberPicker) dialogEx.findViewById(R.id.numberpicker_repeats_ex);
    final Spinner spinMuscleGroup = (Spinner) dialogEx.findViewById(R.id.spinner_select_mg_ex);
    final Spinner spinExName = (Spinner) dialogEx.findViewById(R.id.spinner_select_ex);
    np.setMaxValue(Exercise.MAX_REPEATS);
    np.setMinValue(Exercise.MIN_REPEATS);

    //Filling muscle groups spinner
    String[] muscleGroups = getResources().getStringArray(R.array.muscle_groups);

    ArrayAdapter<String> mgAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,
            muscleGroups);
    spinMuscleGroup.setAdapter(mgAdapter);

    spinMuscleGroup.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            String arrayName = ("exercises_" + spinMuscleGroup.getSelectedItem().toString()).toLowerCase();
            int id = getResources().getIdentifier(arrayName, "array", MainActivity.this.getPackageName());
            String[] exercises = getResources().getStringArray(id);
            ArrayAdapter<String> exAdapter = new ArrayAdapter<String>(MainActivity.this,
                    android.R.layout.simple_spinner_item, exercises);
            spinExName.setAdapter(exAdapter);
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });
    spinMuscleGroup.setSelection(1);

}

From source file:com.craftingmobile.alertdialogusage.LoginDialogFragment.java

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    super.onCreateDialog(savedInstanceState);
    Log.i(TAG, "onCreateDialog");
    LayoutInflater lf = LayoutInflater.from(getActivity());

    /** //  w  w  w  .j  a va  2  s .c  o  m
     *  Inflate our custom view for this dialog, it contains
     *  two TextViews and two EditTexts, one set for the username
     *  and a second set for the password
     */
    View v = lf.inflate(R.layout.login_dialog, null);

    username = (TextView) v.findViewById(R.id.login_username);
    password = (TextView) v.findViewById(R.id.login_password);

    /**
     * Register our listener for the 'Done' key on the soft keyboard
     */
    password.setOnEditorActionListener(this);
    username.requestFocus();

    final AlertDialog dialog = new AlertDialog.Builder(getActivity()).setView(v).setTitle(R.string.log_in)
            .setPositiveButton(R.string.log_in, null).setNegativeButton(R.string.cancel, null).create();

    /**
     * We have to override setOnShowListener here (min API level 8)
     * in order to validate the inputs before closing the dialog.
     * Just overriding setPositiveButton closes the dialog
     * automatically when the button is pressed
     */
    dialog.setOnShowListener(this);

    /**
     * Show the soft keyboard automatically
     */
    dialog.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);

    /**
     * These TextWatchers are used to clear the error icons
     * automatically once the user has remedied an error
     */
    username.addTextChangedListener(this);
    password.addTextChangedListener(this);
    return dialog;
}

From source file:com.cuddlesoft.nori.fragment.AddTagFilterDialogFragment.java

@SuppressLint("InflateParams")
@Override//from  w w  w.j  av a 2  s  .com
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Inflate the Dialog view XML.
    final LayoutInflater inflater = LayoutInflater.from(getActivity());
    final View view = inflater.inflate(R.layout.dialog_add_tag_filter, null);
    tagEditText = (EditText) view.findViewById(R.id.editText);

    // Restore preserved text from savedInstanceState, if possible.
    if (savedInstanceState != null && savedInstanceState.containsKey(BUNDLE_ID_TAG_FILTER)) {
        tagEditText.setText(savedInstanceState.getString(BUNDLE_ID_TAG_FILTER));
    }

    // Create the AlertDialog object.
    final AlertDialog alertDialog = new AlertDialog.Builder(getActivity()).setView(view)
            .setPositiveButton(R.string.action_add, null)
            .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    // Dismiss dialog.
                    dismiss();
                }
            }).create();

    // onShowListener is used here as a hack to override Android DialogInterface's default onClickInterface
    // that doesn't provide a way to prevent the dialog from getting dismissed when a button is clicked.
    alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialogInterface) {
            alertDialog.getButton(AlertDialog.BUTTON_POSITIVE)
                    .setOnClickListener(AddTagFilterDialogFragment.this);
        }
    });

    return alertDialog;
}

From source file:org.catrobat.catroid.ui.dialogs.NewSpriteDialog.java

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    dialogView = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_new_object, null);
    setupPaintroidButton(dialogView);/*from www . ja v a  2 s. com*/
    setupGalleryButton(dialogView);
    setupCameraButton(dialogView);
    setupMediaLibraryButton(dialogView);

    AlertDialog dialog = null;
    AlertDialog.Builder dialogBuilder = new CustomAlertDialogBuilder(getActivity()).setView(dialogView)
            .setTitle(R.string.new_sprite_dialog_title);
    if (wizardStep == DialogWizardStep.STEP_1) {
        dialog = createDialogStepOne(dialogBuilder);
    } else if (wizardStep == DialogWizardStep.STEP_2) {
        dialog = createDialogStepTwo(dialogBuilder);
    }
    dialog.setOnShowListener(new DialogInterface.OnShowListener() {

        @Override
        public void onShow(final DialogInterface dialog) {
            Button button = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE);
            button.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    if (handleOkButton()) {
                        dialog.dismiss();
                    }
                }
            });
        }
    });
    dialog.setCanceledOnTouchOutside(true);
    dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

    return dialog;
}