Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android call onClick method without Clicking

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?

like image 358
Victorio Pui Avatar asked May 24 '13 10:05

Victorio Pui


2 Answers

you can use View.performClick()

reference

like image 187
Iftikar Urrhman Khan Avatar answered Sep 27 '22 16:09

Iftikar Urrhman Khan


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
like image 24
steliosf Avatar answered Sep 27 '22 15:09

steliosf