Material Dialogs

Additional

Language
Kotlin
Version
2.0.0-beta5 (Nov 10, 2018)
Created
Nov 3, 2014
Updated
Nov 11, 2018
Owner
Aidan Follestad (afollestad)
Contributors
Josh Burton (athornz)
pylerSM
egor-n
Leonardo Taehwan Kim (TheFinestArtist)
Tolriq
AEFeinstein
Aidan Follestad (afollestad)
MaciejCiemiega
rndstr
madlymad
Frieder Bluemle (friederbluemle)
emartynov
Balys Valentukevicius (balysv)
jrgonzalezg
blinkseb
Zac Sweers (hzsweers)
Phascinate
ashughes
Show all (76)76
Activity
Badge
Generate
Download
Source code
APK file

Advertisement

If Gradle is unable to resolve, add this to your repositories:

maven { url "https://dl.bintray.com/drummer-aidan/maven/" }

Changes in Version 2

The whole library has been rebuilt, layouts and everything. The library is 100% Kotlin. APIs have changed and a lot of things will be broken if you upgrade from the older version. Other things to note:

  1. This library will be more opinionated. Not every feature request will be implemented.
  2. Minimum API level is 16 (Android Jellybean).
  3. There is no longer a separate Builder class, it's all-in-one.
  4. The whole library was completely re-written in Kotlin. All the layouts and views were remade as well. This library is now designed specifically to work with Kotlin - it technically will work with Java, but not pleasantly.
  5. All main classes exist in the core module, the extension modules take advantage of Kotlin extensions to append functionality to it (such as input dialogs, color dialogs, etc.). This way, you can include only what your app needs.
  6. The use of the neutral button is deprecated to discourage use, see the newer Material guidelines.
  7. There is no longer a progress dialog included in library, since they are discouraged by Google, and discouraged by me. You should prefer a non-blocking inline progress indicator.
  8. No dynamic color support, your dialogs will match your app theme. I will be making sure Aesthetic works correctly with this library if you really need dynamic theming.
  9. Other things will probably be added here.

Basics

Here's a very basic example of creating and showing a dialog:

MaterialDialog(this)
  .title(R.string.your_title)
  .message(R.string.your_message)
  .show()

this should be a Context which is attached to a window, like an Activity.

If you wanted to pass in literal strings instead of string resources:

MaterialDialog(this)
  .title(text = "Your Title")
  .message(text = "Your Message")
  .show()

Note that you can setup a dialog without immediately showing it, as well:

val dialog = MaterialDialog(this)
  .title(R.string.your_title)
  .message(R.string.your_message)
  
dialog.show()

Action Buttons

There are simple methods for adding action buttons:

MaterialDialog(this)
  ...
  .positiveButton(R.string.agree)
  .negativeButton(R.string.disagree)
  .show()

You can use literal strings here as well:

MaterialDialog(this)
  ...
  .positiveButton(text = "Agree")
  .negativeButton(text = "Disagree")
  .show()

Listening for clicks on the buttons is as simple as adding a lambda to the end:

MaterialDialog(this)
  ....positiveButton(R.string.agree) { dialog ->// Do something
  }
  .negativeButton(R.string.disagree) { dialog ->// Do something
  }
  .show()

If action buttons together are too long to fit in the dialog's width, they will be automatically stacked: