Is possible to customize positive and negative buttons in AlertDialog?

孤街醉人 提交于 2019-12-04 08:55:46
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);
    }
}
}

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

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

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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!