Tuesday 27 June 2023

How to switch between hide and view password with icon

                                                            

How to show and hide password in login screen or registration screen in android.

So , Here we see  how it works with coding.

Below  is your layout file.

        <RelativeLayout

            android:id="@+id/rel2"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:padding="5dp">

            <EditText

                android:id="@+id/edtpass"

                android:layout_width="match_parent"

                android:layout_height="wrap_content"

                android:hint="Password"

                android:drawablePadding="@dimen/d_10dp"

                android:textSize="15sp"

                android:textColor="@color/pink2"

                android:drawableLeft="@drawable/password"

                android:inputType="textPassword"

                android:paddingRight="@dimen/d_20dp"

                android:layout_centerVertical="true"/>

            <ToggleButton

                android:id="@+id/toggleButtonShowPassword"

                android:layout_width="30dp"

                android:layout_height="30dp"

                android:layout_marginTop="@dimen/d_10dp"

                android:layout_alignParentRight="true"

                android:background="@drawable/passicon"

                android:textOff=""

                android:textOn="" />

        </RelativeLayout>


I make selector for show hide icon in drawable file passicon.xml


<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/passnotshow" android:state_checked="false"></item>

    <item android:drawable="@drawable/passshow" android:state_checked="true"></item>

    <item android:drawable="@drawable/passnotshow"></item>

</selector>



And now from coding  side you have to write below code.


 toggleButtonShowPassword.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override

            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                if (isChecked) {

                    // Show password

                    edtpass.setTransformationMethod(HideReturnsTransformationMethod.getInstance());

                } else {

                    // Hide password

                    edtpass.setTransformationMethod(PasswordTransformationMethod.getInstance());

                }

            }

        });



No comments:

Post a Comment

Comments

Find Hours Diffrence in Kotlin

  In Kotlin, determining the difference in hours between two timestamps is a common task, especially in scenarios involving time-based calcu...