3

I have a ListView with an invisible EditText and a clickable TextView in each listview item. I am trying to implement a behaviour where clicking on the TextView will make the EditText visible, focusing on the EditText and make the soft keyboard appear automatically.

I am using adjustResize as my input method instead of adjustPan because apparently the S3 i am testing the feature on goes bonkers with adjustPan. (Specifically, when soft keyboard appears, the whole view gets pushed up, thats great. But, as soon as i start typing, the whole view slide back down as if the soft keyboard isnt there! But it is! Problem doesnt occur with HTC One). The listview also has afterDecendents.

When the listview has more items that can fit into the screen entirely, a problem occurs where the softkeyboard triggered is not responsive to the editText.

I programatically trigger the softkeyboard with imm.showSoftInput(holder.editText,0) and the entire view re-adjust itself to accomodate the softkeyboard. Once the softkeyboard is shown and the view completed its relayout, all key entries except backspace are not received by the edittext. That means i cannot add any words or numbers to the edittext but i can delete them, which is weird as hell.

Another funny thing is, I realized that if i do not programatically show the soft keyboard and have the user tap on the visible edittext to reveal the softkeyboard, everything works fine. (Which also baffles me because I suspected the listview recycling and focusing mechanism is the one causing all this problems. Where softkeyboard appears, causes listview to relayout hence recycling of views so reference to the editText is lost etc.)

So, ultimately, my question is, is there any way for me to reliably programatically simulate a user tap on the visible edit text so that i can trigger a softkeyboard that works properly with the "clicked" edittext?

I have tried the following method too and it doesnt work reliably (sometimes it works, sometimes it doesnt. Playing with the delay duration doesnt do anything.).

holder.editText.postDelayed(new Runnable(){
    @Override
    public void run() {
         holder.editText.requestFocus();
         holder.editText.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN , 0, 0, 0));
         holder.editText.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP , 0, 0, 0));
         }
}}, 500);

Other permutation of answers i have tried from many SO questions also didnt really work reliably. Example questions: How to show soft-keyboard when edittext is focused , How come the Android soft keyboard is not responding to EditText? , Android: Dialog box show soft keyboard automatically when focus is on an EditText not working

Community
  • 1
  • 1
Tikiboy
  • 892
  • 10
  • 20

1 Answers1

-2

Q Is there any way for me to reliably programatically simulate a user tap on the visible edit text

so that i can trigger a softkeyboard that works properly with the "clicked" edittext?

Yes,

Add this when you want simulate your click on the EditText

          editText.performClick();    //softkeyboard will open
          editText.setPressed(true);  // edittext will get highlighted
          editText.invalidate();        //Invalidate the whole view

Done!!

Metalhead1247
  • 1,978
  • 1
  • 17
  • 28
  • Ive tried `performClick()` before but it didnt reveal the softkeyboard. The listview is invalidated when the editText visibility changes from invisible to visible. Ill try out your code sequence. Hopefully the `setPressed(true)` does the trick. :D – Tikiboy Sep 18 '13 at 07:18
  • 1
    Ok, just tested it. Unfortunately it doesnt cause the softkeyboard to appear. :( I even registered a click event listener. `performClick()` does trigger a click event, but it doesnt cause the softkeyboard to appear. Ive even looked at the android 4.0 EditText(TextView) source code to see what triggers the softkeyboard and it seems to only be triggered in 3 places which is `onKeyUp()`, `onTouchEvent()` and when the user triggers a landscape rotation. – Tikiboy Sep 18 '13 at 07:42
  • @Tikiboy call all the 3 method at once and try ( performclick,setand invalidate) – Metalhead1247 Sep 18 '13 at 08:13
  • I did call all 3 of them at once. It didnt work. :( It might work if this is just a standard edittext nested in a single root view (didnt test it so not sure). But in this senario, its an edittext in a listview item. ;/ – Tikiboy Sep 18 '13 at 11:33