Search code examples
androidonclicklisteners

Using same onClick listener with more than one view


I am using the same onClick listener for a number of items.

When I click I want to know which one.

I know that I can do a Switch statement on the getId() but would rather be able to get at the name of the item. Is there any easy way to do this?


Solution

  • I think what you are referring to when you say "get the name" is the id string from resources. So you would have a switch statement like:

    switch(view.getId()) {
        case R.id.HomeButtonOne:
            // Do Button One Action
            break;
        case R.id.HomeButtonTwo:
            // Do Button Two Action
            break;
    }
    

    otherwise please elaborate more on what you are trying to achieve.