establecer fondo transparente para alertar diálogo en android

4 minutos de lectura

Avatar de usuario de Neeha
neha

ingrese la descripción de la imagen aquí

Quiero mostrar un cuadro de diálogo de alerta con fondo transparente. Mi código de diálogo de alerta es:

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();

Mi imagen de fondo contiene esquinas redondeadas. Pero, desafortunadamente, aparece una ventana emergente con un fondo blanco rectangular.

Avatar de usuario de Blackbelt
Cinta negra

definir lo que sigue en el styles.xml expediente

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

y pasarlo como argumento al AlertDialog constructor

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

O programáticamente, a través del Dialog instancia a la que puede llamar

myDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT))

  • thanq blackbelt funcionó. Pero mostrando fondo negro ahora … necesito transparente.

    – Neeha

    24 de abril de 2013 a las 9:43

  • ¿Puedes probar con un Diálogo en lugar de un AlertDialog?

    – Cinta negra

    24 de abril de 2013 a las 10:36

  • Gracias por esta respuesta, trabaja para mí.

    – Sumit Pathak

    8 de septiembre de 2016 a las 7:03

  • Gracias. ¡es impresionante!

    – Islam compartido

    19 de julio de 2018 a las 6:11

  • Bueno, esto funciona para mí, mientras que ContextThemeWrapper no… Es extraño, pero gracias💗~

    – Miau Gato 2012

    22 de julio de 2018 a las 13:43

Avatar de usuario de Sagar Shah
Sagar Shah

Una solución más:

Cuando usas Alertdialog.builder – No está dando getwindow() opción en ella. Entonces podemos hacer que funcione así:

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

Y tu fondo de AlertDialog será transparente.

  • Esta es la forma exacta de hacerlo. Camino a seguir. Gracias.

    – Alex K.

    22 de diciembre de 2014 a las 20:37

avatar de usuario de zbr
zbr

En lugar de esto:

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

podrías intentar hacer algo como esto:

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

  • muestra el error: El método getWindow() no está definido para el tipo AlertDialog.Builder

    – Neeha

    24 de abril de 2013 a las 9:59

  • Sí, lo siento. Por favor, eche un vistazo a la respuesta actualizada, debería funcionar ahora.

    – zbr

    24 de abril de 2013 a las 11:34

Llama esto,

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

justo antes

customDialog.show();

Si solo desea eliminar el fondo blanco del diseño del cuadro de diálogo, simplemente agregue:

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

Si también desea eliminar el color translúcido del fondo de la ventana para el cuadro de diálogo, debe agregar ambas líneas:

dialog.getWindow().setDimAmount(0f);    
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);

Avatar de usuario de Abhijit Chakra
Abhijit Chakra

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);
}

Avatar de usuario de Matías Cánepa
matias canepa

El siguiente código cambia el diseño de fondo.

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")));

¿Ha sido útil esta solución?