Question Detail

How to create Android Default Material Design AlertDialog Box 
I have tried  GitHub - pedant/sweet-alert-dialog: SweetAlert for Android, a beautiful  this is third Party AlertBox very Good but i want to use Android Default AlertBox.

Pre-Lollipop Material AlertDialog Box Android


Thread Reply

Nick Johnson

- 11 months ago

You can use Android Basic AlertDialog for this no need to use any third party AlertBox 
 

 private void showLocationDialog(String title, String Message, int drawable) {

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(title);
        builder.setIcon(drawable);
        builder.setCancelable(true);
        builder.setMessage(Message);
        String positiveText = getString(android.R.string.ok);
        builder.setPositiveButton(positiveText,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                      //opration do here on Click "Ok"
                    }
                });
        builder.setNegativeButton("Close", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                 //opration do here on Click "Ok"
                dialog.dismiss();
            }
        });
        AlertDialog dialog = builder.create();
        // display dialog
        dialog.show();
    }

Anonymous

- 11 months ago

private void createAndDisplayDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        LinearLayout layout       = new LinearLayout(this);
        TextView tvMessage        = new TextView(this);
        final EditText etInput    = new EditText(this);

        tvMessage.setText("Enter name:");
        tvMessage.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16f);
        etInput.setSingleLine();
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.addView(tvMessage);
        layout.addView(etInput);
        layout.setPadding(50, 40, 50, 10);

        builder.setView(layout);

        builder.setNegativeButton("Cancel", (dialog, which) -> {
            Toast.makeText(this, "Cancel clicked", Toast.LENGTH_SHORT).show();
            dialog.cancel();
        });

        builder.setPositiveButton("Done", (dialog, which) -> {
            String name = etInput.getText().toString();
            Toast.makeText(this, "Name entered: " + name, Toast.LENGTH_SHORT).show();
        });

        builder.create().show();
    }