Zohaib
Zohaib

Reputation: 41

Android validation with edit text is not working?

I'm new to android and making and app which required Edit Text and it's validation. I have tried different ways to validate the edit text but is is not working. Please some help me in this, Thanks.

Here is the code I'm using:

    EditText age = (EditText)findViewById(R.id.age);
    EditText weight= (EditText)findViewById(R.id.weight);
    EditText height= (EditText)findViewById(R.id.height);


    Button calculate = (Button) findViewById(R.id.btn_cal);

 calculate.setOnClickListener(new View.OnClickListener() {
         @Override
        public void onClick(View view) {

             final String a=age.getText().toString();
             int aN=Integer.parseInt(a);
             final String w=weight.getText().toString();
             int wN=Integer.parseInt(w);
             final String h=height.getText().toString();
             int hN=Integer.parseInt(h);

             if(a.length()==0){
                 age.requestFocus();
                 age.setError("Between 13 ans 80");
             }
             if(w.length()==0){
                 weight.requestFocus();
                 weight.setError("Weight required!");
             }
             if(h.length()==0){
                 height.requestFocus();
                 height.setError("Height required!");
             }
            }
          });

Upvotes: 0

Views: 599

Answers (3)

MilapTank
MilapTank

Reputation: 10076

try your code like this

 EditText age = (EditText)findViewById(R.id.age);
 EditText weight= (EditText)findViewById(R.id.weight);
 EditText height= (EditText)findViewById(R.id.height);

 age.setInputType(InputType.TYPE_CLASS_NUMBER);
 weight.setInputType(InputType.TYPE_CLASS_NUMBER);
 height.setInputType(InputType.TYPE_CLASS_NUMBER);
 Button calculate = (Button) findViewById(R.id.btn_cal);

  calculate.setOnClickListener(new View.OnClickListener() {
     @Override
    public void onClick(View view) {

         String a=age.getText().toString().trim();
         String w=weight.getText().toString().trim();
         String h=height.getText().toString().trim();

         if(TextUtils.isEmpty(a)){
           age.requestFocus();
           age.setError("Between 13 ans 80");
         }

        if(TextUtils.isEmpty(w)){
             weight.requestFocus();
             weight.setError("Weight required!");
         }
         if(TextUtils.isEmpty(h)){
             height.requestFocus();
             height.setError("Height required!");
         }
         int aN=Integer.parseInt(a);
         int wN=Integer.parseInt(w);
         int hN=Integer.parseInt(h);
        }
      });

DOC :TextUtils.isEmpty()

Upvotes: 0

kgandroid
kgandroid

Reputation: 5595

Try this:

calculate.setOnClickListener(new View.OnClickListener() {
         @Override
        public void onClick(View view) {


             if(age.length()==0){
                 age.requestFocus();
                 age.setError("Between 13 ans 80");
             }
             else if(weight.length()==0){
                 weight.requestFocus();
                 weight.setError("Weight required!");
             }
             else if(height.length()==0){
                 height.requestFocus();
                 height.setError("Height required!");
             }

         else{
              a=age.getText().toString();
              aN=Integer.parseInt(a);
              w=weight.getText().toString();
              wN=Integer.parseInt(w);
              h=height.getText().toString();
             hN=Integer.parseInt(h);

             }
            }
          });

With your initial code it will give a NumberFormat Exception if the editTexts are left blank.I dont know what is a,w,h in your code but i guess you mistakenly assumed your edittexts as a,w,h which is evidently age,weight and height respectively.

UPDATE

Declare the Strings and the integer variables ASSOCIATED WITH THIS CODE globally i.e. before onCreate().Like String a,w,h and int aN,wN,hN.

Upvotes: 1

PedroHawk
PedroHawk

Reputation: 630

Try something like this:

         if(TextUtils.isEmpty(a)){
             age.requestFocus();
             age.setError("Between 13 ans 80");
         }

Hope it helps

Upvotes: 1

Related Questions