简体   繁体   中英

AlertDialog button area background color Android

How to change background color for button area in alert dialog. I have done with title background but can't find solution to change color or add divider to button area.

This is how looks now.

在此处输入图片说明

And code

    new AlertDialog.Builder(MainActivity.this,R.style.MyDialogTheme)
            .setCustomTitle(custom_dialog_header)
            .setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int selectedIndex) {
                    selectedItem = selectedIndex;
                }
            })
            .setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int linkID) {
                    String[] files = links.split(",");
                }
            })
            .setNegativeButton("Cancel", null)
            .show();

And here is style

<style name="MyDialogTheme" parent="Theme.AppCompat.DayNight.Dialog.Alert">
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:windowMinWidthMajor">@android:dimen/dialog_min_width_major</item>
    <item name="android:windowMinWidthMinor">@android:dimen/dialog_min_width_minor</item>

    <item name="android:background">#282828</item>
    <item name="android:colorPrimary">#ffffff</item>
    <item name="android:colorAccent">@color/colorSwipe</item>
    <item name="colorAccent">@color/colorSwipe</item>
    <item name="android:fontFamily">@font/roboto</item>
    <item name="android:textSize">16dp</item>
</style>

You need to call create() on builder object instead of show() to create an AlertDialog object. Then, you can change any part of your dialog such as button area using setOnShowListener . this listener is called after your dialog is created, so we use it to avoid nullpointer exception.

final AlertDialog dialog = new AlertDialog.Builder(MainActivity.this,R.style.MyDialogTheme)
        .setCustomTitle(custom_dialog_header)
        .setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int selectedIndex) {
                int selectedItem = selectedIndex;
            }
        })
        .setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int linkID) {
                String[] files = links.split(",");
            }
        })
        .setNegativeButton("Cancel", null)
        .create();
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
    @Override
    public void onShow(DialogInterface d) {
        // to change background of positive button
        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setBackgroundColor(getResources().getColor(R.color.some_color));
        // to change background of button area
        ButtonBarLayout b = (ButtonBarLayout)(dialog.getButton(AlertDialog.BUTTON_POSITIVE).getParent());
        b.setBackgroundColor(getResources().getColor(R.color.some_color));
    }
});
dialog.show();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM