I have one function and many buttons.
Function get name of button and do something.
How get property of button on which function called?
fun test(view: android.view.View)
{Toast.makeText(this, this.button.name, Toast.LENGTH_LONG).show()}
kraisler is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
You can cast the View parameter to a Button within your test function to access its properties. For example:
fun test(view: View) {
if (view is Button) {
Toast.makeText(this, view.text, Toast.LENGTH_LONG).show()
}
}
This way, when any button is clicked, the function retrieves and displays the text of the clicked button.
Going forward, make sure all your buttons use this test function as their onClick handler. If you need other properties, you can access them similarly after casting.
so, i find tag
it’s can be use like text, my end solution for draw image:
fun test(view: View) {
val resourceId = resources.getIdentifier(view.tag.toString(), "drawable", packageName)
imageView.setImageResource(resourceId)}
kraisler is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.