Search code examples
androidandroid-layoutdialogandroid-alertdialogandroid-theme

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.


Solution

  • 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.