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(;
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. :)


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! 

  • oakleys

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

  • oakleys

    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! 😀 

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

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

  • hosting server

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

  • 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

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

  • 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

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


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

  • christian louboutin sale

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

  • Lancel

    YJFLLBRMRYLOY I like it very much!

  • Naved1982

     For reading more tutorials like this please visit:

  • 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

  • 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


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

  • Amir Deutel

    Great, thanks

  • Yarout

    I read
    your article on blog success through alexa which is
     quite interesting.Liked the given ideas of yours a lot.Thanks for

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

    Hope you enjoy it :)

  • Donn Felker

    nice work!

  • raspberry

    Brilliant! Was just looking for a validator! Much appreciated 

  • Hiraki

    nice article ……

  • Saravanan

    Good blog for beginner

  • 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 –

  • Madre

    Helpful :)

  • Suresh


  • 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 … 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!

  • Eliete

    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

    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

    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

    String enteredUserName = userName.getText().toString();
    String enteredPassword = userPassword.getText().toString();

    if( enteredUserName != ” ” && enteredPassword != ” ” ) {

    this if not working, why?????

  • Benny Margalit

    thanks a lot

  • Kishore Tangudu

    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.

  • Jon Smith

    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

    Thank very much , God bless you Mr DONN FELKER

  • ankita Singh

    How too add special character validation