I want to create a custom dialog box like below
I have tried the following things.
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.
Another attempt was to subclass DialogFragment and customize the dialog inside onCreateDialog that but result was not as expected.
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...
How can I achieve that? As I have already tried so many things, a direct answer will be more appreciated.
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???
Dialog Fragment is the simplest way of creating a custom Alert Dialog.Follow the above code to create a custom view for your dialog and then implement it using Dialog Fragment. Add the following code to your layout file:
See Custom Alert Dialog using Dialog Fragment to know how to do this.
It's a class for Alert Dialog so that u can call the class from any activity to reuse the code.
Xml file for the same is:
I found this as the easiest way for showing custom dialog.
You have layout
your_layout.xml
Here I have created a simple Dialog, like:
custom_dialog.xml
You have to
extends Dialog
andimplements OnClickListener
How to Call Dialog ?
Updates
After a long time one of my friends asked me to make a curved shape dialog with a transparent background. So, Here I have implemented it.
To Make a curved shape you need to create a separate
curve_shap.XML
as below,Now, add this
curve_shap.XML
in your main view Layout. In my case I have usedLinearLayout
How to call this ?
I hope that works for you.
Create custom alert layout custom_aler_update.xml
then Copy to Activity
Coy to style
Another easy way to do this.
step 1) create a layout with proper id's.
step 2) use the following code wherever you desire.