Before this I used a DialogBuilder
to create AlertDialog
like this
AlertDialog.Builder builder = new AlertDialog.Builder(context);
...
...
AlertDialog dialog = builder.create();
How can I build the new AppCompatDialog
from a dialog builder, or is there another new equivalent way to do that?
Just found the solution. I should import
and then
AppCompatDialog dialog = builder.create()
will work.If you would like to use an
AlertDialog
, just import the newsupprt v 22.1
and use a code like this (pay attention to the import):If
I've just moved all my
android.app.AlertDialog
toandroid.support.v7.app.AlertDialog
.After some testing with 4.X emulators I've found that for a simple dialog it's enough to simply change the import. But for multiple choice dialogs, additionally, you need to do
AppCompatDialog alert = builder.create();
to get the Material Design style dialogs (on 4.X).To be clear, if you have a simple dialog like this one:
Changing the import will suffice :)
But for a multi choice dialog, you need to use
AppCompatDialog
like this:Then you get the nice Material Design look on 4.X devices.
Now the fun part!
For a multi choice dialog, on a 5.X device, the native version (
android.app.AlertDialog
) shows the check-boxes at the left, correctly following the Material Design spec. But if you use the support dialogs, then the check-boxes will appear at the right. WTF!On the long term, as Android 5+ gains market share, you will want to switch back to native dialogs.
android.support.v7.app.AppCompatDialog
is direct parent class ofandroid.support.v7.app.AlertDialog
, wherever you can useandroid.support.v7.app.AlertDialog
, you can useandroid.support.v7.app.AppCompatDialog
.