46

I want to show an alert dialog with a transparent background.

My code for the alert dialog is:

AlertDialog.Builder imageDialog = new AlertDialog.Builder(SubProducts.this);
LayoutInflater inflater = (LayoutInflater)SubProducts.this.getSystemService(LAYOUT_INFLATER_SERVICE);

View layout = inflater.inflate(R.layout.cust_toast_layout,(ViewGroup)findViewById(R.id.linearLayout2));

ImageView image = (ImageView)layout.findViewById(R.id.imageView1);
image.setPadding(0, 20, 0, 0);
imgLoader.DisplayImage(image_url, loader, image);

TextView tprice=(TextView)layout.findViewById(R.id.pricetext);
tprice.setText("$ "+pricedouble);

TextView tvdprh=(TextView)layout.findViewById(R.id.textView1);

tvdprh.setText(prohd);

WebView wv=(WebView)layout.findViewById(R.id.webview);

Spanned sub=Html.fromHtml(descp);
String s = "<html><head><style type='text/css' >@font-face {font-family:'myfont';src: url('file:///android_asset/fonts/ABeeZee-Regular.ttf');}body {margin:0px;color:000000;font-family: myfont;"
        + "text-align: justify;}</style></head><body>"
        + sub
        + "</body></html>";

wv.loadDataWithBaseURL("", s, "text/html", "utf-8", null);
wv.setVerticalScrollBarEnabled(true);
wv.setBackgroundColor(Color.TRANSPARENT);
wv.setPadding(5, 25, 5, 0);

ImageView imgcartl=(ImageView)layout.findViewById(R.id.imageView2);
imgcartl.setBackgroundResource(R.drawable.cartlines);

ImageView brobutton=(ImageView)layout.findViewById(R.id.imageView3);
brobutton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

        Intent intentlabl = new Intent(getBaseContext(), Label.class);
Bundle b=new Bundle();
b.putString("url", image_urlpdf);
b.putBoolean("isDialog", true);
intentlabl.putExtras(b);
startActivity(intentlabl);

}
        });

ImageView shobutton=(ImageView)layout.findViewById(R.id.imageView4);

shobutton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
        // TODO Auto-generated method stub
//intent code
        }
        });

ImageView addbutton=(ImageView)layout.findViewById(R.id.imageView5);
addbutton.setBackgroundResource(R.drawable.addicon);
addbutton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
        // TODO Auto-generated method stub

        passingid.add(prodid);

Product prodobj=new Product();
prodobj.setId(passingid);


new LongRunningGetIO4().execute(pricedouble, prodid);
}
        });

imageDialog.setView(layout);

imageDialog.create();
imageDialog.show();

My background image contains rounded corners, but unfortunately, the pop-up is appearing with a rectangular white background. How can I fix this?

enter image description here

Jeff Schaller
  • 2,352
  • 5
  • 23
  • 38
Neeha
  • 727
  • 3
  • 9
  • 21

9 Answers9

105

define what follows in the styles.xml file

<style name="CustomDialog" parent="android:Theme.Dialog">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
</style>

and pass it as argument to the AlertDialog constructor

AlertDialog.Builder imageDialog = new AlertDialog.Builder(SubProducts.this, R.style.CustomDialog);

Or programmatically, through the Dialog instance you can call

myDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT))
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
46

One more solution:

When using Alertdialog.builder - Its not giving getwindow() option in it. So we can make it work like this:

AlertDialog dialog = builderScan.create();
            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
            dialog.show();

And your Background of AlertDialog will be transperent.

Ajeet Choudhary
  • 1,969
  • 1
  • 17
  • 41
Sagar Shah
  • 4,272
  • 2
  • 25
  • 36
9

Instead of this:

imageDialog.create();
imageDialog.show();

you could try doing something like this:

AlertDialog imageDialogAlert = imageDialog.create();
imageDialogAlert.show();
imageDialogAlert.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
zbr
  • 6,860
  • 4
  • 31
  • 43
4

Call this,

customDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

just before

customDialog.show();
Mani
  • 3,394
  • 3
  • 30
  • 36
4

If you just want to remove white background from the dialog layout then you just add:

dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);

If you also want remove window background translucent colour for the dialog then you need to add both lines:

dialog.getWindow().setDimAmount(0f);    
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
DeePanShu
  • 1,236
  • 10
  • 23
2
val dialog: AlertDialog = builder.create()
      dialog.window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        dialog.setView(view)
        dialog.show()```
conrad wawire
  • 194
  • 1
  • 5
  • 1
    Please consider adding some descriptions to your solution. Code only answers are discouraged on Stackoverflow. – Amir Dora. Dec 23 '20 at 15:33
1
protected AlertDialog(Context context) {
    this(context, com.android.internal.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
}

public Builder(Context context) {
    this(context, com.android.internal.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
}
Abhijit Chakra
  • 3,201
  • 37
  • 66
1

The code below changes the background layout.

LayoutInflater inflater = MainActivity.this.getLayoutInflater();

View layout = inflater.inflate(R.layout.your_layout, null);

final AlertDialog alertDio = new AlertDialog.Builder(MainActivity.this)
                        .setView(layout)
                        .show();

alertDio.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.parseColor("#801b5e20")));
Matías Cánepa
  • 5,770
  • 4
  • 57
  • 97
Seymur Mammadli
  • 1,724
  • 15
  • 13
0

First set a custom theme for alert dialog in theme.xml

<style name="YourThemeName" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="colorAccent">#CC8800</item>
    <item name="android:windowBackground">@android:color/transparent</item>

</style>

after this set this theme on your AlertDialog

AlertDialog.Builder alertDialog = new AlertDialog.Builder(requireActivity(),R.style.YourThemeName;
            alertDialog.setView(R.layout.your_custom_view);
            alertDialog.setCancelable(false);
            alertDialog.create();
            alertDialog.show();