-1

What i have is android app , and in one of activities i have two edit text with button, and i want either if one of them were empty when i click the button to preform a toast to tell the user to enter data, and if it was not empty i want it to open intent and do other thing , here is my code :

add.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(quantity.getText()==null){
                    Toast.makeText(FullImageActivity2.this,"please enter quantity",Toast.LENGTH_LONG).show();
                }
                else if(extra.getText()==null){
                    Toast.makeText(FullImageActivity2.this,"please enter extra",Toast.LENGTH_LONG).show();
                }
                else{
                Quan=quantity.getText().toString();
                name=itemId;
                image=R.drawable.products;
                Intent cart=new Intent(FullImageActivity2.this,CartList.class);
                cart.putExtra("name", name);
                cart.putExtra("quantity",Quan);
                cart.putExtra("image", image);
                Log.e("quan",Quan+"");
                Log.e("name",name+"");
                startActivity(cart);
                }
            }
        });

But the weird thing that if they were empty , else is working !! which is not logic at all .. the validation on empty text is not empty , Why this is happening?? Help plz

sereen
  • 75
  • 12

3 Answers3

2

Try to used .equals() method

if(quantity.getText().toString().trim().equals(""))
M D
  • 47,665
  • 9
  • 93
  • 114
  • 1
    this will work. And put this in a thread which contains a loop. this way, the thread will automatically check for an input again and again – PKlumpp Aug 07 '14 at 10:51
  • http://stackoverflow.com/questions/2601978/how-to-check-if-my-string-is-equal-to-null – nobalG Aug 07 '14 at 10:55
0

Try

if(editText.getText().toString().length()<1){
//do something
}

And of course you are mistaking null with "" which means no string but not null.

Nabin
  • 11,216
  • 8
  • 63
  • 98
0

When Edittext is empty, getText() method returns empty string, not null.
Try:

if(quantity.getText().toString().equals("")){

Of course, if you want to avoid entering only spaces, use

if(quantity.getText().toString().trim().equals("")){
Siim P.
  • 11
  • 3