Showing posts with label AsteriskPassword in EditText. Show all posts
Showing posts with label AsteriskPassword in EditText. Show all posts

11 Feb 2016

AsteriskPassword in EditText for Android

Hi All,

There are many features or snippet of code, which we are using in our daily development.
Here I am sharing some common basic function for make our life easy and fast in the development.

AsteriskPassword:

In android, By default provide property android:inputType = "textPassword" for Password edit text. But It has minor issue like password character visible while we are typing.
To Restrict this, I have created class AsteriskPasswordUtils. here you can give any password patterns.

AsteriskPasswordUtils.java

import android.text.method.PasswordTransformationMethod;
 import android.view.View;

 public class AsteriskPasswordUtils extends PasswordTransformationMethod
 {
  @Override    
  public CharSequence getTransformation( CharSequence source, View view )
  {
   return new PasswordCharSequence(source);
  }

  private class PasswordCharSequence implements CharSequence
  {
   private CharSequence mSource;

   public PasswordCharSequence( CharSequence source )
   {
    mSource = source; // Store char sequence       
   }

   public char charAt( int index )
   {
    return '*'; // This is the important part        
   }

   public int length()
   {
    return mSource.length(); // Return default        
   }

   public CharSequence subSequence( int start, int end )
   {
    return mSource.subSequence(start, end); // Return default        
   }
  }
 };
MainActivity 
 EditText etPassword = (EditText) findViewById(R.id.etPassword);
 etPassword.setTransformationMethod(new AsteriskPasswordUtils());
Note : Don't forget to add android:inputType = "textPassword" in the edit text on xml.

I will be happy if you will provide your feedback or follow this blog. Any suggestion and help will be appreciated.
Thank you :)