1

I have a button:

<Button
    android:id="@+id/my_btn"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="My Button"
    android:textColor="@android:color/black"
    android:textSize="20sp"
    android:textStyle="bold" />

And in my activity I set an onclicklistener:

findViewById(R.id.my_btn).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        handleButtonClick();
    }
});

In spots in my code I set the button's visibility to View.INVSISBLE and then back again to View.VISIBLE. When it goes from invisible to visible and I try to click on it, it sometimes takes several clicks before the onclicklistener actually receives the event. Meanwhile whenever I click on it and nothing happens, logcat records this every time:

D/ViewRootImpl@1b16f49[MainActivity]: ViewPostImeInputStage processPointer 0
D/ViewRootImpl@1b16f49[MainActivity]: mHardwareRenderer.destroy()#4
D/ViewRootImpl@1b16f49[MainActivity]: dispatchDetachedFromWindow
W/InputEventReceiver: Attempted to finish an input event but the input event 
receiver has already been disposed.
D/InputTransport: Input channel destroyed: fd=71

Once the button click event is actually received, every other click on the button works until its made invisible and back to visible again.

What should I do to ensure the button click event is always recieved and handled?

K. Rawls
  • 95
  • 1
  • 3
  • 8

2 Answers2

2

The root reason is the two xml properties: android:focusable="true" and android:focusableInTouchMode="true", which causes the button just get focused before its OnClickListener be fired, and each time you set the button View.INVISIBLE and back again to View.VISIBLE, it loses focus, so the weird phenomenon happens again.

See the SO question for more details.

Hong Duan
  • 4,234
  • 2
  • 27
  • 50
0

The solution was actually that I was hiding alerts when I should have been dismissing them.

I had alerts automatically go away after a few seconds by calling alert.hide() but this doesn't delete them. So when the focus was lost from my main activity, the focus was given back to the hidden alerts and thus the button wasn't working on my main activity until I clicked the screen enough times to dismiss all the hidden alerts.

I changed the code to call alert.dismiss() and I no longer have the issue.

K. Rawls
  • 95
  • 1
  • 3
  • 8