Search code examples
javaandroidonclicklistener

Significance of using ID in switch statements in onClick()


I wanted to ask what was the significance of creating a switch statement when implementing the onClickListener interface for multiple buttons. Like, we're already calling the setOnClickListener for that particular button by saying button_name.setOnClickListener()

So what's the point of specifying the id's again in the switch statement ? Why is it necessary to do so ? doesn't the point of doing button_name.setOnClickListener() mean - "do what's in here for this button" ?

public class MainActivity extends ActionBarActivity implements View.OnClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);


    // Capture our button from layout
    Button button = (Button)findViewById(R.id.corky);
    Button button2 = (Button)findViewById(R.id.corky2);
    Button button3 = (Button)findViewById(R.id.corky3);
    // Register the onClick listener with the implementation above
    button.setOnClickListener(this);
    button2.setOnClickListener(this);
    button3.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    // do something when the button is clicked
    // Yes we will handle click here but which button clicked??? We don't know

    // So we will make
    switch (v.getId() /*to get clicked view id**/) {
        case R.id.corky:

            // do something when the corky is clicked

            break;
        case R.id.corky2:

            // do something when the corky2 is clicked

            break;
        case R.id.corky3:

            // do something when the corky3 is clicked

            break;
        default:
            break;
    }
}

}

What I'm trying to ask is, doing button2.setOnCLickListener() means - "Do what's set by this function for button2" right ? if not then what is the actual purpose/function performed by setOnClickListener() ?


Solution

  • Since you have set clickListeners to 3 different buttons, the method

    ....onClick(View v){.....

    is going to get called when you click any of those 3 buttons. If you click the button1 you do not want to perform task actually assigned for button3. So to check and find out which button was pressed the switch statement is necessary.

    Alternatively you could have done this:

        corkyButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v){
               // do something when the corky is clicked
            }
        });
    
        playButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v){
               // do something when the play is clicked
            }
        });
    

    so on and so forth...

    This way you don't need a switch statement as in this case you know which button is being clicked on, AS you're setting the clickListener AND ALSO specifiying what you wanna do there itself.