EditText, OnTouchListener and setSelection doesn't work on first touch

17,330

Solution 1

To avoid the error do something like this:

yourEditText.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                yourEditText.setFocusable(true);
                yourEditText.requestFocus();
                yourEditText.setSelection(emailEditText.getText().length());
                break;
            case MotionEvent.ACTION_UP:
                v.performClick();
                break;
            default:
                break;
            }
            return true;

        }
    });

Solution 2

Just put the edittext as like below

   <EditText    
    android:id="@+id/from"
    android:focusable="false" 
    android:hint="@string/pickup_location"
    android:imeOptions="actionNext"  
    android:inputType="textNoSuggestions"    
    android:padding="10dp"
    android:textColor="@android:color/white" />

Set the ontouch listener in your activity

 EditText et = (EditText)findViewById(R.id.et); et.setOnTouchListener(this)



@Override   public boolean onTouch(View v, MotionEvent event) {     
if (event.getAction() == MotionEvent.ACTION_UP)     
    switch (v.getId()) {            
            case R.id.from:
                //do your action
                break;

Solution 3

You could do

redTime.setOnTouchListener(new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        Log.i("click", "onMtouch");
        redTime.setFocusable(true);
        redTime.requestFocus();
        redTime.setSelection(redTime.getText().length());
        return false;
    }
});

Before you call setSelection, that way redTime will have the focus when you do the selection.

Solution 4

use this method:

redTime.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            if (event.getAction()==MotionEvent.ACTION_UP){  //check the event
                 redTime.requestFocus();  //keep focus on the EditText(redTime)
                 redTime.selectAll();  //select all text
            }

        }
        return true;
});
Share:
17,330
mcsilvio
Author by

mcsilvio

Updated on June 05, 2022

Comments

  • mcsilvio
    mcsilvio almost 2 years

    I've got the following code which fires every time my "redTime" EditText is touched.

    redTime.setOnTouchListener(new OnTouchListener() {
    
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Log.i("click", "onMtouch");
                redTime.setSelection(redTime.getText().length());
                return false;
            }
        });
    

    It is meant to keep the cursor on the right side of the EditText upon every touch. The problem is that the line containing the "setSelection" method doesn't work upon the FIRST touch of the control. That is, if another control has focus, and I touch the "redTime" control for the first time, the method is fired, but the cursor remains at the location I touched (not the right side).

    How do I know the listener fires? The "Log.i" call works, but not the cursor change. I suspect the "setSelection" call is working, but some later event is negating it.

    I tried a few things. I tried consuming the event by returning TRUE in the listener. Didn't work. I tried repeating the "setSelection" call in OnTouchListener, and OnFocusChanged as well. Still doesn't work.

    I almost forgot. Here is the XML for the control in question.

    <EditText
                android:id="@+id/redEditText"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:ellipsize="end"
                android:gravity="right"
                android:inputType="time"
                android:text="@string/zeroTime"
                android:textColor="#ff0000"
                android:textSize="32sp" >
            </EditText>