daemon54
daemon54

Reputation: 1115

Issue with onclicklistener to LinearLayout

In order to group a icon and a text, I have grouped them together in a linearlayout and implemented a listener for the linear layout.

<LinearLayout
        android:id="@+id/ll0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="fill_horizontal"
        android:orientation="vertical" >
        <ImageButton
            android:id="@+id/imageButton0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@null"
            android:src="@drawable/start" />
        <TextView
            android:id="@+id/textView0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Start"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    </LinearLayout>

I have implemented the listener, the following way:-

l0 = (LinearLayout)findViewById(R.id.ll0);
l0.setOnClickListener(new View.OnClickListener() 
    {
        public void onClick(View v) 
        {
            //Some Code
        }
    });

The issue I'm facing is that which I click on the icon, the listener doesn't seem to respond. The listener worked when I click the space in between the textview and the icon. I would like the whole part to be clickable, not at a particular point.

Upvotes: 5

Views: 4917

Answers (1)

quietmint
quietmint

Reputation: 14164

I think the ImageButton is a clickable view and is capturing the click, preventing the LinearLayout from receiving the click event. Try to add android:clickable="false" to the XML defining the ImageButton.

However, a better answer is to use a compound drawable. See How do I use a compound drawable instead of a LinearLayout that contains an ImageView and a TextView. Basically you can add android:drawableTop="@drawable/start" to the XML defining the TextView and do away with the LinearLayout and ImageButton altogether. Then you just handle the click on the TextView.

Upvotes: 6

Related Questions