2

I have created a custom AlertDialog with rounded corners using onDraw of LinearLayout as below,

public class RoundedLinearLayout extends LinearLayout {

private Paint drawPaint;
private Paint roundPaint;

private int mCornerRadius = 100;

private RectF bounds;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public RoundedLinearLayout(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    onInit();
}

public RoundedLinearLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    onInit();
}

public RoundedLinearLayout(Context context) {
    super(context);
    onInit();
}

protected void onInit() {
    drawPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    drawPaint.setColor(0xffffffff);
    drawPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

    roundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    roundPaint.setColor(0xffffffff);

    setWillNotDraw(false);
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    if (w != oldw && h != oldh) {
        bounds = new RectF(0, 0, w, h);
    }
}

@Override
protected void dispatchDraw(Canvas canvas) {
    Bitmap bitmap = Bitmap.createBitmap((int) bounds.width(), (int) bounds.height(), Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(bitmap);
    super.dispatchDraw(c);

    BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);

    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setShader(shader);

    canvas.drawRoundRect(bounds, mCornerRadius, mCornerRadius, paint);
}
}

And then I added transparency by getWindow() and setting window.alpha = 0.5f . The resulting dialog is,

Custom alert with transparency

I want to remove those corner white background. I have searched 100s of questions here and no answer could get me the perfect rounded corner alert dialog. Any help would be appreciated!

Bharath Mg
  • 1,117
  • 1
  • 9
  • 18
  • Possible duplicate of [Android Dialog - Rounded Corners and Transparency](https://stackoverflow.com/questions/16861310/android-dialog-rounded-corners-and-transparency) – Vega Sep 06 '19 at 12:18

9 Answers9

9

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>
XCDreams
  • 91
  • 1
  • 3
6

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));
Ammar ali
  • 1,503
  • 1
  • 15
  • 24
  • According to [Dialogs](https://developer.android.com/guide/topics/ui/dialogs.html): "The Dialog class is the base class for dialogs, but you should avoid instantiating Dialog directly."This might resolve the transparent corner issue, but for me it introduced other problems. – Mark Cramer Jul 11 '16 at 22:25
  • I tried over a dozen different things, and this is indeed the only one that worked. You have to use a simple `Dialog`, regardless of what the docs say. – Mark Cramer Jul 12 '16 at 02:59
6

Use this :

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

It is the simplest solution and it works.

Umang Mathur
  • 833
  • 1
  • 10
  • 32
2

This worked for me

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

background verification is my drawable file

Ramesh Bhupathi
  • 408
  • 7
  • 10
2

This can be solved:

   dialog.getWindow().setBackgroundDrawable(getResources().getDrawable(R.drawable.background_verification));
enb081
  • 3,831
  • 11
  • 43
  • 66
1

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) ...

Community
  • 1
  • 1
android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Lee Taylor Jul 23 '14 at 12:25
  • @LeeTaylor It's a lot of code to copy to here. Sorry. – android developer Jul 28 '14 at 13:53
1

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>
frogatto
  • 28,539
  • 11
  • 83
  • 129
1

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

0
  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));
siddhartha shankar
  • 1,450
  • 13
  • 16