Search code examples
androidandroid-activityonclickclickonclicklistener

performClick() and callOnClick() doing nothing


I am adding voice commands to an already existing, and fully functioning app. The voice interaction seems to work fine. I am able to say the command, I am getting the correct result, and I am even getting the correct button, which click interaction I want to simulate by using the voice.

My problem is that performClick() and callOnClick() seem to not even be called. Each button has its Listener (manual clicks work).

As a side note: I am calling the click-methods outside of my activity. Is that a problem?

How do I get these methods to really trigger the onClick's of my buttons?

PS: Maybe some suggestions would be to use the functions direclty instead of using the click-event. The first point, why I don't want to do that, is that I want to avoid duplicate code or a code restructuring, and second, I would like to keep my voice recognition away from being wired too deep into the code by behave like a simple user (hence the programmatical clicking).

EDIT:

Basically I am not doing anything else than:

From my main activity, I am calling my helper class:

util = new MainVoiceUtil(this, getLayoutInflater(), viewgroup);

viewgrouphere is unrelated to this topic. In MainVoiceUtil it goes:

public MainVoiceUtil(Context context, LayoutInflater inflater, Viewgroup viewgroup)
{
   super(context);
   ...
}

MainVoiceUtil extends VoiceUtil, which contains a method onResult, which contains the performClick. context is set in the constructor of VoiceUtil. See:

@Override
public void onResult(AIResponse result)
{
   View button = ((Activity) context).findViewById(buttonId);
   button.performClick();
}

Thanks to log outputs, I know that I have the right button. This means that I know that I am entering the onResult-method. All log outputs are shown, just this click does not seem to be executed.


Solution

  • It turned out that I was calling the wrong method. The main activity was overriding a method, called onButtonClick. This one was deciding by ID, which button was clicked. I thought it was called anyways as soon as an onClick was triggered, which didn't seem to be the case. It seems like it goes the other way. By clicking manually, both seem to work together. But in my case I had to go from button.performClick() to call onButtonClick(buttonId) instead.

    I was not familiar with this, hence my confusion. So this was rather a project specific misunderstanding.