Check for empty Edittext


Last Updated:

  1. gstein1231

    gstein1231 Member This Topic's Starter

    Joined:
    Dec 21, 2009
    Messages:
    12
    Likes Received:
    0
    What is the best way to see if an edittext field is empty before moving on to another section of code.

    i have a field that requires a number, so I am trying this but having no luck:

    double Aoneone = new Double (input11A.getText().toString());
    if ((Double.toString(Aoneone).equals(" ")))
    {show = new AlertDialog.Builder(mContext).setTitle("Error")
    .setMessage("Some inputs are empty")
    .setPositiveButton("OK", null).show();}
     

    Advertisement
  2. KeithG

    KeithG Well-Known Member

    Joined:
    Nov 15, 2009
    Messages:
    103
    Likes Received:
    3
    Best way to see if an edit field is empty.

    String myText = xxx.getText();

    Check the length of the myText string object? You also should check to make sure that the entry looks like a number before just throwing it at the Double constructor. If it isn't parseable to a number ... the constructor is going to throw an exception. Which you may or may not be catching (the above code doesn't) == app crash.

    Double.toString(fooDouble).equals(" ") ??? How does a double ever equal the string " "?

    R.styleable | Android Developers

    You can change the "input method" for the textView object. If you really need a number (just a number) you could make sure only a valid number can be put into the view in the first place.

    If that doesn't fit within your design, then you can drop back to testing the retrieved text again a regular expression of valid characters. TIMTOWTDI (there is more than one way to do it).

    begin pseudo code :)
    Code (Text):
    1.  
    2. if String.length() > 0 and String_looks_like_a_number(String)
    3.    then:
    4.       ;; we're good continue processing
    5.    else:
    6.       ;; bad input, optionally notify user
    7.       ;; reset bad input
    8.       ;;return to UI thread
    9.  
    The more you tell the UI about the underlying data format the more the UI can help you validate user input. The UI will/may present the user a number input pad vs the full char input method. This saves the user having to switch to the number input setting on the virtual keyboard. << happier user.

    Always expect the data to be invalid. Even if you get a number, and can successfully translate it to a Double/Integer etc, is that number *valid* within the context of your app? .. etc ..

    Unit test, Unit test, Unit test...

    Things that you should keep in mind.

    Exceptions are for exceptional conditions and *generally* should not be used for logic flow. There are certain valid situations where its quite acceptable to use exceptions in this way.

    Exceptions cost more vs (a single) if / then / else. If you are 3 methods deep and need to break back to the top level method, then throw an exception. But if your throwing and catching exceptions inside the same method, you may want to rethink the statement flow.

    The hardware on the phones (current generation) lack FPU's. Doing FP ops is way more costly than Integer math. If you can get away with starting out with a higher base # ... input * 100 or 1000 or 10000... and then do your calculations against integers ... = much nicer to the cpu and battery life.
     
  3. cred05

    cred05 Well-Known Member

    Joined:
    Oct 17, 2008
    Messages:
    178
    Likes Received:
    3
    This is what I'm using.

    if ((txtField.getText().toString().equals("")
    new AlertDialog.Builder(Appname.this).setTitle("Error")
    .setMessage("Inputs Can't be Empty")
    .setPositiveButton("OK", null).show();
    else
    Field = double.parsedouble(txtField.getText().toString());
    do a calculation.

    Problem is, the app still force closes if I leave it empty. If I comment out the else and calculation below it, it gives me the error message just fine. Am I missing something? It's like it ignores the fact that its not suppose to run the else when it finds the IF to be true.
     
  4. cp1

    cp1 Well-Known Member

    Joined:
    Apr 30, 2010
    Messages:
    375
    Likes Received:
    35
    It looks like you need some more parenthesis

    if ((txtField.getText().toString().equals("")))

    If you have more than one statement in your else (or if) you need brackets
    else
    {
    Field = double.parsedouble(txtField.getText().toString());
    do a calculation.
    }


    You also can set the edittext to only accept numeric input, which will help you out.


    It might make KeithG cringe, but you can always do it the lazy way

    try
    {
    Field = double.parsedouble(txtField.getText().toString());
    do a calculation.
    }
    catch (NumberFormatException e)
    {
    new AlertDialog.Builder(Appname.this).setTitle("Error" )
    .setMessage("That's not a number")
    .setPositiveButton("OK", null).show();
    }
     
    Swizz and cred05 like this.
  5. cred05

    cred05 Well-Known Member

    Joined:
    Oct 17, 2008
    Messages:
    178
    Likes Received:
    3
    Sorry, I probably should have added all the syntax, I was just being lazy. I believe that I have all the brackets in place.

    I changed the input type to be decimal/phone which makes it impossible to enter non-numeric text.

    I can give the try/catch a shot. Not sure if that will work with what I have because I actually have 16 edittext fields that I'm checking to see if they are blank, and then using them to do the calculation.
     
  6. phaniraghav@gmail.com

    phaniraghav@gmail.com New Member

    Joined:
    May 5, 2010
    Messages:
    1
    Likes Received:
    0
  7. louieansonng

    louieansonng New Member

    Joined:
    Apr 27, 2011
    Messages:
    2
    Likes Received:
    0
    use string.trim().isEmpty() to check if the string is still empty after removing all whitespaces. .isEmpty() is equivalent to .equals("")
     

Share This Page

Loading...