Search code examples
androidonclicklistenerimagebutton

Android ImageButton click not working sometimes


I'm using ImageButton in my xml, like this:

<FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        >
        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageButton_home"
            android:src="@drawable/home_icon"
            android:background="@android:color/transparent"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="10dp"
            android:focusableInTouchMode="false"
            android:focusable="false"
            android:clickable="true"
            />

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageButton_back"
            android:src="@drawable/back_icon"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="10dp"
            android:background="@android:color/transparent"
            android:visibility="gone"
            android:focusableInTouchMode="false"
            android:focusable="false"
            android:clickable="true"
            />

    </FrameLayout>

Only one image button is visible at a time. And I'm capturing the click in my java code as following:

  imageButtonHome.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent homeIntent = new Intent(SettingsActivity.this, MainActivity.class);
            startActivity(homeIntent);
        }
    });

This works fine sometimes, but not always. I see this line in my logcat whenever I click on image button:

D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN

This line always shows up, even when my ImageButton click action is not performed.

But when my ImageButton click works correctly, this other line adds up to log:

 D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
 D/AbsListView: Get MotionRecognitionManager

I want my image button to work every time. Please help.


Solution

  • Give some padding to your ImageButtons in order to capture the touch event.