Search code examples
androidandroid-edittextandroid-textwatcher

Click on char on EditText


I have editText view with same text. On my way user can touch on char in editText. How I can detect on what char user create touch event?


Solution

  • Override the touch method

    public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
             Layout layout = ((TextView) v).getLayout();
                int x = (int)event.getX();
                int y = (int)event.getY();
                if (layout!=null){
                    int line = layout.getLineForVertical(y);
                    int characterOffset = layout.getOffsetForHorizontal(line, x);
                    Log.i("index", ""+characterOffset);
                    }
                return true;
    
    
        }
    

    sources here