Appcompat Alert Alert Alert Dialog Button Action Button Button

I am trying to create a new AlertDialog from appcompat v7 22.1.1.

It works very well (in all versions of Android), as in the image.

enter image description here

The style for AlertDialog is this. (Now I use hard-coded color values ​​instead of color resources)

 <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <item name="colorPrimaryDark">#111111</item> <item name="colorPrimary">#00ddff</item> <item name="colorAccent">#0044aa</item> <item name="colorButtonNormal">#00aaaa</item> <item name="colorControlHighlight">#00ddff</item> <item name="alertDialogTheme">@style/AlertDialogTheme</item> </style> <style name="AlertDialogTheme" parent="Theme.AppCompat.Dialog.Alert"> <item name="colorAccent">#0044aa</item> <item name="android:background">#ffffff</item> <item name="android:textColorPrimary">#000000</item> <item name="android:windowTitleStyle">@style/MyTitleTextStyle</item> </style> <style name="MyTitleTextStyle"> <item name="android:textColor">#0044aa</item> <item name="android:textAppearance">@style/TextAppearance.AppCompat.Title</item> </style> 

My question is:

1) how to change statePressed color, which is rounded (gray) in the image?

2) There was no pressed color in android> = 21, what to crack for this?

3) How can I use different colors of action buttons (is this possible)?

Any help would be great.

+7
android android-layout dialog alertdialog android-theme
source share
3 answers

You can use style attributes like

  • buttonBarButtonStyle
  • buttonBarNegativeButtonStyle
  • buttonBarNeutralButtonStyle
  • buttonBarPositiveButtonStyle

Example:

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

Where dialogButtonSelector is our custom selector.

Unfortunately setting the background to dialog_button destroy our paddings and fields, so I need to set it again.

dialog_button style can inherit through Widget.AppCompat.Button.ButtonBar.AlertDialog , but I found that it lacks styles, for example textStyle bold .

+15
source share

I have an answer to the 3rd question
(How can I use different colors of action buttons (is this possible)?)

The code:

 // Initialize AlertDialog & AlertDialog Builder AlertDialog.Builder builder = new AlertDialog.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 :)

+2
source share

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

 builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(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** <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/rounded_rect_yellow" ></item> <item android:state_pressed="false" android:drawable="@drawable/rounded_rect_white" ></item> </selector> 
0
source share

All Articles