BasicDialog

Difference between a BasicDialog and an AlertDialog

Dialogs in Android [1] are used to shows alerts for making decisions or to edit a single value [2].
But there are some differences between an AlertDialog and a BasicDialog.
In an AlertDialog you always want to show a message and at least one Button for user interaction.
In a BasicDialog you have a custom view to a TextView or something more complex.

The first working example was quickly done, thanks to the Android Developers Guide [3].
But I was a little bit afraid of getting a warning in Android Studio and in the Lint Report.

Avoid passing null as the view root (needed to resolve layout parameters on the inflated layout’s root element)

Help I’m using a BasicDialog, but what went wrong here?

The solution looked fine and suggests to use the AlertDialog.Builder for a BasicDialog, but I think this example is just outdated and we can do better!
And we can, looking into the API-Reference we find a better example [4].
So lets get started and have a look at the „wrong“ implementation first!

Example: Layout Inflation without a Parent“

I made an working example you can look at Github[5] where the AlertDialog.Builder is used to build with a custom layout.

In the layout we only have the EditText itself.

And why should we change a working solution?

At first I asked me this question, but there are some reasons why we should change this:

  • Make our code more readable
  • Move the styling of the dialog to the layout-file
  • Remove the warning from the linter

The AlertDialog.Builder is removed and we are using the onCreateView to inflate our layout.
I’m using ButterKnife [6] for view injection and OnClick events.

The Button creation is moved to the layout and uses a custom style. In this step I decided to remove the cancel action.

Building the project using Gradle and look at the lint result again and we won’t see any warnings, yay.

Conclusion

Some little changes but we have a great improvement in our source code.
Compared to the old solution, we have a much better cut between the layout and the Java source.
We have moved the labels to the layout and have a method for the OnClick event.
You can grab the source-code for the final version at Github [7].

Summary
Difference between a BasicDialog and an AlertDialog
Article Name
Difference between a BasicDialog and an AlertDialog
Description
When using a DialogFragment there are major differences between an AlertDialog and a BasicDialog. See an complete examples using Gradle and Material Design.
Author