Home > database >  How can I get ID from ViewBinding to use it with switch case statement?
How can I get ID from ViewBinding to use it with switch case statement?

Time:11-06

@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.iv_camera:
            Toast.makeText(this, "Camera Clicked", Toast.LENGTH_SHORT).show();
            break;
        case mBinding.ivCamera.getId():
            break;
    }
}

The first case works perfectly fine however, the second one throws an error that says Constant Expression Required . Am i forced to use R.id.something or can I continue with view binding?

CodePudding user response:

The reason is that case statements in java needs to be compile time constant. Since mBinding.ivCamera.getId() is not thus the error. Here is already an answer in detail.

  • Related