How to identify the click on a drawableLeft

Asked

Viewed 117 times

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?

2 answers

2

I did some tests here and it worked great for the DawableLeft. The proposed calculation is to verify that the coordinate of the clicked event is less than or equal to the size of the left space plus the size of the icon defined. Basically that’s the condition: if(click_X <= (left + icon_width). See the onTouchListener down below:

editText.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) {

                //Retorna a coordenada X original do evento 
                float click_X = event.getRawX(); 
                // tamanho do dawable left / icone
                float icon_width = editText.getCompoundDrawables()[0].getBounds().width();  
                int left = editText.getPaddingLeft()+editText.getLeft();

                if(click_X <= (left + icon_width)) {
                    // Aqui será a área clicada correspondente ao dawable left / icone
                    Toast.makeText(getApplicationContext(), "Clicou no icone",Toast.LENGTH_LONG).show();
                    return true;
                }
            }
            return false;
        }
    });
  • Dude, I was able to use the getx() instead of getRawX()!

  • @Thiagoluizdomacoski you even tested this way as I did?! I will check here the accuracy of the two methods.

  • 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

  • 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!

  • 1

    @Thiagoluizdomacoski cool. You can put an answer, or even edit my adding your experience if you prefer. Good luck there.

  • 1

    Vo do, I’ll just test some more! Thanks for the help Bro!!!

Show 1 more comment

0

I was able to detect the click on DawableLeft using the getX(), instead of getRawX()

Follows the implementation:

   passwordText.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 _x = event.getX();
                final float icon_width = editText.getCompoundDrawables()[0].getBounds().width();
                final int left = editText.getPaddingLeft()+editText.getLeft();
                if(_x <= (left + icon_width) ){
                    Drawable _Drawable = null;
                    if(editText.getInputType() == 129){
                        editText.setInputType(1);
                        _Drawable = context.getResources().getDrawable(R.drawable.ic_lock_open_24dp);
                    }else{
                        editText.setInputType(129);
                        _Drawable =  context.getResources().getDrawable(R.drawable.ic_lock_24dp);
                    }
                    int _width = (int)icon_width;
                    _Drawable.setBounds(0,0,_width, _width);
                    editText.setCompoundDrawables(_Drawable, null, null, null);
                    return true;
                }else{
                    return false;
                }
            }else{
                return false;
            }
        }
    });

Browser other questions tagged

You are not signed in. Login or sign up in order to post.