4

i am working on an android application registration page(in java language) where it contains 13 fields. i have done validation to all the fields and its working fine with toast messages. but my requirement is if any field raises a toast message then that field should be highlighted. here is my sample code

                  if (driverName.length() <= 0) {

                        Toast.makeText(ApplicationActivity.this, "Enter first name", Toast.LENGTH_LONG).show();


                    } else if (firname) {
                        Toast.makeText(ApplicationActivity.this, "please enter the first name correctly", Toast.LENGTH_LONG).show();

                    } else if (driverName_last.length() <= 0) {
                        Toast.makeText(ApplicationActivity.this, "Enter last name", Toast.LENGTH_LONG).show();

                    } else if (secname) {
                        Toast.makeText(ApplicationActivity.this, "please enter last name correctly", Toast.LENGTH_LONG).show();

                    } else if (fatherName.length() <= 0) {
                        Toast.makeText(ApplicationActivity.this, "Enter father name", Toast.LENGTH_LONG).show();

                    } else if (fathername) {
                        Toast.makeText(ApplicationActivity.this, "please enter father name correctly", Toast.LENGTH_LONG).show();

                    } 

thanks in advance

Ravi
  • 34,851
  • 21
  • 122
  • 183

3 Answers3

4

You can use setError() method as follows instead of using Toast.

input.setError("Your particular error");

where, input is your EditText.

It will set the error to particular EditText when your if condition will be wrong or according to your given condition with the particular error message.

Its the better way than displaying Toast.

EDITED WITH CODE:

 if (!Common.isValidLength(fName)) {
        medFirstName.setError("Invalid First Name");
    }
    if (!Common.isValidLength(lName)) {
        medLastName.setError("Invalid Last Name");
    }
    if (!Common.isValidEmail(email)) {
        medEmailId.setError("Invalid Email");
    }
    if (!Common.isValidPassword(pass)) {
        medPassword.setError("Invalid Password");
    }
    if (!Common.isValidPassword(confirmPassword)) {
        medConfirmPassword.setError("Invalid Confirm Password");
    }
    if (!Common.isValidMatchPassword(pass, confirmPassword)) {
        medConfirmPassword.setError("Password does not match");
    }

For that create one Common class and put below methods in it :

/*
* A Common function to check internet connection.
* */
public static boolean isOnline(Context c) {
    try {
        ConnectivityManager cm = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnectedOrConnecting()) {
            return true;
        }
        return false;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

/*
* A common function to check length for input.
* */
public static boolean isValidLength(String fName) {
    if (fName.trim().length() > 0) {
        return true;
    }
    return false;
}

/*
* A common function to validate Email id.
* */
public static boolean isValidEmail(String email) {
    String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
            + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";

    Pattern pattern = Pattern.compile(EMAIL_PATTERN);
    Matcher matcher = pattern.matcher(email);
    return matcher.matches();
}

// validating password with retype password
public static boolean isValidPassword(String password) {
    if (password != null) {
        return true;
    }
    return false;
}

// validating of confirm password
public static boolean isValidMatchPassword(String pass, String confirmPassword) {
    if (pass.equals(confirmPassword)) {
        return true;
    }
    return false;
}
user5716019
  • 598
  • 4
  • 17
  • 1
    setError() method is also working good but error message is still viewing if we give correct values for next attempt. so the solution here is to diable the setError() method before calling the function. so is their any way to disable it? –  Jan 07 '16 at 05:37
1

To hightlight field you have to set foucs for e.g.

firname.requestFocus(); 

Note: change firname with your edittext name.

Giru Bhai
  • 14,370
  • 5
  • 46
  • 74
  • if i use firname.requestFocus(); its raising error like cannot resolve method 'requestFocus' –  Jan 07 '16 at 05:18
  • is `firname` or `driverName` edittext for firstName ? please check. – Giru Bhai Jan 07 '16 at 05:19
  • requestFocus() is working good now. but how to highlight that text field with some color (like red). –  Jan 07 '16 at 05:27
  • @Ramkoti check http://stackoverflow.com/questions/23417443/change-border-color-when-edittext-is-focused – Giru Bhai Jan 07 '16 at 05:42
1

requestFocus() method will return focus to view on which it is called.

for example

 else if (firname) {
                        Toast.makeText(ApplicationActivity.this, "please enter the first name correctly", Toast.LENGTH_LONG).show();
firname.requestFoucs(); ****// here firname is edittext****

                    }
CodingRat
  • 1,934
  • 3
  • 23
  • 43