Creating a positiveButton in a Dialog on Android

1

I have the following method to create a Dialog in an Android application:

public static void alertBuilder(String mensagem, Type tipo, Context contexto){

    Dialog dialog = new Dialog(contexto, R.style.alert);
    dialog.setContentView(R.layout.alert_box);

    TextView text = dialog.getWindow().findViewById(R.id.mensagem);
    TextView type = dialog.getWindow().findViewById(R.id.alerta);
    ImageView image = dialog.getWindow().findViewById(R.id.imagem);

    switch (tipo) {
        case SUCCESS:
            image.setImageDrawable(Drawable.createFromPath("@drawable/ic_ok"));
            type.setText("Pronto!");
            text.setText(mensagem);
            break;
        case ERROR:
            type.setText("Atenção!");
            text.setText(mensagem);
            break;
    }
    dialog.show();
}

And I wanted to add an "ok" button that closed Dialog when clicked, but there is no method of setPositiveButton in class Dialog as in class AlertDialog .

I can not do the conversion from one class to another, because as you can see in that code snippet, I have a layout universal for Dialog of my application, which modifies the layout message depending on kind of event.

Any suggestions on how to add this button?

    
asked by anonymous 21.06.2018 / 20:36

1 answer

2
  

And I wanted to add an "ok" button that would close Dialog when it was clicked. [...]   Any suggestions on how to add this button?

1. Creating a Button in the Dialog Layout

You can add Button to the background of your own alert_box.xml , say:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@android:string/ok"
    android:id="@+id/dialog_box_botao_ok"
    style="@style/Widget.AppCompat.Button.Borderless.Colored"/>

And then make it dispense the dialog when clicked:

Button b = dialog.findViewById(R.id.dialog_box_botao_ok);
b.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        dialog.dismiss();
    }
});

2. Using an AlertDialog

public static void alertBuilder(String mensagem, Type tipo, Context contexto) {
    AlertDialog.Builder builder = new AlertDialog.Builder(contexto);
    // Aí está
    View dialogView = getLayoutInflater().inflate(R.layout.alert_box null);

    TextView text = dialogView.findViewById(R.id.mensagem);
    TextView type = dialogView.findViewById(R.id.alerta);
    ImageView image = dialogView.findViewById(R.id.imagem);

    //builder.setTitle(android.R.string.dialog_alert_title);
    //builder.setMessage(mensagem);
    builder.setView(dialogView);

    switch (tipo) {
        case SUCCESS:
            image.setImageDrawable(Drawable.createFromPath("@drawable/ic_ok"));
            type.setText("Pronto!");
            text.setText(mensagem);
            break;
        case ERROR:
            type.setText("Atenção!");
            text.setText(mensagem);
            break;
    }

    builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int id) {

        }
    });
    builder.show();
}
    
22.06.2018 / 00:09