Click here to Skip to main content
Click here to Skip to main content

Show/hide password in a edit text view (password type).

By , 30 Dec 2012
 

Introduction 

Sometime, in your Android application activity, you want to have a password field that can be show/hide its content. So, with this code snippet, you can achieve quickly and correctly.

Build a simple demo

In this simple demo, there is a password text field. Normally, when user enter text in this field, all characters will be showed like this : *****. To achieve our purpose, I will add a toogle-checkbox. By default, this checkbox is un-checked status. So, the password field be showed in its original format. When user tap on checkbox, the password will be showed it content in readable format. Tap again on checkbox, the password field go back its original format ( only show * characters).

 

                            Hide the password 

Show the password 

Using the code

Main layout : main.xml 
In this layout, I defined an EditText field (password type) and a checkbox field. When user clicks on this checkbox, it will show/hide the password content. 

<RelativeLayout 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" >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="23dp"
        android:layout_marginTop="86dp"
        android:text="@string/hello_world"
        tools:context=".MainActivity" />
    <EditText
        android:id="@+id/etPassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="21dp"
        android:layout_marginTop="14dp"
        android:ems="10"
        android:inputType="textPassword" >
        <requestFocus />
    </EditText>
    <CheckBox
        android:id="@+id/cbShowPwd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/etPassword"
        android:layout_below="@+id/etPassword"
        android:text="@string/show_pwd" />
</RelativeLayout> 

Code of activity :  MainActivity.java 

package vn.com.enclaveit.phatbeo.android.demo;

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;

// @author : Phat (Phillip) H. VU <vuhongphat@hotmail.com>
public class MainActivity extends Activity {

    EditText mEtPwd;
    CheckBox mCbShowPwd;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // get the password EditText
        mEtPwd = (EditText) findViewById(R.id.etPassword);
        // get the show/hide password Checkbox
        mCbShowPwd = (CheckBox) findViewById(R.id.cbShowPwd);
        
        // add onCheckedListener on checkbox
        // when user clicks on this checkbox, this is the handler.
        mCbShowPwd.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // checkbox status is changed from uncheck to checked.
                if (!isChecked) {
                        // show password
                	mEtPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                } else {
                        // hide password
                	mEtPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }
            }
        });
    }
} 

History

First release on Dec 31, 2012.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Phat (Phillip) H. VU
Unknown
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionRemember password in androidmemberNoor Basha20 Mar '13 - 20:29 
QuestionRemember passwordmemberNoor Basha20 Mar '13 - 20:28 
GeneralMy vote of 5memberNoor Basha20 Mar '13 - 20:27 
GeneralMy vote of 5memberuyentran311@yahoo.com10 Jan '13 - 14:47 
GeneralRe: My vote of 5memberPhat (Phillip) H. VU10 Jan '13 - 14:52 
GeneralMy vote of 5memberThong Nguyen883 Jan '13 - 14:15 
GeneralMy vote of 5memberMember 97262843 Jan '13 - 3:56 
GeneralMy vote of 5memberPhat (Phillip) H. VU31 Dec '12 - 20:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130513.1 | Last Updated 31 Dec 2012
Article Copyright 2012 by Phat (Phillip) H. VU
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid