Creating a positiveButton in a Dialog on Android

Asked

Viewed 153 times

1

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

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 a "ok" button that would close the Dialog when it was clicked, but there is no method of setPositiveButton in class Dialog as in class AlertDialog.

I can’t make the conversion from one class to another, because as you see there in this piece of code, I have a layout universal to the Dialog of my application, which modifies the message of layout depending on the type of event.

Any hints on how to add this button?

1 answer

3


And I wanted to add a "ok" button that would close the dialog when it was clicked. [...] Any hints on how to add this button?

1. Creating a Button in the dialog layout

You can add a Button at the bottom of their 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 him dispense with the dialogue by being 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 a 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();
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.