---
title: "Validation is not working and move to next activity in android"  
description: "Validation is not working and move to next activity in android"  
author: "Anonymous User"  
published: 2014-10-19  
updated: 2014-10-19  
canonical: https://www.mindstick.com/forum/2417/validation-is-not-working-and-move-to-next-activity-in-android  
category: "android"  
tags: ["android", "validation"]  
reading_time: 6 minutes  

---

# Validation is not working and move to next activity in android

Hi In My [Application](https://www.mindstick.com/articles/12824/calculator-application-in-android) I have [username and password](https://www.mindstick.com/forum/160407/how-does-a-bearer-token-differ-from-other-authentication-methods-such-as-username-and-password) and login button.click the login [submit button](https://www.mindstick.com/forum/12834/make-text-appear-when-submit-button-clicked-and-checkbox-is-unchecked) without checking validations and empty [fields](https://www.mindstick.com/forum/159126/sql-query-for-all-tables-and-fields-in-an-oracle-db) also moving to next [activity](https://www.mindstick.com/forum/12894/how-to-force-recreation-of-activity-fragment-on-restore).

I want to [check whether](https://www.mindstick.com/forum/157543/write-a-java-program-to-check-whether-a-string-is-a-palindrome) the username is [required](https://www.mindstick.com/forum/160269/what-is-required-data-annotation-how-does-it-affect-model-validation) and password required clicking login button move to next activity.

## Login.java

```
public class Login extends Activity {    Button login;    private static final Pattern USERNAME_PATTERN = Pattern            .compile("[a-zA-Z0-9]{1,250}");    private static final Pattern PASSWORD_PATTERN = Pattern            .compile("[a-zA-Z0-9+_.]{4,16}");    EditText usname,pword;    TextView tv;    String result=null;    HttpPost httppost;    StringBuffer buffer;    HttpResponse response;    HttpClient httpclient;    CheckBox mCbShowPwd;    List<NameValuePair> nameValuePairs;    ProgressDialog dialog = null;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.login);        login = (Button)findViewById(R.id.login);          usname = (EditText)findViewById(R.id.username);        pword= (EditText)findViewById(R.id.password);        mCbShowPwd = (CheckBox) findViewById(R.id.cbShowPwd);       mCbShowPwd.setOnCheckedChangeListener(new OnCheckedChangeListener() {            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {                // checkbox status is changed from uncheck to checked.                if (!isChecked) {                        // show password                    usname.setTransformationMethod(PasswordTransformationMethod.getInstance());                } else {                        // hide password                     pword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());                }            }        });       login.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                final String username = usname.getText().toString();                final String password = pword.getText().toString();                if (username.equals("") || password.equals("")) {                    if (username.equals("")) {                        Toast.makeText(Login.this, "ENTER USERNAME",                                Toast.LENGTH_LONG).show();                    }                    if (password.equals("")) {                        Toast.makeText(Login.this, "ENTER PASSWORD",                                Toast.LENGTH_LONG).show();                    }                } else {                    if (!CheckUsername(username)) {                        Toast.makeText(Login.this, "ENTER VALID USERNAME",                                Toast.LENGTH_LONG).show();                    }                    if (!CheckPassword(password)) {                        Toast.makeText(Login.this, "ENTER VALID PASSWORD",                                Toast.LENGTH_LONG).show();                    }                }                final String queryString = "username=" + username + "&password="                        + password;                String result = DatabaseUtility.executeQueryPhp("login",queryString);                 Intent i = new Intent(getApplicationContext(), Home.class);                    startActivity(i);                        }                      });            }            private boolean CheckPassword(String password) {                return PASSWORD_PATTERN.matcher(password).matches();            }            private boolean CheckUsername(String username) {                return USERNAME_PATTERN.matcher(username).matches();            }
```

## Replies

### Reply by Anonymous User

Use this to check [username](https://www.mindstick.com/forum/12798/there-is-no-viewdata-item-of-type-ienumerable-selectlistitem-that-has-the-key-username) and same way check [password](https://www.mindstick.com/blog/118/retriving-user-password-by-using-stored-procedure-in-asp-dot-net)\

```
public static boolean isValidUsername(EditText ed) {    boolean isValidUsername = true;    if (ed != null) {        Pattern r = Pattern.compile(USERNAME_PATTERN);        String s = ed.getText().toString().trim();        String s1 = s.toString();        Matcher m = r.matcher(s1);        if (m.find()) {            ed.setError("You are allowed to use a-z, A-Z, 0-9, ***") // *** are your                                 other possible symbols            isValidUsername = false;        } else if (s.length() < 5) {            ed.setError("Atleast five characters must be there in username");            isValidUsername = false;        }    }    return isValidUsername;}
```

And now\

```
if(editetxt_username.isValidUsernam() && editetxt_password.isValidPassword()) {   //Change to new activity here}
```

### Reply by Anonymous User

```
login.setOnClickListener(new OnClickListener() {        @Override        public void onClick(View v) {            final String username = usname.getText().toString();            final String password = pword.getText().toString();            if (username.equals("") || password.equals("")) {                if (username.equals("")) {                    Toast.makeText(Login.this, "ENTER USERNAME",                            Toast.LENGTH_LONG).show();                }                if (password.equals("")) {                    Toast.makeText(Login.this, "ENTER PASSWORD",                            Toast.LENGTH_LONG).show();                }            } else if (!CheckUsername(username) && !CheckPassword(password)){                    Toast.makeText(Login.this, "ENTER VALID USERNAME & PASSWORD",                          Toast.LENGTH_LONG).show();            }else{             Intent i = new Intent(getApplicationContext(), Home.class);                startActivity(i);            }         }      });
```


---

Original Source: https://www.mindstick.com/forum/2417/validation-is-not-working-and-move-to-next-activity-in-android

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
