I have the following EditText
:
<EditText
android:id="@+id/mPasswordTxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:drawableLeft="@drawable/ic_lock_24dp"
android:ems="10"
android:hint="@string/login_password"
android:inputType="textPassword"
android:paddingLeft="2dp"
android:textColor="@color/white"
android:textColorHint="@color/colorAccent" />
This is a password type field!
I would like to identify the drawable click ( android:drawableLeft
) to change the inputType
!
For this I did the following:
mPasswordTxt.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
final EditText editText = EditText.class.cast(v);
if(event.getAction() == MotionEvent.ACTION_UP) {
final float click_X = event.getRawX();
final float icon_width = editText.getCompoundDrawables()[0].getBounds().width();
final int left = editText.getPaddingLeft()+editText.getLeft();
Log.d("CLICK" , ("click_X:"+click_X));
Log.d("CLICK" , ("icon_width: "+icon_width));
Log.d("CLICK" , ("left:"+left));
}
return false;
}
});
The Log of a click on the icon:
D / CLICK: click_X: 253.65059
D / CLICK: icon_width: 96.0
D / CLICK: left: 8
I think the value of event.getRawX()
is too high (I believe you are counting the beginning of the screen)
How do I find out that the user clicked on drawableLeft
?