17

This should be simple, but I have tried if statements checking for null values and also ones checking the .length of it:

EditText marketValLow = (EditText) findViewById(R.id.marketValLow);
EditText marketValHigh = (EditText) findViewById(R.id.marketValHigh);
if (marketValLow.getText().length() != 0 && marketValHigh.getText().length() != 0) {
    Intent intent = new Intent();
    intent.setClass(v.getContext(), CurrentlyOwe.class);
    startActivity(intent);
} else {
    Toast.makeText(CurrentMarketValue.this, "You need to enter a high AND low.", Toast.LENGTH_SHORT);
}

But it doesn't detect nothing was entered. Any ideas?

Sender
  • 6,660
  • 12
  • 47
  • 66
Allen Gingrich
  • 5,608
  • 10
  • 45
  • 57

7 Answers7

38

Please compare string value not with == but equals() :

String yourString = ;
if (marketValHigh.getText().toString().equals(""))
{
    // This should work!
}
kenju
  • 5,866
  • 1
  • 41
  • 41
Sephy
  • 50,022
  • 30
  • 123
  • 131
9

This will check if the edit text is empty or not:

if (marketValLow.getText().toString().trim().equals(""))
{
}
Hare-Krishna
  • 1,425
  • 3
  • 16
  • 26
2

Rather what you can check is like:

String text = mSearchText.getText().toString();

if (!TextUtils.isEmpty( mSearchText.getText().trim())) {
    // your code
}
Amit Lad
  • 41
  • 6
0

write a simple function inside a class:

    public static boolean isEditTextEmpty( EditText et){
      String s = et.getText().toString();
      return s.equals("");  // really empty.          
   }

    public static boolean isEditTextInvisible( EditText et){
      String s = et.getText().toString();
      return s.trim().equals("");  // might have only spaces...           
   }

OR... with positive Logic:

    public static boolean hasContentEditText( EditText et){
      String s = et.getText().toString();
      return s != null; // spaces will count.             
   }
Sergio Abreu
  • 2,686
  • 25
  • 20
0

Maybe like this?

String value = editText.getText().toString();
if (value == "")
{
    // your code here
}
Benny Skogberg
  • 10,431
  • 11
  • 53
  • 83
0

If it stops progression to the next activity but doesn't show the toast there must be something wrong in your else statement. Have you read the Dev Guide article on Toast notifications? It is under User Interface -> Notifying the User -> Creating Toast Notifications.

Zhehao Mao
  • 1,789
  • 13
  • 13
0

Validation is somewhat tedious. Maybe I can help you in a more generic manner. I have written a basic framework to validate fields in Android.

It is totally free, and you can do whatever you like with it.

Ragunath Jawahar
  • 19,513
  • 22
  • 110
  • 155
Jaavaaan
  • 122
  • 1
  • 4