I want to change/display different icons for show password in android edittext. I am using following code to display icon.
In your Xml Make a RelativeLayout with TextInputLayout
and Imageview
<RelativeLayout
android:id="@+id/relative"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/relativeone"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:orientation="horizontal"
android:weightSum="5">
<android.support.design.widget.TextInputLayout
android:id="@+id/layoutTextInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_gravity="center_vertical"
android:layout_weight="5"
android:gravity="center_vertical"
android:hint="Password"
android:paddingTop="4dp"
android:textColorHint="#3f3f3f">
<EditText
android:id="@+id/passwordedit"
android:layout_width="match_parent"
android:layout_height="60dp"
android:drawableLeft="@mipmap/bluelocked"
android:drawablePadding="13dp"
android:gravity="center_vertical"
android:inputType="textPassword"
android:paddingLeft="15dp"
android:textColor="#3f3f3f"
android:textColorHint="#3f3f3f"
android:textSize="13sp" />
</android.support.design.widget.TextInputLayout>
<ImageView
android:id="@+id/imagepassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_gravity="center_vertical"
android:paddingBottom="20dp"
android:paddingRight="10dp"
android:src="@mipmap/IMAGEYOUWANT" />
</RelativeLayout>
And in your Activity add onTouch()
method for showing and hidding your password.
ImageView imagepass = (ImageView) findViewById(R.id.imagepassword);
imagepass .setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
editpass.setInputType(InputType.TYPE_CLASS_TEXT);
break;
case MotionEvent.ACTION_UP:
editpass.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
break;
}
return true;
}
});
1.create a xml layout...I have created in my way
<FrameLayout
android:layout_width="match_parent"
android:layout_height="@dimen/_50dp"
android:layout_marginTop="@dimen/_15dp">
<android.support.v7.widget.AppCompatEditText
android:id="@+id/etCurrentPassword"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/drawable_edit_text_2"
android:fontFamily="@font/poppins"
android:hint="@string/old_password"
android:inputType="textPassword"
android:padding="@dimen/_10dp" />
<android.support.v7.widget.AppCompatImageView
android:id="@+id/imgShowPassword1"
android:layout_width="@dimen/_50dp"
android:layout_height="@dimen/_50dp"
android:layout_gravity="end|center_vertical"
android:layout_marginRight="@dimen/_10sp"
android:padding="@dimen/_12dp"
android:tint="@android:color/darker_gray"
app:srcCompat="@drawable/ic_eye" />
</FrameLayout>
2.In activity class: call the method with the mathing parameters
showORhidePass(view,etRetypePassword,showPassword3 = !showPassword3);
void showORhidePass(AppCompatImageView imageView, AppCompatEditText editText, Boolean save){
if (TextUtils.isEmpty(editText.getText())){
return;
}
if (save){
editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
editText.setSelection(editText.length());
imageView.setColorFilter(ContextCompat.getColor(requireContext(), android.R.color.darker_gray));
}else{
editText.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
editText.setSelection(editText.length());
imageView.setColorFilter(ContextCompat.getColor(requireContext(), R.color.colorPrimary));
}
}
Using the Android Material library all you need is -> app:passwordToggleEnabled="true"
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/TextInputLayout_password"
style="@style/TextInputLayoutStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="24dp"
android:hint="Password"
app:passwordToggleEnabled="true">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/password_EditText"
style="@style/EditText_Common_style"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:imeOptions="actionDone"
android:inputType="textPassword"
app:errorEnabled="true" />
</com.google.android.material.textfield.TextInputLayout>