Android custom alert dialog with rounded corners and transparent background

时光怂恿深爱的人放手 提交于 2019-11-30 16:09:24

I use this and it worked for me:

ConfirmacionMensaje customDialog = new ConfirmacionMensaje(MainActivity.this);
customDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
customDialog.show();

ConfirmacionMensaje exntends from Dialog

and this is my xml for Dialog:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<solid android:color="#ffDB0000"/>
<corners
    android:bottomLeftRadius="4dp"
    android:bottomRightRadius="4dp"
    android:topLeftRadius="4dp"
    android:topRightRadius="4dp" />
</shape>

Do use alert dialog use simple dialog

 LayoutInflater  factory = LayoutInflater.from(getActivity());
            AlertDialog alert = new AlertDialog.Builder(getActivity());

        Dialog  dialog = new Dialog(getActivity());

            dialog.setContentView(your layout);

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

Use this :

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

It is the simplest solution and it works.

This worked for me

dialog.getWindow().setBackgroundDrawable(getResources().getDrawable(R.drawable.background_verification));

background verification is my drawable file

Suresh Chary

This can be solved:

   dialog.getWindow().setBackgroundDrawable(getResources().getDrawable(R.drawable.background_verification));
android developer

are you sure you want to use a dialog? it seems more like a temporary popup, like a toast or a crouton:

about the background, you could use one with 9-patch or a custom xml drawable (example here and here) ...

If your dialog is an instance of either AlertDialog or Dialog add the following to your codes:

myDialog
    .getWindow()
    .setBackgroundDrawable(new ColorDrawable(Color.argb(0,0,0,0)));

Side note: Extending LinearLayout for applying rounded box, in my opinion is not a good practice, You can alternatively do this by the very straightforward XML representation, in this case a XML rectangular shape can help much more :

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    ...
    <corners
        android:radius="3dp" />
    ...
</shape>

this is worked for me,for the first time

dialog.getWindow().setBackgroundDrawable(getResources().getDrawable(R.drawable.background_verification));

here i am getting the resource from drawable folder,background_verification is drawable file

  1. create xml in drawable folder with dialog_corner.

    <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="@color/main_background"/> <corners android:topLeftRadius="@dimen/margin_10" android:topRightRadius="@dimen/margin_10" android:bottomRightRadius="@dimen/margin_10" android:bottomLeftRadius="@dimen/margin_10" /> </shape>

2.put in layout

android:background="@drawable/dialog_corner"

3.in you java file keep below code

View mView =LayoutInflater.from(mContext).inflate(R.layout.layout_pob,null); 
         alertDialog.getWindow().setBackgroundDrawable(new   ColorDrawable(Color.TRANSPARENT));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!