Thursday, December 5, 2019

Password Hide and Show Android

Create your XML using EditText with ImageView.
   <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/ten_view_size"
    android:background="@drawable/border_edittext">

    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_margin="1dp"
        android:layout_weight="1"
        android:background="@color/white"
        android:inputType="textPassword"
        android:padding="@dimen/ten_view_size"
        android:singleLine="true" />

    <ImageView
        android:id="@+id/visibility"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:paddingLeft="@dimen/margin_10"
        android:paddingRight="@dimen/margin_10"
        android:src="@drawable/hide_password_eye"
        android:tag="1" />

</LinearLayout>
use this code for show and hide the password.
 public static void changeVisibility(EditText mPassword, ImageView mIcon) {
    if (mIcon.getTag().toString().equals("0")) {
        mIcon.setImageResource(R.drawable.hide_password_eye);
        mPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        mIcon.setTag("1");
    } else {
        mIcon.setImageResource(R.drawable.show_password_eye);
        mPassword.setInputType(InputType.TYPE_CLASS_TEXT);
        mIcon.setTag("0");
    }
    mPassword.setSelection(mPassword.getText().length());
  }

That is all. If any help related to this post please comment.

Thank you, guys.

Enjoy coding.

No comments:

Post a Comment