How do you avoid repeating code for UI elements that are exactly the same? In android, to access an element programatically you need to name each element with a unique id.
For example I have a back button that performs the exact same function wherever its used but because of having to id them, I end up having BackButton1, BackButton2, BackButton3 etc. As a result I have to rewrite the same OnClickListener for each button.
Is there something similar to a html ‘class’ attribute or a similar way to group elements in android? If not, what is the best way to name repeating elements?
if you are defining the button in the layout xml, then you can use the
onclickattribute to refer to a common method in your activity that will called when the button is clicked.if you use
onclick="doBack"in the xml in then your activity you have a method called