0

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.

rsjaffe
  • 5,600
  • 7
  • 27
  • 39
KoralReef
  • 41
  • 7

3 Answers3

0

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)

MXC
  • 458
  • 1
  • 5
  • 21
0

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.

Pankaj Mundra
  • 1,401
  • 9
  • 25
0

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();
Grygorii
  • 162
  • 2
  • 6