AlertDialog change background color in a fragment

Good day, how can I change the background of my AlertDialog in fragment?

My AlertDialog will show when I click the button (fragment->button->alertdialog).

I tried the following code in implementing my AlertDialog and changing its background color:

AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle(R.string.scaleTitle);
builder.setView(R.layout.scale_layout);
builder.setNegativeButton("Close", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialogInterface, int i) {
        dialogInterface.dismiss();
    }
});
AlertDialog alertDialog = builder.create();
builder.show();
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.YELLOW));  

but the background is still the same.

Please, how can I possibly change it? Thank you.

3 answers

  • answered 2018-08-09 00:45 Abhishek Sharma

    Define a style like this in your res-->values-->styles.xml

    <style name="CustomAlertDialogTheme" parent="Theme.AppCompat.Dialog.Alert">
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:colorBackground">@color/dialogColor</item>
        <item name="android:windowBackground">@color/dialogWindowColor</item>
    </style>
    

    And create the builder by using above theme resource.

    AlertDialog.Builder builder = new AlertDialog.Builder(getContext(), R.style. 
    CustomAlertDialogTheme);
    

    https://developer.android.com/reference/android/app/AlertDialog.Builder.html#AlertDialog.Builder(android.content.Context,%20int)

  • answered 2018-08-09 05:08 Pankaj Mundra

    You can try the below code for AlertDialog with Yellow color.

    In Java:

    View view = LayoutInflater.from(this).inflate(R.layout.dialog, null);
            View customTitleView = LayoutInflater.from(this).inflate(R.layout.title, null);
            AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.AlertDialogCustom);
            builder.setView(view);
            builder.setCustomTitle(customTitleView);
            builder.setNegativeButton("Close", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    dialogInterface.dismiss();
                }
            });
            AlertDialog alertDialog = builder.create();
            alertDialog.show();
    

    In style.xml:

     <style name="AlertDialogCustom" parent="Theme.AppCompat.Dialog.Alert">
            <item name="android:colorBackground">@color/yellow</item>
            <item name="android:windowBackground">@color/yellow</item>
        </style>
    

    In colors.xml:

    <color name="yellow">#FFFF00</color>
    

    Create below layout XML:

    title.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Dialog"
            android:textColor="@android:color/black"
            android:textSize="20sp" />
    </LinearLayout>
    

    dialog.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        Create a custom view with textview, edittext or whatever you want.
    
    </LinearLayout>
    

    Your output will be like below image:

    enter image description here

    I hope this is what you want to achieve, if you have any queries please let me know.

  • answered 2018-08-09 15:43 Grygorii

    Try this:

    AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
    builder.setTitle(R.string.scaleTitle);
    builder.setView(R.layout.scale_layout);
    builder.setNegativeButton("Close", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            dialogInterface.dismiss();
        }
    });
    final AlertDialog alertDialog = builder.create();
    alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
                    @Override
                    public void onShow(DialogInterface arg0) {
                    alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.YELLOW));
                    }
    });
    alertDialog.show();