randbo
randbo

Reputation: 36

How to listen for Key input from User?

I managed to get a Alert Dialog to pop up with a editText to handle input from the user. How would i handle the submit process when they hit Enter on the keyboard? I would like to refrain from using a button to submit and change the text. Hopefully i gave enough detail as i am still quite new to this. Thanks for your time.

Phone App Pic

The Alert Dialog:

(1..912).forEach {
        val id = resources.getIdentifier("Price$it", "id", packageName)
        val tv = findViewById<TextView>(id)
        tv.setOnLongClickListener {

            //Alert Window
            val alertDialog = AlertDialog.Builder(this@MainActivity)
            alertDialog.setTitle("NEW PRICE")
            val input = EditText(this@MainActivity)
            val lp = LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT
            )
            input.layoutParams = lp
            alertDialog.setView(input).show()
            return@setOnLongClickListener true

        }
    }

UPDATE:

(1..912).forEach {
        val id = resources.getIdentifier("Price$it", "id", packageName)
        val tv = findViewById<TextView>(id)
        tv.setOnLongClickListener {

            //Alert Window
            val alertDialog = AlertDialog.Builder(this@MainActivity)
            alertDialog.setTitle("NEW PRICE")
            val input = EditText(this@MainActivity)
            //Alert Submit on Enter
            input.setOnKeyListener { v, keyCode, event ->
                if (event.action == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER) {
                    // Input changes text
                    tv.text = input.text
                    when {
                        tv.text.startsWith("-") -> tv.setTextColor(Color.RED)
                        tv.text.startsWith("+") -> tv.setTextColor(Color.GREEN)
                    else -> {
                        tv.text = "_"
                        tv.setTextColor(Color.DKGRAY)
                    }
                    }
                    // Hide Keyboard
                    // Save Price Table
                }
                false
            }

Upvotes: 0

Views: 3681

Answers (2)

alextcn
alextcn

Reputation: 529

You need to set OnEditorActionListener for your EditText:

val input = EditText(this@MainActivity)
input.setOnEditorActionListener { _, actionId, event ->
    // If triggered by an enter key, this is the event; otherwise, this is null.
    // if shift key is down, then we want to insert the '\n' char in the TextView;
    if (event == null || event.isShiftPressed) return@setOnEditorActionListener false
    // TODO: your code goes here
    return@setOnEditorActionListener true
}

In this example I additionally check that shift isn't pressed. It will work on all devices with any kind of keyboard.

Note 1. We don't need actionId here, but you still can set different actions for the keyboard (with input.imeOptions = EditorInfo.IME_ACTION_SEND or with xml attribute android:imeOptions="actionSend") and listener will be called for any kind action on any type of keyboard. Read Android documentation to learn more about actions.

Note 2. I made custom wrapper for all this logic that allows me to set enter key listener in the easiest possible way. Check out this gist.

editText.setOnEnterActionListener {
    Toast.makeText(context, "Click", Toast.LENGTH_SHORT).show()
}

Upvotes: 1

forpas
forpas

Reputation: 164164

You can set your custom OnKeyListener for the EditText:

val input = EditText(this@MainActivity)
input.setOnKeyListener(View.OnKeyListener { v, keyCode, event ->
    if (event.action == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER) {
        // your code here
        true
    }
    false
})

Upvotes: 3

Related Questions