How to identify the click on a drawableLeft

1

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 ?

    
asked by anonymous 27.01.2017 / 17:58

2 answers

2

I did some testing here and it worked great for 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 set. Basically this is the condition: if(click_X <= (left + icon_width) . See onTouchListener 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;
        }
    });
    
27.01.2017 / 23:04
0

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

The implementation follows:

   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;
            }
        }
    });
    
30.01.2017 / 13:04