Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if EditText has a value in Android / Java [duplicate]

Tags:

java

android

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?

like image 494
Allen Gingrich Avatar asked Aug 04 '10 13:08

Allen Gingrich


3 Answers

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

String yourString = ;
if (marketValHigh.getText().toString().equals(""))
{
    // This should work!
}
like image 199
Sephy Avatar answered Nov 06 '22 09:11

Sephy


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

if (marketValLow.getText().toString().trim().equals(""))
{
}
like image 39
Hare-Krishna Avatar answered Nov 06 '22 11:11

Hare-Krishna


Rather what you can check is like:

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

if (!TextUtils.isEmpty( mSearchText.getText().trim())) {
    // your code
}
like image 2
Amit Lad Avatar answered Nov 06 '22 11:11

Amit Lad