1
I got 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 click on the drawable ( android:drawableLeft
) to change the inputType
!
For that 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;
}
});
One click Log 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 very high (I believe I am counting the beginning of the screen)
How do I find out that the user clicked on the drawableLeft
?
Dude, I was able to use the getx() instead of getRawX()!
– Thiago Luiz Domacoski
@Thiagoluizdomacoski you even tested this way as I did?! I will check here the accuracy of the two methods.
– viana
Yes, yes! In an example with the rawX he returns me 103.94634 With the x returned me 23.946342 Being the icon_width: 36.0 + 3 of left
– Thiago Luiz Domacoski
I’ve seen several examples that use the getRawX, I even found it strange... I did the tests on 3 different screens and good funfo!
– Thiago Luiz Domacoski
@Thiagoluizdomacoski cool. You can put an answer, or even edit my adding your experience if you prefer. Good luck there.
– viana
Vo do, I’ll just test some more! Thanks for the help Bro!!!
– Thiago Luiz Domacoski