Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 719
daemon54 Avatar asked Mar 06 '13 20:03

daemon54


People also ask

Can we use LinearLayout in ConstraintLayout?

Most of what can be achieved in LinearLayout and RelativeLayout can be done in ConstraintLayout. However, learning the basics of LinearLayout and RelativeLayout is important before trying to understand how to use it with ConstraintLayout.

What is the relevance of LinearLayout?

LinearLayout is a view group that aligns all children in a single direction, vertically or horizontally. You can specify the layout direction with the android:orientation attribute. Note: For better performance and tooling support, you should instead build your layout with ConstraintLayout.

Can we set OnClickListener on TextView?

In Android, TextView is a child class of View, and hence can use the method setOnClickListener() on the object of TextView. In this tutorial, we will learn how to set OnClickListener for TextView in Kotlin file, with the help of an example.


1 Answers

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.

like image 179
quietmint Avatar answered Oct 23 '22 11:10

quietmint