Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing AlertDialog.Builder button color

Im trying to change the button color of AlertDialog.Builder but i didnt find a way to do it.

I want to change the color of the buttons and the title to white like in HOLO theme.

see these 2 screenshots for examples:

enter image description here

enter image description here

Ive looked here:

How to change theme for AlertDialog

Change the style of AlertDialog

How to change the background of the custom alert dialog

Applying Styles Android

All of them are not working for me.

Here is my code:

public void logInDialog()
{
    ContextThemeWrapper ctw = new ContextThemeWrapper( this,  R.style.dialogStyle);
    AlertDialog.Builder builder = new AlertDialog.Builder(ctw);
    builder.setTitle("Log in");
    View prefView = View.inflate(this, R.layout.log_in, null);  
    //The rest of the code.........
}

This is my style code:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="dialogStyle" parent="android:Theme.Dialog">
        <item name="android:background">@color/white</item>
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:button">@color/white</item>
    </style>    
</resources>
like image 340
dasdasd Avatar asked Jul 27 '13 18:07

dasdasd


2 Answers

I know this is a very old question, but I came across with the same problem and I found a solution. In order to change the color of the text inside a button of an Alert dialog you should do something like this:

public void logInDialog()
{
    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));
    }

}

And the xmls for your button used :

custom_background.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:drawable="#000000"/>
    <item android:drawable="@drawable/selectable_item_background"/>

</layer-list>

And selectable_item_background.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/item_pressed" android:state_pressed="true"/>
    <item android:drawable="@drawable/item_focused" android:state_focused="true"/>
    <item android:drawable="@drawable/item_focused" android:state_selected="true"/>
    <item android:drawable="@android:color/transparent"/>

</selector>

I personally used this code inside a Fragment, this is why I have a LayoutInflater. In your case you can skip this step. Hope it helps other people in the future.

like image 86
loumaros Avatar answered Sep 20 '22 21:09

loumaros


To follow up on @Ioumaros's answer, setBackgroundDrawable is now deprecated. You can achieve the same background colour change with this code:

    Button negativeButton = alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE);
    negativeButton.setBackgroundColor(getResources().getColor(R.color.colorBackground));

    Button positiveButton = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
    positiveButton.setBackgroundColor(getResources().getColor(R.color.colorBackground));

But doing this programatically isn't usually considered the best way...

like image 41
Lara Ruffle Coles Avatar answered Sep 20 '22 21:09

Lara Ruffle Coles