How can I design a custom alert dialog with rounded corners and a transparent dismiss button?

- 30,738
- 21
- 105
- 131

- 607
- 1
- 10
- 23
-
I need only rounded corner and transparent dismiss button,not the whole view. – Braj Bhushan Singh Jan 18 '17 at 12:49
-
asking question need to show minimum effort – Rasel Jan 18 '17 at 12:49
-
@Rasel Can you help me out with this? – Braj Bhushan Singh Jan 18 '17 at 12:51
-
First Google it..you can find. Don't post your question directly. http://stackoverflow.com/questions/28937106/how-to-make-custom-dialog-with-rounded-corners-in-android – Vishal Jadav Jan 18 '17 at 12:52
-
Possible duplicate of [How to implement a custom AlertDialog View](http://stackoverflow.com/questions/2795300/how-to-implement-a-custom-alertdialog-view) – Braj Bhushan Singh Jan 22 '17 at 08:20
5 Answers
Create your dialog like this:
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context, R.style.CustomAlertDialog);
AlertDialog alertDialog = alertDialogBuilder.create();
In your styles.xml file:
<style name="CustomAlertDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:windowBackground">@drawable/popup_background</item>
</style>
In file popup_background.xml, write whatever corner radius you want:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF" />
<corners android:radius="6dp" />
</shape>
You can change the corner radius.

- 30,738
- 21
- 105
- 131

- 944
- 1
- 9
- 20
-
-
1https://stackoverflow.com/questions/13929877/how-to-make-gradient-background-in-android The answer to this question should help you, change the popup_background into a gradient shape. – Belzebub May 19 '20 at 18:14
-
This one pretty good solution but one issue I face dialog width changed as compare to original alert dialog. you have any solution for same. – Kishan Donga Jun 09 '20 at 11:09
You can use the Material components for Android library and the androidx.appcompat.app.AlertDialog
.
Just use something like:
new MaterialAlertDialogBuilder(context)
.setTitle("Dialog")
.setMessage("Lorem ipsum dolor ....")
.setPositiveButton("Ok", /* listener = */ null)
.setNegativeButton("Cancel", /* listener = */ null)
.show();
Using a Material Components Theme you can customize the shape of your component with the shapeAppearanceOverlay
attribute in your style.
Something like:
<!-- Alert Dialog -->
<style name="MyThemeOverlayAlertDialog" parent="@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
<item name="shapeAppearanceOverlay">@style/ShapeAppearanceOverlay.MyApp.Dialog.Rounded</item>
</style>
Here you can define the rounded corners:
<style name="ShapeAppearanceOverlay.MyApp.Dialog.Rounded" parent="">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">8dp</item>
</style>

- 30,738
- 21
- 105
- 131

- 320,139
- 94
- 887
- 841
-
2Works! The best alternative I could find. Setting another drawable modifies the size of the dialog box, so it is not an option – Nicolás Vera Dec 18 '19 at 07:56
-
1What a great answer, this suppose to be the correct answer, I agree with @NicolásVera – MusabAlothman Aug 30 '22 at 08:26
You can create your customview by extending the alert dialog class.
But I would recommend a PopupWindow or a subview that you show with an animation when a specific action performed.
Or you can make an activity with a transparent background by adding this attribute to your Manifest.xml file:
android:theme="@android:style/Theme.Translucent"

- 30,738
- 21
- 105
- 131

- 932
- 7
- 9
Try this. It worked for me like a charm.
I am working on sdkversion 28 with a minimum SDK version 19.
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

- 30,738
- 21
- 105
- 131

- 342
- 5
- 13
-
OP asked for rounded corners *and* transparent background. Do you have any suggestion for the corners? – Bö macht Blau Jan 25 '19 at 18:49
Try it...
final Dialog dialog = new Dialog(context);
// Include the dialog.xml file
dialog.setContentView(R.layout.your_custom_layout);
// Set the dialog title
//dialog.setTitle("Custom Dialog");
// Set values for custom dialog components -
// text, image and button
final EditText name = (EditText) dialog.findViewById(R.id.name_edit);
dialog.show();
/
Button editButton = (Button) dialog.findViewById(R.id.editbtn);
// If the decline button is clicked, close the custom dialog
editButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Close dialog
dialog.dismiss();
}
});
final Button cancenbtn = (Button) dialog.findViewById(R.id.cancelbtn);
// If the decline button is clicked, close the custom dialog
cancelnbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Close dialog
dialog.dismiss();
}
});

- 30,738
- 21
- 105
- 131

- 105
- 3