I am trying to figure out how to change textview text on a long key press. I have included a screen shot to hopefully explain what i am trying to do.
the price table with the green +10.00. i want to be able to hold down on each price and have the keyboard pop up to edit the text field. i am new to programming and finding it difficult to search for proper terminology.
the code i have tried so far for this is:
(1..912).forEach {
val id = resources.getIdentifier("Price$it", "id", packageName)
val tv = findViewById<TextView>(id)
when {
onKeyLongPress(id, setText()) -> tv.text.getText()
}
}
having 912 prices i grouped them to reference the ID of each. Instead of trying to apply any code on individual prices. i am aware the onKeyLongPress()
is not correct usage. i stated it like that to help me understand what i wanted to do even though that would not work. Reading through reference material on key presses has left me confused. i don't know where to go from here any information is greatly appreciated. Thank you for your time.
Android has a specific View for editing text: EditText
In your case, you should implement EditText and it will pop up the keyboard and cursor.
For the long click action, you should implement the onLongClickListener:
textView.setOnLongclickListener {
// [ Let the EditText pop up here ]
// ....
true // Return value for the onLongClick function
};
I recommend implementing onLongclickListener in your ListView adapter class (or RecyclerViewAdapter) in the getView() or onBindViewHolder() method. Here is a detailed example for this.
The next step you want to do is enabling EditText so that users can type something. This function should go inside of the code snippet above. ("Let the EditText pop up here" part.)
The options you can take are:
Create an alert dialog that has an EditText. After user types something and click "Okay", the dialog will be dismissed and you can simply pass that user input to the TextView the user originally pressed. For the AlertDialog with an EditText, you can find useful samples here
Overlap the EditText on top of the TextView, and set the visibility of the EditText as View.INVISIBLE. When the user conducts long click on the TextView, you can set the EditText's visibility to View.VISIBLE, which will make the EditText visible to users and allow keyboard input. Once the user is done with typing, they can click "DONE" button in their keyboard or any equivalent of it, then you can set the visibility of the EditText to View.INVISIBLE again. Make sure to retrieve the EditText value and set it to your TextView.