AK824
AK824

Reputation: 217

Android RelativeLayout with multiple elements set ClickListener

I am trying to set an onClickListener for a RelativeLayout with multiple elements. I have placed the listener on the RelativeLayout but it will not register if you click on top of any of the children elements, only if you click around them. I have tried a few different ways setting the children to clickable="true" or all focusable="false" but cant seem to get the entire layout to act like one button. Is this possible or do I need clickListeners for each of the children essentially doing the same thing?

<RelativeLayout 
        android:id="@+id/nearbyLikeGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:layout_gravity="center"
        android:gravity="center"
        android:layout_weight="1">

            <ImageView 
                android:id="@+id/nearbyLikeIconImageView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp"
                android:layout_centerInParent="true"
                android:focusable="false"
                android:contentDescription="@string/content_description"
                android:src="@drawable/like_icon"/>

            <Button
                android:id="@+id/nearbyFeedsLikeButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@android:color/transparent"
                android:layout_toRightOf="@+id/nearbyLikeIconImageView"
                android:focusable="false"
                android:paddingBottom="15dp"
                android:paddingTop="15dp"
                android:text="@string/like"
                android:textColor="@color/white"
                android:textSize="12sp" />
</RelativeLayout>

Adapter Class :

RelativeLayout likeGroup = (RelativeLayout)v.findViewById(R.id.nearbyLikeGroup);

likeGroup.setOnClickListener(new OnClickListener() 
{ 
    @Override
    public void onClick(View v) 
    {
        Log.d("test", "layout group clicked");                  
    }
});

Upvotes: 0

Views: 976

Answers (1)

Xaver Kapeller
Xaver Kapeller

Reputation: 49817

To prevent the child views from consuming the click event you need to set android:clickable="false" on them. I assume only the Button is making problems since ImageViews are not clickable by default.

Try this layout:

<RelativeLayout 
        android:id="@+id/nearbyLikeIconGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:layout_gravity="center"
        android:gravity="center"
        android:layout_weight="1">

            <ImageView 
                android:id="@+id/nearbyLikeIconImageView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp"
                android:layout_centerInParent="true"
                android:focusable="false"
                android:contentDescription="@string/content_description"
                android:src="@drawable/like_icon"/>

            <Button
                android:id="@+id/nearbyFeedsLikeButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@android:color/transparent"
                android:layout_toRightOf="@+id/nearbyLikeIconImageView"
                android:focusable="false"
                android:clickable="false"
                android:paddingBottom="15dp"
                android:paddingTop="15dp"
                android:text="@string/like"
                android:textColor="@color/white"
                android:textSize="12sp" />
</RelativeLayout>

Upvotes: 1

Related Questions