2

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.

Amit Raz
  • 5,370
  • 8
  • 36
  • 63
  • [onClickListener on a LinearLayout](http://stackoverflow.com/questions/2130875/onclicklistener-on-a-linearlayout) and [LinearLayout's click listener is never called](http://stackoverflow.com/questions/5651894/linearlayouts-click-listener-is-never-called) might help. – Shobhit Puri Jul 29 '13 at 17:34

2 Answers2

1

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"
codeMagic
  • 44,549
  • 13
  • 77
  • 93
0

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();

        }
    });
Wryday
  • 161
  • 7
amatellanes
  • 3,645
  • 2
  • 17
  • 19