question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[MaterialAlertDialogBuilder] Rounded corners for AlertDialog pre Android P

See original GitHub issue

Hi, thanks for the great work on Material Components.

Currently I’m facing a problem with MaterialAlertDialogBuilder. I’d like to style my AlertDialogs consistently across Android versions but setting a dialog corner radius is only possible from Android P onwards, since MaterialAlertDialogBuilder is using android.R.attr.dialogCornerRadius.

If the dialogCornerRadius attribute from AppCompat with R.attr.dialogCornerRadius was used instead, it would be possible to define a corner radius for versions < P as well.

Right now, I work around this issue by supplying a custom drawable with rounded corners to the builder in setBackground, which is a bit of a hassle.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

31reactions
SamStennercommented, Dec 20, 2019

@dominikgold So I’ve found a good, backwards compatible solution.

Create the following styles in your styles.xml like so:

<style name="MaterialAlertDialog.Rounded" parent="ThemeOverlay.MaterialComponents.MaterialAlertDialog">
    <item name="shapeAppearanceOverlay">@style/ShapeAppearanceOverlay.MaterialAlertDialog.Rounded</item>
</style>
<style name="ShapeAppearanceOverlay.MaterialAlertDialog.Rounded" parent="">
    <item name="cornerSize">10dp</item>
</style>

When instantiating your MaterialAlertDialogBuilder, pass it the resource ID of the first style, like this:

new MaterialAlertDialogBuilder(getContext(), R.style.MaterialAlertDialog_Rounded)

And now you can easily change the corner radius with the ‘cornerSize’ attribute in the style. Hope this helps!

10reactions
leticiarossicommented, Mar 27, 2020

To have the change not affect the button shapes, you can do as follows:

<style name="ThemeOverlay.MaterialAlertDialog.Rounded" parent="ThemeOverlay.MaterialComponents.MaterialAlertDialog">
    <item name="alertDialogStyle">@style/MaterialAlertDialog.Rounded</item>
</style>

<style name="MaterialAlertDialog.Rounded" parent="MaterialAlertDialog.MaterialComponents">
    <item name="shapeAppearance">@style/ShapeAppearance.MaterialAlertDialog.Rounded</item>
</style>

<style name="ShapeAppearance.MaterialAlertDialog.Rounded" parent="">
    <item name="cornerSize">10dp</item>
</style>

and in your theme set materialAlertDialogTheme to your ThemeOverlay.MaterialAlertDialog.Rounded to change all of your dialogs!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to make custom dialog with rounded corners in android
Create an XML file in drawable, say dialog_bg.xml : <?xml version="1.0" encoding="utf-8"?> ...
Read more >
Dialogs - Material Design
... a dependency to the Material Components for Android library. For more information, go to the Getting started page. MaterialAlertDialogBuilder(context)
Read more >
Android Dialog With Rounded Corners - ADocLib
Step 1 Create a new project in Android Studio go to File New P When user ... [MaterialAlertDialogBuilder] Rounded corners for AlertDialog pre...
Read more >
How to make custom dialog with rounded corners in android?
Step 2 − Add the following code to res/layout/activity_main.xml. <?xml version="1.0" encoding= ...
Read more >
Class Index - Android Developers
Draws an arrow on the edge given the radius of a circle. MaterialAlertDialogBuilder, An extension of AlertDialog.Builder for use with a Material ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found