There are two EditText and a submit button with the onClick() method.
When you click on a button in the method, the code for sending data from the fields to the server is triggered.
You need to check the number of characters for the minimum value (6 characters). And check that both EditText fields are not empty.

Here is my code:

 user = (EditText) findViewById(R.id.user); password = (EditText) findViewById(R.id.password); l_user = user.getText().toString(); p_user = password.getText().toString(); if (l_user == "") { setContentView(R.layout.act_login); } else { if (p_user == "") { setContentView(R.layout.act_login); } else { // код отправки данных из форм на сервер 

This method of checking for filling for some reason does not work correctly. If both fields are empty, then everything works, and if you enter a letter in one of the fields, the script starts sending a request. But it should not, because the second field is empty!
The code shows that if at least one of the fields is empty, the screen with the same registration form is loaded, but, as I said, the check does not work well.

How to make a check on the number of characters entered in the EditText field?

  • four
  • @eugeneek however, the author claims that if both fields are empty, the request does not go away. And this seems strange. - Regent
  • why double setContentView (R.layout.act_login) ;? - iFr0z
  • @ iFr0z and why compare the lines with == without good reason? - Regent
  • and in your case "".equals(l_user) , which checks for emptiness will return false , even when l_user == null . - Lex Hobbit

3 answers 3

You do not need to compare strings, it is easier to check the number of characters in a string. Something like this:

 if (l_user.length() > 5 && p_user.length() > 5) { // код отправки данных из форм на сервер } 
  • Thanks, robot! - Vadim

Why convert to strings at all? Everything works fine anyway:

 if (user.getText().length() == 0 || password.getText().length() == 0) { setContentView(R.layout.act_login); } else { // код отправки данных из форм на сервер } 

Less code, easier to read.

  • about restrictions are long, please specify - Lex Hobbit
  • to the minimum allowed value (6 characters) - Lex Hobbit
  • @LexHobbit so it seems so now the answer is? The word "length", by the way, with one "n". - Regent
  • From the question is not entirely clear what to check for at least 6 characters. - Eugene Krivenja
  • @Regent that's right =) it's time to relax ... - Lex Hobbit

Check strings for emptiness as follows:

 if(l_user.equals("")) { //строка пуста } 

Here you have reading on this topic: http://developer.alexanderklimov.ru/android/java/string.php#equals