I have a fragment which has 4 images(on a Linear Layout) - based on the image clicked, the background should change. I can implement onClick listener for each image and change the background based on the image ID clicked. I wanted to understand if there is a better procedure. I read here(Best practice for defining button events in android) that I can I can register onClick in the XML and handle it. But If I do it that way, it looks for a call back method in activity. But I want it to deal it in Fragments itself. Is there a better way for it?
Asked
Active
Viewed 95 times
-2
-
1In order to determine if there is a better way, it would be helpful for you to provide the code you currently have. Then people can suggest changes to improve it. – Marshall Davis Jun 30 '15 at 21:46
-
Thank you for all the comments and feedback! – ivap arht Jul 01 '15 at 18:26
1 Answers
0
In the onClick method use something like:
if (v.getId() == myItem.getId()) {
// do something
} else if (v.getId() == myItem2.getId()) {
// do something else
} else if (v.getId() == myItem3.getId()) {
// do something else
}

Mark
- 9,604
- 5
- 36
- 64