-1

The problem is, I'm having an AlertDialog, in which I inflate my custom layout by this:

AlertDialog.Builder mBuilder = new AlertDialog.Builder(getActivity());
View mView  = getLayoutInflater().inflate(R.layout.exp_add, null);
mBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) { 
                         //some of my data and mView manipulation
                    });


mBuilder.setView(mView);
AlertDialog dialog = mBuilder.create();
dialog.show();

The question is how do I customize .setPositiveButton, because I'm having a view with a specific background color, but mBuilder adds a button with default white background color and a pink text button.

Is there any way to customize this button?

Vignesh VS
  • 921
  • 1
  • 14
  • 30
V. Dalechyn
  • 314
  • 1
  • 10

1 Answers1

1
AlertDialog.Builder builder = new AlertDialog.Builder(context);
    LayoutInflater inflater = context.getLayoutInflater();

    //setting custom view for our dialog
    View myview = inflater.inflate(R.layout.YOUR_CUSTOM_LAYOUT, null);
    builder.setNeutralButton(android.R.string.cancel, null);
    builder.setView(myview);

    //creating an alert dialog from our builder.
    AlertDialog dialog = builder.create();
    dialog.show();

    //retrieving the button view in order to handle it.
    Button neutral_button = dialog.getButton(DialogInterface.BUTTON_NEUTRAL);

    Button positive_button = dialog.getButton(DialogInterface.BUTTON_POSITIVE);


    if (neutral_button != null) {
        neutral_button.setBackgroundDrawable(context.getResources()
                        .getDrawable(R.drawable.custom_background));

        neutral_button.setTextColor(context.getResources()
                        .getColor(android.R.color.white));
    }
    if (positive_button != null) {
        positive_button.setBackgroundDrawable(context.getResources()
                        .getDrawable(R.drawable.custom_background));

        positive_button.setTextColor(context.getResources()
                        .getColor(android.R.color.white));
    }
Mohammed Farhan
  • 1,120
  • 8
  • 14
Bhavin Soni
  • 94
  • 1
  • 3