androidkotlinandroid-sourceandroid-softkeyboardandroid-automotive

How to display keyboard in two display at a time in android?


I want to display keyboard in both primary and secondary display.

But the keyboard is displaying in any one of the display. Let me know is there any work around to display the keyboard in both display[By using android default keyboard]?

Actual Behaviour: When I keep cursor in primary display, the keyboard is displaying. Now when i keep cursor in secondary display, the keyboard gets closed in primary display and displaying in secondary display.

Expected Behaviour: In both display, keyboard should display.

Platform: Android 12 and above? If it not possible, then how multi displays such as infotainments are displaying keyboards in both passenger and central display?

The below POC works only in one display and not in other display. Is there any API Android used to handle and let me know how to use?

class MainActivity : AppCompatActivity() {
    private lateinit var displayManager: DisplayManager
    private lateinit var inputMethodManager: InputMethodManager

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Get display and input method managers
        displayManager = getSystemService(Context.DISPLAY_SERVICE) as DisplayManager
        inputMethodManager = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager

        // Set up multiple displays
        setupMultipleDisplays()
    }

    private fun setupMultipleDisplays() {
        // Get all available displays
        val displays = displayManager.displays

        // Check if we have at least two displays
        if (displays.size >= 2) {
            try {
                // Create presentation for the second display
                val secondaryDisplay = displays[1]
                val secondaryPresentation = SecondaryDisplayPresentation(this, secondaryDisplay)
                secondaryPresentation.show()

                // Set up input for both displays
                setupDisplayInputs(displays)
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }

    private fun setupDisplayInputs(displays: Array<Display>) {
        // Primary display input - use safe null check
        val primaryEditText: EditText? = findViewById(R.id.primaryEditText)
        primaryEditText?.setOnFocusChangeListener { view, hasFocus ->
            if (hasFocus) {
                showSoftInput(view, displays[0])
            }
        }

        // Secondary display input - use safe null check
        val secondaryEditText: EditText? = findViewById(R.id.secondaryEditText)
        secondaryEditText?.setOnFocusChangeListener { view, hasFocus ->
            if (hasFocus) {
                showSoftInput(view, displays[1])
            }
        }
    }

    private fun showSoftInput(view: View, display: Display) {
        // Show soft input keyboard on the specific display
        view.requestFocus()

        // Show soft input keyboard
        inputMethodManager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT)
    }

    // Secondary display presentation
    inner class SecondaryDisplayPresentation(
        context: Context,
        display: Display
    ) : Presentation(context, display) {

        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.custom_keyboard)
        }
    }
}

Actual Output:(Keyboard is not displaying in Primary Display when focus is on Secondary Display) in the below image:

https://i.sstatic.net/nubYyacP.png

Expected Output: Keyboard should display in both primary and secondary display.


Solution

  • I want to display keyboard in both primary and secondary display

    That is not offered by stock Android for phones and tablets. I am not aware of any device manufacturers that offer full touchscreen support for secondary displays where the secondary displays are showing separate content (rather than a mirror of the primary display).

    You are certainly welcome to create your own fork of Android, running on your own custom hardware, that attempts to support having IMEs on multiple displays. Or, you could look to contribute such support to projects like LineageOS, if they support your desired hardware. Or, you could find somebody who offers hardware and their own customized Android build that supports multiple IMEs.

    then how multi displays such as infotainments are displaying keyboards in both passenger and central display?

    There are three major possibilities:

    1. As was noted in a comment, Android Automotive 14 supports IMEs on multiple displays
    2. The car manufacturer or a supplier customized Android, as I suggested in my second paragraph above
    3. The car uses multiple OS/OS copies (e.g., two running Android Automotive, one running Android Automotive and one running something else) on distinct computers or hypervisor sessions, with each display tied to its own OS or OS copy