Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

enter image description here

Style for AlertDialog is this. (For now I am using hardcoded 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) No pressed color is there in android >= 21 , what is hack for this ?

3) How can I have different colors of action buttons (Is it possible)?

Any help would be great.

like image 467
MohK Avatar asked May 14 '15 09:05

MohK


2 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 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 textStyle bold.

like image 71
wrozwad Avatar answered Sep 21 '22 20:09

wrozwad


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.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 :)

like image 34
Venkatesh Selvam Avatar answered Sep 21 '22 20:09

Venkatesh Selvam