-2

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?

Community
  • 1
  • 1
ivap arht
  • 19
  • 3

1 Answers1

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