EditText contents validation

empty edittext validation in android example
how to validate multiple edittext in android
different types of edittext in android
registration form validation in android
name validation in android
edittext validation in kotlin
email validation in android-edittext example
number validation in android edittext

I was validating my EditText contents,

I used validating my EditText contents using

String enteredUserName = userName.getText().toString();
String enteredPassword = userPassword.getText().toString();

if( enteredUserName != " " && enteredPassword != " " ) {
    createNewAccount();
}

but above did't work for me, then i see a solution of this,

if( enteredUserName.length() != 0 && enteredPassword.length() != 0 ) {
    createNewAccount();
}

It worked perfectly for me

My question is, why my first code snippet not worked, while i have used above code for validation in java. Thanks

Edit2

setContentView( R.layout.create_account_activity_layout );
btn = ( Button ) findViewById( R.id.create_account_btn );
userName = ( EditText ) findViewById( R.id.name );
userPassword = ( EditText ) findViewById( R.id.password );

enteredUserName = userName.getText().toString().trim();
enteredPassword = userPassword.getText().toString().trim();

userName.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
               if( enteredUserName.trim().length() != 0 && enteredPassword.trim().length() != 0){
                   createNewAccount();
               }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

userPassword.addTextChangedListener(new TextWatcher() {     
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // TODO Auto-generated method stub
         if( enteredUserName.trim().length() != 0 && enteredPassword.trim().length() != 0){
               createNewAccount();
           }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
        // TODO Auto-generated method stub          
    }

    @Override
    public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub  
    }
});

Edit3

String enteredUserName; 
String enteredPassword;

@Override
protected void onCreate( Bundle savedInstanceState ) {
    // TODO Auto-generated method stub
    super.onCreate( savedInstanceState );

    setContentView( R.layout.create_account_activity_layout );
    btn = ( Button ) findViewById( R.id.create_account_btn );
    userName = ( EditText ) findViewById( R.id.name );
    userPassword = ( EditText ) findViewById( R.id.password );

    enteredUserName = userName.getText().toString().trim();
    enteredPassword = userPassword.getText().toString().trim();

    userName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                enteredUserName = userName.getText().toString();
                enteredPassword = userPassword.getText().toString();
                 if( enteredUserName.trim().length() != 0 && enteredPassword.trim().length() != 0){
                       createNewAccount();
                 }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

    userPassword.addTextChangedListener(new TextWatcher() {         
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub
             enteredUserName = userName.getText().toString();
             enteredPassword = userPassword.getText().toString();
             if( enteredUserName.trim().length() != 0 && enteredPassword.trim().length() != 0){
                   createNewAccount();
               }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

        }
    });

    /*while( enteredUserName.toString().isEmpty() || enteredPassword.toString().isEmpty() ) {
        Toast.makeText(CreateAccountActivity.this, "Please enter proper user name and password", Toast.LENGTH_SHORT).show();
        userName = ( EditText ) findViewById( R.id.name );
        userPassword = ( EditText ) findViewById( R.id.password );
        enteredUserName = userName.getText().toString().trim(); 
        enteredPassword = userPassword.getText().toString().trim();
    }*/
    /*  while( userName.getText().toString().equals("")  && userPassword.getText().toString().equals("") ) {
        Toast.makeText(CreateAccountActivity.this, "Please enter proper user name and password", Toast.LENGTH_SHORT).show();        
    }*/
    btn.setOnClickListener( this );
}

Edit4

protected void onCreate( Bundle savedInstanceState ) {
    // TODO Auto-generated method stub
    super.onCreate( savedInstanceState );

    setContentView( R.layout.create_account_activity_layout );
    btn = ( Button ) findViewById( R.id.create_account_btn );
    userName = ( EditText ) findViewById( R.id.name );
    userPassword = ( EditText ) findViewById( R.id.password );

    //enteredUserName = userName.getText().toString().trim();
    //enteredPassword = userPassword.getText().toString().trim();

    TextWatcher textWatcher = new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            String enteredUserName = userName.getText().toString();
            String enteredPassword = userPassword.getText().toString();
            if (enteredUserName.trim().length() != 0 && enteredPassword.trim().length() != 0) {
                Toast.makeText(CreateAccountActivity.this, "test", Toast.LENGTH_LONG).show();
            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,int after) {

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    };
    userName.addTextChangedListener(textWatcher);
    userPassword.addTextChangedListener(textWatcher);

    /*
    userName.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

             enteredUserName = userName.getText().toString();
             enteredPassword = userPassword.getText().toString();
             if( enteredUserName.trim().length() != 0 && enteredPassword.trim().length() != 0){
                   createNewAccount();
             }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

    userPassword.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub
             enteredUserName = userName.getText().toString();
             enteredPassword = userPassword.getText().toString();
             if( enteredUserName.trim().length() != 0 && enteredPassword.trim().length() != 0){
                   createNewAccount();
               }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

        }
    });
    */
    /*while( enteredUserName.toString().isEmpty() || enteredPassword.toString().isEmpty() ) {
        Toast.makeText(CreateAccountActivity.this, "Please enter proper user name and password", Toast.LENGTH_SHORT).show();
        userName = ( EditText ) findViewById( R.id.name );
        userPassword = ( EditText ) findViewById( R.id.password );
        enteredUserName = userName.getText().toString().trim(); 
        enteredPassword = userPassword.getText().toString().trim();
    }*/
    /*  while( userName.getText().toString().equals("")  && userPassword.getText().toString().equals("") ) {
        Toast.makeText(CreateAccountActivity.this, "Please enter proper user name and password", Toast.LENGTH_SHORT).show();
    }*/
    btn.setOnClickListener( this );
}

Your first solution didn't work because you try to compare instance of your text with instance of String which contains only one space.

The second solution will work, but if user will write space the condition will be validated, so you should trim() your string before you check the condition as is shown in code below:

if( enteredUserName.trim().length() != 0 && enteredPassword.trim().length() != 0){
     createNewAccount();
}

the trim() method remove white marks from your String

Or you can use method isEmpty() from String class

If you want to recognise situation where user change text on EditText you should use TextWatcher as is shown in code below:

  editText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
               String enteredUserName = userName.getText().toString();
               String enteredPassword = userPassword.getText().toString();
               if( enteredUserName.trim().length() != 0 && enteredPassword.trim().length() != 0){
                   createNewAccount();
               }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

Add TextWatcher as I shown for userName and userPassword

EDIT

Change your onCreate method to the method below:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.create_account_activity_layout);
    Button btn = (Button) findViewById( R.id.create_account_btn );
    final EditText userName = (EditText) findViewById(R.id.name);
    final EditText userPassword = (EditText) findViewById(R.id.password);
    userName.addTextChangedListener(textWatcher);
    userPassword.addTextChangedListener(textWatcher);
    btn.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String enteredUserName = userName.getText().toString();
            String enteredPassword = userPassword.getText().toString();
            if (enteredUserName.trim().length() != 0 && enteredPassword.trim().length() != 0) {
                createNewAccount();
            }else{
                Toast.makeText(MainActivity2.this, "Fill both fields", Toast.LENGTH_LONG).show();
            }
        }
    });

}

Mini Project, Image credit: http://www.androidhive.info/wp-content/uploads/2015/09/android- I have encountered different forms of validation for text fields (EditText) from  The TextWatcher is used to detect changes in the EditText control — roughly akin to “if the text changes, do something”. I use EditText.setError() (well, the Kotlin equivalent) to alert the UI that there is an issue with the input field. Android renders this as part of the UI with an alert icon and a message box.

To compare Strings You have to do it like this:

  if( !enteredUserName.equals(" ") && !enteredPassword.equals(" ") ) {
      createNewAccount();
 }

For comparing Strings allways use the method equals() or equalsIgnoreCase() if the capital letters are not important

EditText, Contents; Summary. Inherited XML attributes; Inherited constants; Inherited When you define an edit text widget, you must specify the R.styleable. as changes are made, or validate the format of user input, for example. EditText contents validation. Tag: android. i was validating my EditText contents, i used validating my EditText contents using.

You did String comparision by = which is not possible - just use

if(!enteredUserName.equals("") && !enteredPassword.equals("")) 

How to Validate the Edit Text in Android Studio, Validation is a process to ensure that the value entered by the user is within the accepted boundaries of the application. For example, if a user  hi i want to validate edittext fields in my xamarin.android application. My code is. EditText subltn,email; protected override void OnCreate(Bundle savedInstanceState)

Try this

String enteredUserName = userName.getText().toString();
     String enteredPassword = userPassword.getText().toString();

     if( !enteredUserName.equals("") && !enteredPassword.equals("") ) {
          createNewAccount();
     }

In addition

Read this document http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

Other solution

String enteredUserName = userName.getText().toString();
         String enteredPassword = userPassword.getText().toString();

         if( !enteredUserName.isEmpty() && !enteredPassword.isEmpty() ) {
              createNewAccount();
         }

Working with the EditText, By default, any text contents within an EditText control is displayed as plain text. In addition, any validation error messages had to be managed manually by  Android Platform. API level 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1. Manifest.permission. Manifest.permission_group. android.accessibilityservice. AccessibilityService.MagnificationController.OnMagnificationChangedListener. AccessibilityService.SoftKeyboardController.OnShowModeChangedListener.

Try this:

 String enteredUserName = userName.getText().toString().trim();
 String enteredPassword = userPassword.getText().toString().trim();

 if( !enteredUserName.equalsIgnoreCase("")
      && !enteredPassword.equalsIgnoreCase("") )
 {
      createNewAccount();
 }

How to check email Address Validation in Android on edit Text, How to check email Address Validation in Android on edit Text package com.​example.andy.myapplication; import android.content.res. EditText Tutorial With Example In Android Studio: Input Field. In Android, EditText is a standard entry widget in android apps. It is an overlay over TextView that configures itself to be editable. EditText is a subclass of TextView with text editing operations.

Edittext Validation in Android Example, This tutorial show you how to use EditText user interface widget in android and write validation for the inputs. In this example we will create an  Validation Validation is a process to ensure that the value entered by the user is within the accepted boundaries of the application. For example if a user enters a name then the programmer validates the edit text in such a way that it consists of only letters but no numeric values. Step 1 Create a new project.

Text Fields, Contents. Using text fields; Filled text field; Outlined text field; Theming If an EditText is being used, make sure to set its android:background to @null so that  Android Support Library. AndroidX Constraint Layout. Architecture Components. Jetpack Compose UI. Android Automotive Library. Databinding Library. Constraint Layout Library. Material Components. Test Support Library. Wearable Library. Play Billing Library. Play Core Library. Play Install Referrer Library. Kotlin API Reference (Preview)

Text fields, Text fields let users enter and edit text. For text fields that validate their content (​such as passwords), replace helper text with error text when applicable. The method EditTextUtils.isInvalid(…) loops through the supplied validation rules and tests each rule based on the input supplied in the input fields. If they pass, the error marker on the input

Comments
  • You're only checking for a space: " " in your first code, not an empty character, so if you change it to "" without the space in the middle, it should work.
  • Empty string doesn't store as " ". It stores as "".
  • @jyoon you are perfect
  • Another way ...use isempty() method & use String Trim
  • @Programmer Do you try my solution?
  • yes yes i also tried if( userName.getText().toString().equals(" ") && userPassword.getText().toString().equals(" ") ) but it too did't work
  • because you're looking for a space - that won't be in the edittext unless you enter it as username and password. you should trim and check if it is empty or not afterwards
  • actually i was checking either a user enters something in edittext or not, if he enters something then move to next activity
  • want to use in a loop
  • explains better I'm not understanding your question
  • length of String is not important for me, i just want to validate on runtime that either a user enters something in edit text or just move further
  • @Programmer Try the other solutions that I gave to you :)
  • when i use this code, while( enteredUserName.toString().isEmpty() || enteredPassword.toString().isEmpty() ) { Toast.makeText(CreateAccountActivity.this, "Please enter proper user name and password", Toast.LENGTH_SHORT).show(); userName = ( EditText ) findViewById( R.id.name ); userPassword = ( EditText ) findViewById( R.id.password ); } control goes to infinity loop and activity does't displayed
  • its ok for me, but for someone's else may be headache :)
  • @Programmer I know I know... my bad, did you try my logic?