Android Validation with EditText

Here’s a quick tip to spice up your Android applications.

Need Help?

Expert On-Demand Android Consulting is only an email away! Click here for more info.

A lot of people perform Android input validation when a button is pressed and display the validation message in a dialog as shown below.

Validation via Alert Dialog

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).

Qonqr Create Account Validation Example

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!

Android Dev Digest

Get the best Android Developer posts delivered weekly to your inbox.

Don't worry, I wont spam (I hate that stuff too).

  • haha

    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

    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! 

  • http://www.fakeoakleysunglasses4u.net/ oakleys

    YT4M327CJS02QNU
    Welcome to Oakley Sunglasses Hut to buy cool and cheap oakleys sunglasses.

  • http://www.fakeoakleysunglasses4u.net/ oakleys

    TGC5NFJR6PFEWY
    Welcome to Oakley Sunglasses Hut to buy cool and cheap oakleys sunglasses.

  • Andy

    Great Article, I have always wondered how to achieve this. Many thanks for sharing

  • Andy

    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

    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

    woah!! thanks, this is very helpful for me! :D 

    btw i love the book you wrote, it very useful for beginner developer like me :D

  • http://loopbyte.com/ VPS Hosting

    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.

  • http://www.webhostings.in/ hosting server

    I get it clear idea about Android Related information through this blog.All the points are explained very clearly.

  • http://www.seopositiveltd.co.uk/reputation-management.php Samara

    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

    nyc man…thanx alot

  • http://www.mgtdesign.co.uk/ Zena

    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.

  • http://detoxdrugtesting.com/lipid-panel-what-to-know-about-it/ Lipid panel

    I was looking for one because others wouldn’t work well on my iPod Touch 4g. This one is super fast wow lol

  • http://www.seopositiveltd.com/pay-per-click-management/ Katie

    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.

     

  • http://www.websitedesignpositive.co.uk/ Mike

    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.

  • http://www.christianloubotin2012.com/ christian louboutin sale

    NHMYZTDLWM
    His boots are coveted over the world, because all women, all over, like to view and sense beautiful. 

  • Bhattjalp

    Simple and Awesome ! It sounds nice :)

  • http://www.saclancelsoldes.org/ Lancel

    YJFLLBRMRYLOY I like it very much!

  • Naved1982

     For reading more tutorials like this please visit:

    http://developersisland.com/viewCMSPages.php

  • 2 hot

    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

  • http://www.saclancelsoldes.org/ Lancel

    Trouver Sacs à main Lancel bon marché fabriqués en cuir de Lancel 

  • Ravindra Bhavsar

    Do u have complete Source code of above demo.if yes the please  forward me.

  • GSR

    Excellent…

  • http://www.burberryoutlet2012.org/ Burberry

    JTGWJFJMTDDZMG I like it very much!

  • Laxmanrao99

    thank u

  • Yuehuosudou Sale
  • Marc Jacobs

    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. .

  • http://www.facebook.com/amir.deutel Amir Deutel

    Great, thanks

  • http://www.totallyseo.co.uk/ Yarout

    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

    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 :)

  • http://blog.donnfelker.com/ Donn Felker

    nice work!

  • http://www.raspberryketoneplus.net/ raspberry

    Brilliant! Was just looking for a validator! Much appreciated 

  • Hiraki

    nice article ……

  • Saravanan

    Good blog for beginner

  • http://twitter.com/structerer Martin

    Thanks, didn’t know that.

  • AMIT Dwivedi

    thank you so much for such a nice and simple tutorial…

  • Ragunath Jawahar

    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

    Helpful :)

  • Suresh

    nice

  • Umair

    Wow … I didn’t know this … This blog saved a lot of time for me … Cheers ..

  • shubh

    thanks buddy…pop box not going automatic when i start enter txt…any idea here?

  • androidGuy

    Do you know if there is a way to change the text size for the error text?

  • Balakrishnan

    HI sir
    How to display ALL Error message in ONE alert dialog box ?

  • mj

    Great man. I’ve never seen this in any app.

  • obi

    Thanks Donn, this helped, may the force be with you.

  • tituskorir

    simple n very effective thanks :-)

  • asmaa mohamed magd helali

    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

    You can use Toast.makeText(Context context, int resId, int duration)

  • moon

    Hi sir, how to ensure that the password field match with the confirm password field.?

  • Supratim

    Had no idea. Thank you Donn Felker. Cheers!

  • priyansh

    can u tel me button in the above tutorial image…how can i create button like that..

  • priyansh

    i find it …thanks..to give …new creative idea..

  • Artem Mihelson

    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

    Awesome! Thanks!