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.
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
}
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
})