Amit Raz
Amit Raz

Reputation: 5536

catching OnClick event on layout which has elements on it

I have a UI segment which look like this :

 <LinearLayout
            android:orientation="horizontal"
            android:minWidth="25px"
            android:minHeight="25px"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/linearLayout4">
            <ImageView
                android:src="@android:drawable/ic_menu_gallery"                    
                android:layout_width="0dp"
                android:layout_height="24dp"
                android:layout_weight="1" />
            <TextView
                android:text="Text"
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:gravity="center" />
        </LinearLayout>

I was wandering whether its possible to listen to a click event on the linearlayout and get it even if i click on the ImageView, similar to whats going on in WPF with routed events.

Thanks.

Upvotes: 2

Views: 5924

Answers (2)

amatellanes
amatellanes

Reputation: 3735

You must set an onClickListener in your LinearLayout:

    LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linear_layout);
    linearLayout.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            Toast.makeText(getApplicationContext(), "Hello World!",
                    Toast.LENGTH_LONG).show();

        }
    });

Upvotes: 0

codeMagic
codeMagic

Reputation: 44571

You should be able to set the LinearLayout to clickable

<LinearLayout
        android:orientation="horizontal"
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout4"
        android:clickable="true">

then set this on each child

android:duplicateParentState="true"

Upvotes: 1

Related Questions