Click here to Skip to main content
15,112,453 members
Articles / Mobile Apps / Android
Tip/Trick
Posted 30 Dec 2012

Tagged as

Stats

128.1K views
137 bookmarked

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

Rate me:
Please Sign up or sign in to vote.
4.97/5 (33 votes)
30 Dec 2012CPOL
Show/hide password in a edit text view (password type) .

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. 

XML
<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 

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.

This article was originally posted at http://sdrv.ms/ZMwZdW

License

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

Share

About the Author


Comments and Discussions

 
Questionque Pin
Member 1190207111-Aug-15 7:49
MemberMember 1190207111-Aug-15 7:49 
QuestionRemember password in android Pin
Noor Basha20-Mar-13 21:29
MemberNoor Basha20-Mar-13 21:29 
QuestionRemember password Pin
Noor Basha20-Mar-13 21:28
MemberNoor Basha20-Mar-13 21:28 
GeneralMy vote of 5 Pin
Noor Basha20-Mar-13 21:27
MemberNoor Basha20-Mar-13 21:27 
GeneralMy vote of 5 Pin
uyentran311@yahoo.com10-Jan-13 15:47
Memberuyentran311@yahoo.com10-Jan-13 15:47 
GeneralRe: My vote of 5 Pin
WebMaster10-Jan-13 15:52
MemberWebMaster10-Jan-13 15:52 
GeneralMy vote of 5 Pin
Thong Nguyen883-Jan-13 15:15
MemberThong Nguyen883-Jan-13 15:15 
GeneralMy vote of 5 Pin
Member 97262843-Jan-13 4:56
MemberMember 97262843-Jan-13 4:56 
GeneralMy vote of 5 Pin
WebMaster31-Dec-12 21:31
MemberWebMaster31-Dec-12 21:31 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.