Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is possible to customize positive and negative buttons in AlertDialog?

Is possible to customize positive and negative buttons in AlertDialog ? I need to replace default look of positive and negative with custom.

.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {...
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {...

Can somebody tell me how to do that ?

like image 868
Damir Avatar asked Jan 14 '12 10:01

Damir


People also ask

How do I change the color of my AlertDialog Builder positive button?

There are two ways to change the dialog button color. I'll recommend adding a theme for AlertDialog in styles. xml so you don't have to write the same code again and again in each activity/dialog call. You can just create a style and apply that theme on the dialog box.

How many button options can you use in creating AlertDialog?

An alert dialog can have maximum three action buttons. If you want the user to accept the action, use Positive action button. It is normally displayed as OK/YES. If the user wants to cancel the action , then you can use Negative action button (NO).

What is the difference between dialog and AlertDialog?

AlertDialog is a lightweight version of a Dialog. This is supposed to deal with INFORMATIVE matters only, That's the reason why complex interactions with the user are limited. Dialog on the other hand is able to do even more complex things .

How do you set the functionality to the positive button for the alert dialog box?

setPositiveButton() is used to create a positive button in alert dialog and setNegativeButton() is used to invoke negative button to alert dialog. Here setNeutralButton() is used to create a neutral cancel button.


4 Answers

public class ComentarDialog extends DialogFragment{

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    builder.setMessage("Mensaje de alerta")
           .setTitle("Comentar")
           .setPositiveButton("OK", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {

               }
           })
           .setNegativeButton("CANCELAR", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {

               }
           });

    return builder.create();
}

@Override
public void onStart() {
    super.onStart();

    //Personalizamos

    Resources res = getResources();

    //Buttons
    Button positive_button =  ((AlertDialog) getDialog()).getButton(DialogInterface.BUTTON_POSITIVE);
    positive_button.setBackground(res.getDrawable(R.drawable.btn_selector_dialog));

    Button negative_button =  ((AlertDialog) getDialog()).getButton(DialogInterface.BUTTON_NEGATIVE);
    negative_button.setBackground(res.getDrawable(R.drawable.btn_selector_dialog));

    int color = Color.parseColor("#304f5a");

    //Title
    int titleId = res.getIdentifier("alertTitle", "id", "android");
    View title = getDialog().findViewById(titleId);
    if (title != null) {
        ((TextView) title).setTextColor(color);
    }

    //Title divider
    int titleDividerId = res.getIdentifier("titleDivider", "id", "android");
    View titleDivider = getDialog().findViewById(titleDividerId);
    if (titleDivider != null) {
        titleDivider.setBackgroundColor(color);
    }
}
}
like image 175
Eragonz91 Avatar answered Sep 21 '22 20:09

Eragonz91


Yuo can set every view in dialog box. You can set view with two buttons and dont set positive & negative buttons.

example:

AlertDialog.Builder builder = 
            new AlertDialog.Builder(this);

View dialogView = LayoutInflater.from(this)
            .inflate(R.layout.my_layout, null);

builder.setView(dialogView);
like image 29
Roman Black Avatar answered Sep 21 '22 20:09

Roman Black


if you want to customize you can use dialog instead of alert dialog here is the sample code

    final Dialog dialog = new Dialog(ThisweekActivity.this, android.R.style.Theme_Translucent_NoTitleBar);
    View view = LayoutInflater.from(ThisweekActivity.this).inflate(R.layout.issue_cover_prompt_layout, null);
    view.findViewById(R.id.close_btn).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });
    ImageView img = (ImageView) view.findViewById(R.id.issue_cover_img);
    img.setImageBitmap(issue.getCoverImage());

    dialog.setContentView(view);
    dialog.show();

you can set set the layout in dialog and add click listner on it

like image 20
Harsh Dev Chandel Avatar answered Sep 17 '22 20:09

Harsh Dev Chandel


if you want to change them, I will suggest using activity with your required layout and add them= Dialog in your activity, in Manifest file

like image 33
AAnkit Avatar answered Sep 20 '22 20:09

AAnkit