Reputation: 3
The xml file:
<EditText
android:drawableLeft="@drawable/icon_lock"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:id="@+id/et_pwd"
android:drawableRight="@drawable/icon_closeeye_32"
style="@style/editText_base"
android:hint="@string/pwd_input"/>
Outside is LinearLayout
The code:
private boolean isTouch = false;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN :
Drawable drawableRight = etPwd.getCompoundDrawables()[2];
if(drawableRight == null && event.getAction() != MotionEvent.ACTION_UP) {
return false;
}
if (event.getX() > etPwd.getWidth()
- etPwd.getPaddingRight()
- drawableRight.getIntrinsicWidth()){
Drawable drawableLeft = getResources().getDrawable(R.drawable.icon_lock);
drawableLeft.setBounds(0, 0, drawableLeft.getMinimumWidth(), drawableLeft.getMinimumHeight());
if (isTouch) {
etPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
Drawable drawableOpenEye = getResources().getDrawable(R.drawable.icon_openeye_32);
drawableOpenEye.setBounds(0, 0, drawableOpenEye.getMinimumWidth(), drawableOpenEye.getMinimumHeight());
etPwd.setCompoundDrawables(drawableLeft, null, drawableOpenEye, null);
} else {
etPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
Drawable drawableCloseEye = getResources().getDrawable(R.drawable.icon_closeeye_32);
drawableCloseEye.setBounds(0, 0, drawableCloseEye.getMinimumWidth(), drawableCloseEye.getMinimumHeight());
etPwd.setCompoundDrawables(drawableLeft, null, drawableCloseEye, null);
}
isTouch = !isTouch;
etPwd.setSelection(etPwd.getText().toString().length());
}
break;
case MotionEvent.ACTION_MOVE :
break;
case MotionEvent.ACTION_UP :
break;
}
return false;
}
And i`ve already implements View.OnTouchListener , find the relevant controls to do the click event . Help me , thanks!
Upvotes: 0
Views: 128
Reputation: 3348
Try this,
XML:
<RelativeLayout
android:id="@+id/relative01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/et_username"
android:background="@drawable/selector"
android:padding="10dp">
<com.mycalculator.utils.CustomEditText
android:id="@+id/et_pwd"
android:layout_width="fill_parent"
android:layout_height="42dp"
android:layout_toStartOf="@+id/iv_show_pwd"
android:background="@null"
android:hint="@string/Password"
android:inputType="textPassword"
android:singleLine="true"
android:textColor="@color/colorBlack"
/>
<ImageView
android:id="@+id/iv_show_pwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginEnd="3dp"
android:layout_marginStart="3dp"
android:src="@drawable/icon_closeeye_32" />
</RelativeLayout>
selector:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners android:radius="6dp" />
<gradient
android:angle="90" />
<solid android:color="@color/colorWhite"/>
<stroke
android:width="1dp"
android:color="@color/Gray" />
</shape>
JAVA:
EditText et_pwd = (EditText) findViewById(R.id.et_pwd);
ImageView iv_show_pwd = (ImageView) findViewById(R.id.iv_show_pwd);
iv_show_pwd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (et_pwd.getTransformationMethod() == PasswordTransformationMethod.getInstance()) {
et_pwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
iv_show_pwd.setImageResource(R.drawable.ic_visible);
} else {
et_pwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
iv_show_pwd.setImageResource(R.drawable.ic_invisible);
}
}
});
Upvotes: 0
Reputation: 903
You need to either change
private boolean isTouch = false;
to true.
private boolean isTouch = true;
Or you need to switch the blocks inside the if
statement around.
The reason for this is because the first time it goes through the codeblock, isTouch is false, and it sets the transformation to password - which it's already in! Now isTouch is set to true, and the second time it goes through the expected code block to show the password.
Upvotes: 1