Floating Label for EditText

In most Android applications there will be a registration form. For creating a registration form we need to add a label and EditText for every field. Labels are used for showing the user which data the following EditText will accept. And we need to do validations for the required fields. All the things can be achieved using a single UI control. This is known as TextInputLayout, and this can be used with an EditText.

For achieving this we need to add the below two lines in the build.gradle file for the app

compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.android.support:design:23.0.1'


Now bind our views to the Java file like the following,

  1. nameTxt = (EditText) findViewById(R.id.name);  
  2. emailTxt = (EditText) findViewById(R.id.email);  
  3. passwordTxt = (EditText) findViewById(R.id.password);  
  4.   
  5. layoutPassword = (TextInputLayout) findViewById(R.id.layoutPassword);  
  6. layoutEmail = (TextInputLayout) findViewById(R.id.layoutEmail);  
  7. layoutName = (TextInputLayout) findViewById(R.id.layoutName);  
  8.   
  9. Button btn_signup = (Button) findViewById(R.id.btn_signup);  
  10. nameTxt.addTextChangedListener(new MyTextWatcher(nameTxt));  
  11. emailTxt.addTextChangedListener(new MyTextWatcher(emailTxt));  
  12. passwordTxt.addTextChangedListener(new MyTextWatcher(passwordTxt));  
Now create a private class for text watcher, and it will listen for text changes in the EditText field.
  1. private class MyTextWatcher implements TextWatcher  
  2. {  
  3.   
  4.     private View view;  
  5.   
  6.     private MyTextWatcher(View view)   
  7.     {  
  8.         this.view = view;  
  9.     }  
  10.   
  11.     public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}  
  12.   
  13.     public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {}  
  14.   
  15.     public void afterTextChanged(Editable editable)   
  16.     {  
  17.         switch (view.getId())   
  18.         {  
  19.             case R.id.name:  
  20.                 validateName();  
  21.                 break;  
  22.             case R.id.email:  
  23.                 validateEmail();  
  24.                 break;  
  25.             case R.id.password:  
  26.                 validatePassword();  
  27.                 break;  
  28.         }  
  29.     }  
  30. }  
Now create the functions for validating email, name, and password like the following,
  1. private boolean validateName()   
  2. {  
  3.     if (nameTxt.getText().toString().trim().isEmpty())   
  4.     {  
  5.         layoutName.setError(getString(R.string.error_msg_name));  
  6.         requestFocus(nameTxt);  
  7.         return false;  
  8.     } else   
  9.     {  
  10.         layoutName.setErrorEnabled(false);  
  11.     }  
  12.   
  13.     return true;  
  14. }  
  15.   
  16. private boolean validateEmail()   
  17. {  
  18.     String email = emailTxt.getText().toString().trim();  
  19.   
  20.     if (email.isEmpty() || !isValidEmail(email))   
  21.     {  
  22.         layoutEmail.setError(getString(R.string.error_msg_email));  
  23.         requestFocus(emailTxt);  
  24.         return false;  
  25.     } else   
  26.     {  
  27.         layoutEmail.setErrorEnabled(false);  
  28.     }  
  29.   
  30.     return true;  
  31. }  
  32.   
  33. private boolean validatePassword()   
  34. {  
  35.     if (passwordTxt.getText().toString().trim().isEmpty())   
  36.     {  
  37.         layoutPassword.setError(getString(R.string.error_msg_password));  
  38.         requestFocus(layoutPassword);  
  39.         return false;  
  40.     } else   
  41.     {  
  42.         layoutPassword.setErrorEnabled(false);  
  43.     }  
  44.   
  45.     return true;  
Now call the button click for validating them on clicking time,
  1. private void submitForm()  
  2. {  
  3.     if (!validateName())   
  4.     {  
  5.         return;  
  6.     }  
  7.   
  8.     if (!validateEmail())   
  9.     {  
  10.         return;  
  11.     }  
  12.   
  13.     if (!validatePassword())   
  14.     {  
  15.         return;  
  16.     }  
  17.   
  18.     Toast.makeText(getApplicationContext(), "Thank You!", Toast.LENGTH_SHORT).show();  
  19. }  
  20. btn_signup.setOnClickListener(new View.OnClickListener()   
  21. {  
  22.     @Override  
  23.     public void onClick(View view)   
  24.     {  
  25.         submitForm();  
  26.     }  
  27. });  
Please see the screen shots also,

output

output

 

Ebook Download
View all
Learn
View all