How to create a Custom Dialog box in android?

I want to create a custom dialog box like below

enter image description here

I have tried the following things.

  1. I created a subclass of AlertDialog.Builder and used a custom Title and Custom Content View and used that but the result was not as expected.

  2. Another attempt was to subclass DialogFragment and customize the dialog inside onCreateDialog that but result was not as expected.

  3. Then I tried using a plain Dialog class. The result was not as expected.

In all three cases, the problem is when I overlook the title view the size of the dialog is not as expected and when I use Title view the result is there is a thick border around the content view (which really looks bad). Now I have two questions in my mind…

  1. How can I achieve that? As I have already tried so many things, a direct answer will be more appreciated.

  2. What is the best way to show an error or alert dialog in an android app?

EDIT
Android Developer Documentation recommends that we should use either DialogFragments or Dialogs for showing Error / Alert Messages to the user. However at one point they say …

Tip: If you want a custom dialog, you can instead display an Activity as a dialog instead of using the Dialog APIs. Simply create an activity and set its theme to Theme.Holo.Dialog in the manifest element.

What is the meaning of that? Isn’t it too much to use an Activity just for showing an error message???

21 Answers
21

Leave a Comment