Scenario: I have three buttons defined in xml
<button android:id="@+id/firstbtn"
...
/>
<button android:id="@+id/secbtn"
...
/>
<button android:id="@+id/thirdbtn"
...
/>
In Java one way to listen to them is
Button firstbtn = (Button) findViewById(R.id.firstbtn);
firstbtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(getBaseContext(),
"You have clicked first button",
Toast.LENGTH_SHORT).show();
}
});
for second btn , same code has to be repeated with different id ??
How can I make it generic enough that , it can listen to all buttons (say in for loop) and while handling I should be able to differentiate different btns. (may be get elements id)