ImageView OnClickListener not working with layout_gravity bottom

246 views Asked by At

I have the following layout containing ImageView imageView2, which I want to be completely clickable.

      <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="15dp" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginStart="20dp"
            android:layout_marginEnd="20dp"
            android:layout_weight="1"
            android:src="@drawable/image1"
            android:adjustViewBounds="true" />

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:src="@drawable/image2"
            android:layout_weight="1"
            android:adjustViewBounds="true"
            android:clickable="true"
            android:focusable="true"
            android:layout_gravity="bottom"/>

    </LinearLayout>

I have an OnClickListener set to imageView2. I want the whole image to be clickable. This works fine. However, whenever I add android:layout_gravity="bottom", only the top border of the image remains clickable. However, I want the whole image to be clickable.

How can I get imageView2 to be completely clickable with layout_gravity="bottom"?

I also tried moving the gravity to the parent

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:orientation="horizontal"
        android:gravity="bottom">

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginEnd="20dp"
            android:layout_marginStart="20dp"
            android:layout_weight="1"
            android:adjustViewBounds="true"
            android:src="@drawable/image1" />

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_weight="1"
            android:adjustViewBounds="true"
            android:clickable="true"
            android:focusable="true"
            android:src="@drawable/image2" />

    </LinearLayout>

This did not work either.

1

There are 1 answers

3
hungryghost On

I'm not sure why your entire ImageView wouldn't be clickable. Seems like it should just work. But maybe you can just try wrapping the ImageView inside a FrameLayout:

<FrameLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:clickable="true"
    android:focusable="true" >

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:src="@drawable/image2"
        android:adjustViewBounds="true"
        android:layout_gravity="bottom"/>

</FrameLayout>

Or, (as I mentioned in comments) try loading your image using background instead of src, then remove the adjustViewBounds attribute. I'm guessing that's probably what's causing the strange OnClick behavior:

<ImageView
    android:id="@+id/imageView2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp"
    android:background="@drawable/image2"
    android:layout_weight="1"
    android:clickable="true"
    android:focusable="true"
    android:layout_gravity="bottom"/>