1

I have 2 EditText (myEditText1 and myEditText2)
And I need when touch myEditText1
1. Do some business [No problem here and required business applied]
2. Then request focus to myEditText2 [ No luck? ]

final EditText myEditText1 = (EditText) findViewById(R.id.myEditText1);
final EditText myEditText2 = (EditText) findViewById(R.id.myEditText2);

// ...

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

        // My business when myEditText1 touched [ OK ]
        // ...

        // TODO
        // Need to request focus to another EditText myEditText2 ?

        return false;
    }
});

I try some solutions like following trials
But no luck and focusing still on myEditText1

myEditText2.requestFocus();

Related questions
EditText request focus
Change Focus to EditText Android

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Ahmed Nabil
  • 17,392
  • 11
  • 61
  • 88

4 Answers4

3

You can set an OnClickListener on your edit text like this:

this.editText1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //your code
                editText2.requestFocus();
            }
});

Just take care about a special case
onClick() is not called when the EditText doesn't have focus
And you can find details of this case in the following question thread
onClick event is not triggering | Android

Ahmed Nabil
  • 17,392
  • 11
  • 61
  • 88
Vsionary
  • 48
  • 1
  • 4
2

I would recommend using TextWatcher, than use conditions to know when your business is done

try this:

myEditText1.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if(myEditText1.getText().length() > 3){
                myEditText2.requestFocus();
            }
        }
     });
1

It can be because of the keyboard. You can try

myEditText2.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(myEditText2, InputMethodManager.SHOW_IMPLICIT);

This works for me

Bach Vu
  • 2,298
  • 1
  • 15
  • 19
1

you can try this

public void requestFocus(View view) {
    if (view.requestFocus()) {
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    }
}

and use like,

requestFocus(myEditText2);
Dinesh Sarma
  • 461
  • 3
  • 14