How to determine which button pressed on android

Most ellegant pattern to follow:

public void onClick(View v) {
switch(v.getId())
{
case R.id.button_a_id:
// handle button A click;
break;
case R.id.button_b_id:
// handle button B click;
break;
default:
throw new RuntimeException("Unknow button ID");
}

This way it’s much simplier to debug it and makes sure you don’t miss to handle any click.

Leave a Comment