I want to use existing onClick method to make my program simpler. It consists of onClick method and other method:
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.button1:
....
break;
}
}
void foo(){
....
onClick(????);
}
Is there any way to make it do the same behaviour like when i click it on the phone?
you can use View.performClick()
reference
performClick() will play a sound just like if the user clicked on that view, therefore in most cases it's better to use callOnClick(), which will call the OnClickListener without playing any click sound. (Available since API level 15)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) myView.callOnClick(); //won't play sound
else myView.performClick(); //will play sound
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With