Pages

Saturday, November 8, 2014

Show/Hide Password text in Android (Password type EditText View)


Sometime, we have password field in android appliction. In some special case we want show and hide password. see below simple tutorial demo for that.

1. Create XML file in layout folder "res/layout/activity_hideshowpassword.xml".

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#99afafaf"
        android:gravity="center"
        android:padding="10dp" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:shadowColor="@android:color/black"
            android:shadowDx="0.5"
            android:shadowDy="0.5"
            android:shadowRadius="1"
            android:text="Show/Hide Password"
            android:textColor="@android:color/white"
            android:textSize="22sp"
            android:textStyle="bold" />
    </LinearLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="20dp"
        android:text="Password"
        tools:context=".MainActivity" />

    <EditText
        android:id="@+id/edt_Password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="14dp"
        android:ems="10"
        android:inputType="textPassword" >

        <requestFocus />
    </EditText>

    <CheckBox
        android:id="@+id/chbox_showpassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="8dp"
        android:text="Show Password" />

</LinearLayout>


2. Create on activity java file "ShowHidePasswordActivity.java"

import android.app.Activity;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;

public class ShowHidePasswordActivity extends Activity {

 private EditText edt_password;
 private CheckBox mCbShowPwd;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  
  setContentView(R.layout.activity_hideshowpassword);

  edt_password = (EditText) findViewById(R.id.edt_Password);
  mCbShowPwd = (CheckBox) findViewById(R.id.chbox_showpassword);

  //Add onCheckedListener
  mCbShowPwd.setOnCheckedChangeListener(new OnCheckedChangeListener() {

   public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if (!isChecked) {
     //Show password
     edt_password.setTransformationMethod(PasswordTransformationMethod.getInstance());
    } else {
     //Hide password
     edt_password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
    }
   }
  });
 }
}




3.Add ShowHidePasswordActivity activity class in AndroidManifest.xml file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.limbani"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".ShowHidePasswordActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest> 


4. ScreenShot




Download APK file Here
Enjoy :)
Thank you.