I'm trying to "press a button" programmatically in a Fragment with no success.
Any ideas of how to do it?
This is what I've tried so far:
private fun pressKey(editText: View) {
        // Prepare a list of different events to test //
        val list = listOf(
                KeyEvent(0, 0, 0, KeyEvent.KEYCODE_NUMPAD_ENTER, 0, 0, 0, 0, KeyEvent.KEYCODE_ENDCALL),
                KeyEvent(0, 0, 0, KeyEvent.KEYCODE_NUMPAD_ENTER, 0, 0, 0, 0, KeyEvent.ACTION_UP),
                KeyEvent(0, 0, 0, KeyEvent.KEYCODE_NUMPAD_ENTER, 0, 0, 0, 0, KeyEvent.ACTION_DOWN),
                KeyEvent(0, 0, 0, KeyEvent.KEYCODE_ENTER, 0, 0, 0, 0, KeyEvent.KEYCODE_ENDCALL),
                KeyEvent(0, 0, 0, KeyEvent.KEYCODE_ENTER, 0, 0, 0, 0, KeyEvent.ACTION_UP),
                KeyEvent(0, 0, 0, KeyEvent.KEYCODE_ENTER, 0, 0, 0, 0, KeyEvent.ACTION_DOWN),
                KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_NUMPAD_ENTER),
                KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER),
                KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_NUMPAD_ENTER),
                KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_ENTER),
                KeyEvent(KeyEvent.KEYCODE_ENDCALL, KeyEvent.KEYCODE_NUMPAD_ENTER),
                KeyEvent(KeyEvent.KEYCODE_ENDCALL, KeyEvent.KEYCODE_ENTER))
        val eventList = listOf(
                KeyEvent.KEYCODE_ENTER,
                KeyEvent.KEYCODE_NUMPAD_ENTER,
                KeyEvent.KEYCODE_ENTER)
        // Try each one of the events in a different way //
        list.forEach { activity!!.dispatchKeyEvent(it) }
        // similar to above
        list.forEach { view!!.dispatchKeyEvent(it) }
        // similar to above
        list.forEach { editText.dispatchKeyEvent(it) }
        ///////////////////////////////////////
        val inputConnectionEditText = BaseInputConnection(editText, true)
        list.forEach { inputConnectionEditText.sendKeyEvent(it) }
        // similar to above
        val inputConnection = BaseInputConnection(view!!, true)
        list.forEach { inputConnection.sendKeyEvent(it) }
        ///////////////////////////////////////
        val instrumentation = Instrumentation()
//      eventList.forEach { instrumentation.sendKeyDownUpSync(it) } // E/UncaughtException: java.lang.RuntimeException: This method can not be called from the main application thread
    }
Thanks in advance.
off-topic: I'm force to write more text otherwise I'm not allow to publish, I hope there is enough info in the post, if not, just let me know
