Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: AlertDialog Button not accepting style

I've got a style set up for my Alert Dialog, and the style is being shown on [most] of the dialog without issue, the only problem are the buttons.

The phone is an HTC Evo running SenseUI, and the AlertDialog buttons continue to be skinned via the SenseUI theme. I have tried changing my application style (rtg_style) to be a child of Theme.Dialog instead of Theme.Light.NoTitleBar, and the buttons for the activities continue to be styled correctly, but the AlertDialogs also continue to be styled inccorrectly. I'm trying to avoid having to write a completely custom AlertDialog replacement, what else can I do?

styles.xml:

<style name="rtg_style" parent="@android:style/Theme.Light.NoTitleBar">
    <item name="android:windowBackground">@drawable/bluebg</item>
    <item name="android:buttonStyle">@style/rtg_Button</item>
    <item name="android:listViewStyle">@style/rtg_ListView</item>
    <item name="android:expandableListViewStyle">@style/rtg_ExpandableListView</item>
</style>

<style name="rtg_AlertDialog" parent="@style/rtg_style"> <!-- parent="@android:style/Theme.Dialog"> --> 
    <item name="android:buttonStyle">@style/rtg_Button</item>
    <item name="android:listViewStyle">@style/rtg_ListView</item>
    <item name="android:alertDialogStyle">@style/dialog</item>
</style>

<style name="rtg_Button" parent="@android:style/Widget.Button">
    <item name="android:background">@drawable/button</item>
    <item name="android:textColor">#ffffff</item>
    <item name="android:textSize">15sp</item>
    <item name="android:textStyle">bold</item>
    <item name="android:height">40dp</item>
</style>

<style name="rtg_ListView" parent="@android:style/Widget.ListView">
    <item name="android:listSelector">@drawable/listview</item>
</style>

<style name="rtg_ExpandableListView" parent="@android:style/Widget.ExpandableListView">
    <item name="android:listSelector">@drawable/listview</item>
</style>    

<style name="base">
    <item name="android:padding">10dp</item>
</style>    

<style name="title" parent="@style/base">
      <item name="android:textColor">#FFFFFF</item>
    <item name="android:textSize">16sp</item>
    <item name="android:textStyle">bold</item>
</style>

<style name="body" parent="@style/base">
    <item name="android:textColor">#000000</item>
    <item name="android:textStyle">normal</item>
</style>

<style name="dialog">
    <item name="android:fullDark">@drawable/dialog_body</item>
    <item name="android:topDark">@drawable/dialog_title</item>
    <item name="android:centerDark">@drawable/dialog_body</item>
    <item name="android:bottomDark">@drawable/dialog_footer</item>
    <item name="android:fullBright">@drawable/dialog_body</item>
    <item name="android:centerBright">@drawable/dialog_body</item>
    <item name="android:bottomBright">@drawable/dialog_footer</item>
    <item name="android:bottomMedium">@drawable/dialog_footer</item>
    <item name="android:centerMedium">@drawable/dialog_body</item>
    <item name="android:windowIsFloating">true</item>

</style>

Activity.java:

AlertDialog.Builder ab = new AlertDialog.Builder(new ContextThemeWrapper(OrderSummary.this, R.style.rtg_AlertDialog));
            ab.setTitle("Select a reason");
            String[] reasons = new String[Shared.Reasons_RejectAll.size()];
            for (int i = 0; i < Shared.Reasons_RejectAll.size(); i++) { 
                try {
                    reasons[i] = Shared.Reasons_RejectAll.get(i).Name;
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            ab.setItems(reasons, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    rejectReason = Shared.Reasons_RejectAll.get(which).Name;
                    for (int i = 0; i <= r.ItemList.length; i++){
                        r.ItemList[index].item.get(i).setStatus(eItemStatus.REJECTED);
                        r.ItemList[index].item.get(i).setRejectReason(rejectReason);
                    }
                    adapter.notifyDataSetChanged();
                }
            }) 
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // No additional code required at this time.
                }
            });
            //ab.show();

            AlertDialog dialog = ab.create();
            dialog.show();
like image 764
autowaaagh Avatar asked Dec 10 '22 02:12

autowaaagh


1 Answers

Something I just tried that works. You can use getButton(int whichButton) in order to extract the Button's View class and then you can butcher it however you want.

Before you do this, call the .show() function, otherwise the Button themselves won't be created yet, and the getButton() command gives you null, and NullPointerExceptions suck. After you get the button, you can do Button/TextView (Button is an extended TextView) functions like .setBackgroundDrawable and .setTextColor.

AlertDialog alert = new AlertDialog.Builder(activity).create();
// configure things here like the title and message
alert.show();

Button button = alert.getButton(AlertDialog.BUTTON_NEGATIVE);
button.setBackgroundResource(R.drawable.negative_button_selector);

If someone can figure out how to set a style/theme outside creating a new Button and swiping that button's stuff, I'm all ears because that would be better.

Rock! Rock On!

like image 89
Joe Plante Avatar answered Jan 03 '23 21:01

Joe Plante