-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

5
  • quantity.getText()==null what you do here? Do you know what null actually means? Commented Aug 7, 2014 at 10:50
  • i am new at android .. i don't know what does it mean here probably !! Commented Aug 7, 2014 at 10:51
  • 1
    see stackoverflow.com/questions/2601978/… and do what @ZerO says Commented Aug 7, 2014 at 10:52
  • @sereen well this is first lesson in Java :-/ Commented Aug 7, 2014 at 10:52
  • what does it do with divide with zero? can you remove the unvote please? they gave me an answer .. it worked :/ Commented Aug 7, 2014 at 10:55

3 Answers 3

2

Try to used .equals() method

if(quantity.getText().toString().trim().equals(""))
Sign up to request clarification or add additional context in comments.

2 Comments

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

Comments

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("")){

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.