How To Create Custom Dialog In Android With Validation

Let’s learn how to create custom dialog in android and while we are at it, let us also do simple validation of the data the user entered before clicking the OK button! What is an alert dialog in android? It is a small window that asks a user to make a decision or enter more information in an app. You have probably seen a lot of them if you have used android apps in your smartphone. Anyway, the reason why I am showing you how to create custom dialog is because sometimes you may wish to have a different looking dialog box than what comes with your sdk and more importantly, you may wish to validate the data – without using your own custom dialog, it can be tricky …but not anymore! Let us take a look!

how to create custom dialog in android

 

Now, after you enter some crappy data and attempt to submit it, we can perform some validation to make sure that you did the RIGHT thing! You cannot trust everyone online after all! So, it will say something like this if there are errors in your entries and you must fix them before moving on!

How To Create Custom Dialog In Android

That being said, I think it is time to see some code – I agree with you on that!

How To Create Custom Dialog In Android – The Sample Code

Step One

As I mentioned earlier, you have to create your custom xml layout that will contain the Edit Text components with the custom button shown above. This implies that we will not be using the default layout provided by the Dialog class and we then override the functionality which includes the setting of the OK and Cancel actions – this will actually enable us to validate our data whenever a user tries to submit silly information.

name_and_email.xml

NOTE Always remember to create your xmls correctly (do not copy and paste the above code without correcting the component names like button must start with an upper case) – my syntax highlighter sucks and I am planning to look for a better one!
Now, we have created our custom dialog layout that the users will see and the next thing is to create a java class that will do the displaying and validation of the data. Without much ado, here we have it!

CUstomDialogActivity.java

Believe it or not, this will do the job shown above. You can then add your own background image/tiling to pretty this baby up a little more if you want.

You learn how to create a custom dialog in android for several reasons; perhaps to avoid conformity or just because you want to do something that default dialog do not provide – like in our case, validating data before dismissing the window. This also means if the data is wrong, you want to keep the window open so the user can correct the data entered before re-submitting it. The other possible reason could be just being creative and I will tell you something for sure; it goes a long way!

You can add anything you want into your custom xml – that is why it is custom!

So, that being said, I would like to call it a day here! If you have any questions, please do not hesitate to ask me because I would love to hear from you! If you also liked this post, please share it with your friends using the buttons below and drop me some comments. Like I have always tried to say, I would hate to close this post without directing you to the birthplace of ‘How to create custom dialog in android’ – the well written documentation.

Thank you for stopping by and please see you soon!

Be The First To Get New Posts From Simple Developer…

Written By Elisha Chirchir

Elisha Chirchir is a software developer. He is also the founder of Simple Developer and co-founder of Instinctive Software Solutions. On any given day, he works on both Android and Web Development. During his 'free time', he offers training to those interested in learning how to code in php, java, python, javaScript etc. You can easily find him on StackOverflow Android chatroom or on Twitter @Eenvincible

13 Comments on “How To Create Custom Dialog In Android With Validation

  1. […] How To Create Custom Dialog In Android With Validation – Learn how to create custom dialog in android with validation using xml in this simple tutorial with sample code. Add validation to your custom alert dialog… […]

  2. […] How To Create Custom Dialog In Android With Validation – Learn how to create custom dialog in android with validation using xml in this simple tutorial with sample code. Add validation to your custom alert dialog… […]

  3. […] How To Create Custom Dialog In Android With Validation – Learn how to create custom dialog in android with validation using xml in this simple tutorial with sample code. Add validation to your custom alert dialog… […]

  4. […] How To Create Custom Dialog In Android With Validation – Learn how to create custom dialog in android with validation using xml in this simple tutorial with sample code. Add validation to your custom alert dialog… […]

  5. […] How To Create Custom Dialog In Android With Validation – Learn how to create custom dialog in android with validation using xml in this simple tutorial with sample code. Add validation to your custom alert dialog… […]

  6. […] How To Create Custom Dialog In Android With Validation – Learn how to create custom dialog in android with validation using xml in this simple tutorial with sample code. Add validation to your custom alert dialog… […]

  7. […] How To Create Custom Dialog In Android With Validation – Learn how to create custom dialog in android with validation using xml in this simple tutorial with sample code. Add validation to your custom alert dialog… […]

  8. […] How To Create Custom Dialog In Android With Validation – Learn how to create custom dialog in android with validation using xml in this simple tutorial with sample code. Add validation to your custom alert dialog… […]

  9. […] How To Create Custom Dialog In Android With Validation – Learn how to create custom dialog in android with validation using xml in this simple tutorial with sample code. Add validation to your custom alert dialog… […]

  10. Sildio Mbonyumuhire Reply

    December 24, 2015 at 8:43

    Good thank you.
    Can’t we just use a normal dialog OK button to verify the validations?
    Customize the layout but leave OK & Cancel buttons then verify the validations by pressing OK but before OK action is performed.
    I think if there is a best way to make it, it would be better.

    • Elisha Chirchir Reply

      December 28, 2015 at 2:00

      Just try it; just see if you can still set the OK and Cancel buttons on the dialog itself!

  11. anu Reply

    April 19, 2016 at 11:53

    How can we stop the Dialog box from dismissing when the user enters an invalid input, so that he can reenter or correct the input?
    I tried ur example but does not stop and it gets dismissed

    • Elisha Chirchir Reply

      June 2, 2016 at 10:09

      To do that, you can create a custom clickListener then set it to the button you want to listen to; like the Positive or negative button. Let me know if you need more help!

Leave a Reply

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