Android Custom Dialog Boxes

A dialog box is a temporary window an application creates to show any information or retrieve user input. You can use default Dialog Box like Alert Dialog Box or you can customize your own.

A Dialog Box can contain almost anything like TextViews, EditText, ProgressBar, Image Switcher, ImageView, SeekBar or even Fragments with ListView Or GridView. It’s upto you how you want your Dialog Box to perform. It’s provides very beautiful user experience.

In this tutorial we will create 3 custom Dialog Boxes,

1> Login Dialog Box with EditText and Buttons

2> User information Dialog Box with ImageView and TextViews

3> Error Dialog Box with TextViews to show custom error message

 

To create a Custom Dialog Box follow this steps

1> Create an instance of Dialog Box,

 

2> Provide Layout Resource file with setContentView() method,

 

3> To remove Dialog Title bar use this method,

 

4> This method is used to make Dialog Box noncancelable,

 

5> To cancel Dialog Box use this method,

 

To implement this tutorial you need to create 3 XML Layout Resources file apart from activity_main.xml.

Goto res > layout, right click and select New > Layout resource file

Now create 3 xml resource file and name it

1> error_layout.xml

2> image_layout.xml

3> login_layout.xml

 

We are all set now we can jump to our implementation

In your activity_main.xml copy following code,

 

now in your error_layout.xml write following

 

In your image_layout.xml write following,

 

In your login_layout.xml write following,

 

Now in your MainActivity.java write this code,

 

No changes in XML and Gradle file.

I have not done anything with Buttons events and TextViews you can try it to make them more interactive, you can also use SQlite or SharedPreference to create Login Application.

If u have already setup an Emulator or have a Android phone connected in USB Debugging mode just press Alt + Shift +F10 to run app.

Create more interactive Dialogs with more custom View. You can send it your code. I will happy to post it on this site with your name on the post.

Leave a Reply

Your email address will not be published. Required fields are marked *