andreluizreis
7/6/2017 - 3:14 PM

Show Input Alert Dialog

Show Input Alert Dialog

/**
 * Dialog Type Enum
 */
private enum DialogType {
    FULL_NAME,
    EMAIL,
    PHONE,
    PASSWORD
}
    
/**
 * Show Input Alert Dialog
 *
 * @param dialogType dialog type
 */
private void showInputAlertDialog(final DialogType dialogType) {
    // Create builder
    final AlertDialog.Builder builder;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        builder = new AlertDialog.Builder(mActivityReference,
            R.style.AppTheme_AlertDialog);
    } else {
        builder = new AlertDialog.Builder(mActivityReference);
    }

    // Set the dialog title
    builder.setTitle(selectTitle(dialogType));

    // Inflate the layout
    LayoutInflater inflater = LayoutInflater.from(mActivityReference);
    final View dialogView = inflater.inflate(R.layout.layout_dialog_input_text, null);
    builder.setView(dialogView);

    // Edit Text
    mInputEditText = (EditText) dialogView.findViewById(R.id.dialog_edit_text);
    createEditText(dialogType);

    // Remove error if user change the text
    mInputEditText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            mInputEditText.setError(null);
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

    // Set up the buttons
    builder.setPositiveButton(mContext.getString(R.string.save), new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    });

    builder.setNegativeButton(mContext.getString(R.string.cancel), new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });

    AlertDialog dialog = builder.create();

    // Open soft keyboard
    dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
        WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
    dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

    dialog.setOnShowListener(new DialogInterface.OnShowListener() {

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

                @Override
                public void onClick(View view) {

                    // Positive Button Click
                    String value = mInputEditText.getText().toString();
                    handlePositiveButton(value, dialogType);
                }
            });
        }
    });

    // Show dialog
    dialog.show();
    mInputEditText.requestFocus();
}