Fry
Fry

Reputation: 6275

Select EditText programmatically in Android

I'm new in Android and I can't select an EditText programmatically. My scenario. I have a lot of EditText, but only one is enable, I'll calling it A. The user write in this EditText A with the keyboard; when he finishes writing, starts an algorithm that recognize the text inside A and put it in the right EditText, for example B. After this I need to empty A and set the focus on it, show the cursor inside A and show the keyboard.

I'll try with myET.requestFocus() but nothing happens.

How can I make A editable again without the user having to touch A ?

Thanks

Upvotes: 1

Views: 4366

Answers (2)

MaTTP
MaTTP

Reputation: 953

Try this

final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            myET.requestFocus();
        }
    }, 100);

Upvotes: 3

Nabz
Nabz

Reputation: 390

In order to show the keyboard without pressing on the TextEdit, you must put the code inside a handler with some delay. It works well with 200ms delay, but failed without any delay or with only a delay of 1ms.

   (new Handler()).postDelayed(new Runnable() {

        public void run() {
            youEditText.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN , 0, 0, 0));
            youEditText.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP , 0, 0, 0));                       

        }
        }, 200);

And so by this code, you simulate a tap on the EditText. Cheers

Upvotes: 3

Related Questions