Here’s a quick tip to spice up your Android applications. A lot of people perform Android input validation when a button is pressed and display the validation message in a dialog as shown below.
However, there’s a much easier (and elegant way) to do this by using the built in setError construct of the EditText class in Android (note, I’m only talking about EditText in this instance). Using this method, you will get built in support for the EditText **validation control. By using the **setError method you will receive validation as shown below in the Qonqr Android app (Currently in development for release in Q1 of 2012).
To get these free validation controls you simply need to perform validation on the EditText at any time during the runtime execution of your app. This could be after a button is pressed, via a Text Changed Listener or via any other method. Once you determine that your input is invalid simply call the setError method on the EditText instance as shown below.
EditText firstName = (EditText)findViewById(R.id.first_name);
if( firstName.getText().toString().length() == 0 )
firstName.setError( "First name is required!" );
… and BINGO, you now have a free icon show up in the right hand side of the EditText as well as a popup validation message that shows up. As soon as the user starts typing in the EditText, the validation icon and message will disappear. 🙂 Enjoy!
haha says
replica designer bags Good quality
replica designer handbags A good package
air max 2012 A better characters
nike shox turbo Do not miss
men puma shoes The value of a word
air max 90 Very comfortable shoes
wholesale puma shoes Shoes are grade
puma shoes sale I would buy. Fortunately
timberland mens boots I love you ah
gucci women shoes Believe that they
louis vuitton outlet An good words
Coach Factory Outlet says
Coach outlet online Leading American designer and maker of luxury lifestyle handbags and accessories.Large market in Europe and Canada,UK,USA etc.Welcome to Order!2011 New Style arrive,Free Shipping!
oakleys says
YT4M327CJS02QNU
Welcome to Oakley Sunglasses Hut to buy cool and cheap oakleys sunglasses.
oakleys says
TGC5NFJR6PFEWY
Welcome to Oakley Sunglasses Hut to buy cool and cheap oakleys sunglasses.
Andy says
Great Article, I have always wondered how to achieve this. Many thanks for sharing
Andy says
I’ve got a quick question about your EditText’s are they the standard Android ones with no changes? They look much better. What value did you use for the height?
Thanks in advance
Ravindra Bhavsar says
Hello sir Android is new for for i just try Your code to apply validation to EditText in my database application but it not work .when i press button without filling EditText the android emulator shows “Force Quit dialog box”.i dont understand wats going wrong plz help
Klepon Boy14 says
woah!! thanks, this is very helpful for me! 😀
btw i love the book you wrote, it very useful for beginner developer like me 😀
VPS Hosting says
What a great web log. I like the
design and the interface. Because this blog is mind blowing and designing
portion is too strong and the interface is supporting the visitors and user of
this blog and i like this blog to visit.
hosting server says
I get it clear idea about Android Related information through this blog.All the points are explained very clearly.
Samara says
Nice tutor.You have put lots of information in a single blog.Its really very important to us. I would like to read your more updates.This is my first time reading this blog. Definitely won’t be my last though, really enjoyed it.Keep it up.
Munibq1 says
nyc man…thanx alot
Zena says
You have post an informative post regarding Android Related information.Its really an important information. It can be difficult to find good blogs nowadays. Found one here though by the looks of things.
Lipid panel says
I was looking for one because others wouldn’t work well on my iPod Touch 4g. This one is super fast wow lol
Katie says
Great post.I am glad I found this article because i been surfing for something like this.I get it clear idea about your topic.Most of the people looking this kind of valuable tips.
Mike says
Well written article.I appreciate your writing skills.Its great.You have done a great job by sharing this post with us.I like this post & would like to read your more updates in future too.Keep sharing with us in future too.
christian louboutin sale says
NHMYZTDLWM
His boots are coveted over the world, because all women, all over, like to view and sense beautiful.
Bhattjalp says
Simple and Awesome ! It sounds nice 🙂
Lancel says
YJFLLBRMRYLOY I like it very much!
Naved1982 says
For reading more tutorials like this please visit:
http://developersisland.com/viewCMSPages.php
2 hot says
mbt shoes
mbt shoe
mbt shoes uk
mbt sandals
MBT Shoes Mens
MBT Shoes Womens
New Arrival MBT Shoes
MBT Sandals Mens
MBT Sandals Womens
Lancel says
Trouver Sacs à main Lancel bon marché fabriqués en cuir de Lancel
Ravindra Bhavsar says
Do u have complete Source code of above demo.if yes the please forward me.
GSR says
Excellent…
Burberry says
JTGWJFJMTDDZMG I like it very much!
Laxmanrao99 says
thank u
Yuehuosudou Sale says
burberry burberry uk louis vuitton uk louis vuitton christian louboutin christian louboutin uk gucci uk gucci belt chanel uk chanel outlet coach outlet coach outlet canada louis vuitton louis vuitton bags louis vuitton wallet louis vuitton belt tiffany uk tiffanys
Marc Jacobs says
It is rather apparent this Marc Jacobs nothing at all is tricky on the earth, if you ever compose you actually imagination to undertake them, you are going to unquestionably execute a person’s ending. This is so that you can rationale. Despite the fact that to get scholars for instance united states, provide have got a fine attitude Marc Jacobs bags on the way to disaster. Confronting by using challenges plus failures, Marc Jacobs handbags continue to keep leads right up without stop. Furthermore, provide generate 100 % groundwork, thouth strong Marc by Marc Jacobs helps united states prevail over the down sides, it is far from to get individual Marc by Marc Jacobs handbags having virtually no groundwork. .
Amir Deutel says
Great, thanks
Yarout says
I read
your article on blog success through alexa which is
quite interesting.Liked the given ideas of yours a lot.Thanks for
sharing.
Andrea Baccega says
I wrote a class that extends EditText which supports natively some validation methods and is actually very flexible.
You can check it out here: https://github.com/vekexasia/android-form-edittext
Hope you enjoy it 🙂
Donn Felker says
nice work!
raspberry says
Brilliant! Was just looking for a validator! Much appreciated
Hiraki says
nice article ……
Saravanan says
Good blog for beginner
Martin says
Thanks, didn’t know that.
AMIT Dwivedi says
thank you so much for such a nice and simple tutorial…
Ragunath Jawahar says
Nice tutorial, I have written a validation library for Android that uses annotations to enforce constraints on UI widgets. You can also write your own rules if the Annotations or the built-in rules are insufficient. You can check the library from here – https://github.com/ragunathjawahar/android-saripaar
Madre says
Helpful 🙂
Suresh says
nice
Umair says
Wow … I didn’t know this … This blog saved a lot of time for me … Cheers ..
shubh says
thanks buddy…pop box not going automatic when i start enter txt…any idea here?
androidGuy says
Do you know if there is a way to change the text size for the error text?
Balakrishnan says
HI sir
How to display ALL Error message in ONE alert dialog box ?
mj says
Great man. I’ve never seen this in any app.
obi says
Thanks Donn, this helped, may the force be with you.
tituskorir says
simple n very effective thanks 🙂
asmaa mohamed magd helali says
it works with me separately with one text field but when i use it with another one it doesn`t work what can i do?
else if(emailValid()==0&&phoneValid()==0){
text.setError(“Error Email”);
phone.setError(“Error Phone”);
}
tausif says
You can use Toast.makeText(Context context, int resId, int duration)
moon says
Hi sir, how to ensure that the password field match with the confirm password field.?
Supratim says
Had no idea. Thank you Donn Felker. Cheers!
priyansh says
can u tel me button in the above tutorial image…how can i create button like that..
priyansh says
i find it …thanks..to give …new creative idea..
Artem Mihelson says
Hi. I tried to use EditText.setError(“Error”) for EditText that placed within a PopupWindow. I wonder if you know if setError works for EditText in PopupWindow. Because I tried create project from scratch and setError works perfectly within single layout, but app crashes if I use setError on EditText in PopupWindow. Appreciate your answer.
Victor says
Awesome! Thanks!
Eliete says
Hello, I really appreciate your tutorial. It’s very clear and succinct. Could you please help me on another situation using Error messages? I’m using an error message in the follow situation: if I click on button and there isn’t an iformation setted at editText show the error message, so I can’t pass to another fragment, but when I click this button and show the error message and I change the fragment throught drawer item, the error message stay at the new fragment. Do you know how to solve the problem or indicates another tutorial for me please?
ThemuRR says
Hey Donn,
Cheers for showing us this, it’s very handy! Is there any chance you can point me in the right direction when it comes to designing a layout like the one you have shown? I’m finding it very difficult to get a handle on them!
Christian says
i’m trying to put this on my code but didn’t work, once i click on my save button to save that on a SQLite DB it doesn’t works… could you help me please?
Shoaib Murtaza says
String enteredUserName = userName.getText().toString();
String enteredPassword = userPassword.getText().toString();
if( enteredUserName != ” ” && enteredPassword != ” ” ) {
createNewAccount();
}
this if not working, why?????
Benny Margalit says
thanks a lot
Kishore Tangudu says
for name it is not working properly.. without entering data into any field of the form., if you submit, then its stopping application. The error msg is like this “Applicaiton stopped unfortunately” . help me out. [email protected]
Jon Smith says
That is awesome! This was the answer I was looking for! Thanks for a great example of built in validation control, it has made my field level validation look great and work great with one simple code change!
James Buretta says
Thank very much , God bless you Mr DONN FELKER
ankita Singh says
How too add special character validation
Renier de Bruyn says
Thanks for this!
GaryM. says
Have you solved this issue?
Best,
Gary
192.168.1.1
Martin Sloup says
Beware on screen rotation because the error disappear after it. You have to store error message (EditText.getError()) when onSaveInstanceState is called and call setError again after the layout is created. When you use it in AlertDialog with custom view, it’s a little bit tricky to call setError after rotation, because layout is created when dialog is shown. You have to register listener in AlertDialog.setOnShowListener(…) and call it here.
Asif Iqbal says
Sir how can i also bind my mark text field not more than 100 ? please help
best regards 🙂
Eljohn Noli says
how can download source this codes please T_T?
Jones Malama says
This is great Man! saves me time, thanks for the tut