Skip to content Skip to sidebar Skip to footer

Appcompat Alert Dialog Action Button Background On Pressed State

I am trying new AlertDialog from appcompat v7 22.1.1. It works pretty well (In all android versions) as in image. Style for AlertDialog is this. (For now I am using hardcoded colo

Solution 1:

You can use style attributes like

  • buttonBarButtonStyle
  • buttonBarNegativeButtonStyle
  • buttonBarNeutralButtonStyle
  • buttonBarPositiveButtonStyle

Example:

<stylename="dialog_theme"parent="Theme.AppCompat.Dialog.Alert"><itemname="buttonBarNegativeButtonStyle">@style/dialog_button.negative</item><itemname="buttonBarPositiveButtonStyle">@style/dialog_button.positive</item></style><stylename="dialog_button"><itemname="android:textStyle">bold</item><itemname="android:minWidth">64dp</item><itemname="android:paddingLeft">8dp</item><itemname="android:paddingRight">8dp</item><itemname="android:background">@drawable/dialogButtonSelector</item></style><stylename="dialog_button.negative"><itemname="android:textColor">#f00</item></style><stylename="dialog_button.positive"><itemname="android:layout_marginLeft">8dp</item><itemname="android:textColor">#00f</item></style>

Where dialogButtonSelector is our custom drawable selector.

Unfortunatelly setting background on dialog_button destroy our paddings and margins so I need to set it again.

dialog_button style can inherit through Widget.AppCompat.Button.ButtonBar.AlertDialog but I found that it has missing styles like textStylebold.

Solution 2:

I have the Answer for the 3rd Questions ( How can I have different colors of action buttons (Is it possible)? )

Code:

// Initialize AlertDialog & AlertDialog Builder
AlertDialog.Builderbuilder=newAlertDialog.Builder(YourActivity.this);
builder.setTitle("AlertDialog Title");
...........
......... 
//Build your AlertDialog 
AlertDialog Demo_alertDialog= builder.create();
Demo_alertDialog.show();

//For Positive Button:
Button b_pos; 
b_pos=Demo_alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
if(b_pos!=null){
   b_pos.setTextColor(getResources().getColor(R.color.YourColor));
   }    


//For Neutral Button:
Button b_neu;
b_neu=Demo_alertDialog.getButton(DialogInterface.BUTTON_NEUTRAL);
if(b_neu!=null){
   b_neu.setTextColor(getResources().getColor(R.color.YourColor));
   }

//For Negative Button:
Button b_neg;
b_neg=Demo_alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE);
if(b_neg!=null){
   b_neg.setTextColor(getResources().getColor(R.color.YourColor));
   }

Happy Coding :)

Solution 3:

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setMessage("Title"); builder.setCancelable(true);

builder.setPositiveButton("OK",
        newDialogInterface.OnClickListener() {
            publicvoidonClick(DialogInterface dialog, int id) {
                dialog.cancel();

            }
        });

builder.setNegativeButton("Cancel",
        newDialogInterface.OnClickListener() {
            publicvoidonClick(DialogInterface dialog, int id) {
                dialog.cancel();
            }
        });

AlertDialog alertdialog = builder.create();
alertdialog.show();

Button nbutton = alertdialog.getButton(DialogInterface.BUTTON_NEGATIVE);
nbutton.setBackground(getResources().getDrawable(R.drawable.btn_press_white_rect));

Button pbutton = alertdialog.getButton(DialogInterface.BUTTON_POSITIVE);
pbutton.setBackground(getResources().getDrawable(R.drawable.btn_press_white_rect));

        **btn_press_white_rect.xml**

 <selectorxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:state_pressed="true"android:drawable="@drawable/rounded_rect_yellow" ></item><itemandroid:state_pressed="false"android:drawable="@drawable/rounded_rect_white" ></item></selector>

Post a Comment for "Appcompat Alert Dialog Action Button Background On Pressed State"