Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LinearLayout's click listener is never called

Trying to get an onclick listener working on a linearlayout but its never called :(. Have enabled clickable and focsuable (both modes) and still cant get the click listener to respond. Platform details: Android 3.0.. Any help?? Code below

           <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/menu_items_button"
                android:layout_width="0dip"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:orientation="vertical"
                android:gravity="center_horizontal"
                android:paddingTop="@dimen/gen_margin_xsmall"
                android:paddingBottom="@dimen/gen_margin_xsmall"
                android:background="@drawable/rule_bg_menu_button"
                android:clickable="true"
              android:focusableInTouchMode="true"
            >
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/menu_items"
                    android:tag="image"
                />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:tag="text"
                    android:text="@string/menu_items_icon_txt"
                    style="@style/textDisplay.mediumLarge"
                />

            </LinearLayout>

and in the code to add the event listener

_itemsButton = (LinearLayout) menu.findViewById(R.id.menu_items_button);
final Intent itemsIntent = new Intent(this, ItemsActivity.class);
_itemsButton.setOnClickListener(
            new View.OnClickListener() {    
                @Override
                public void onClick(View v) {
                    startActivity(itemsIntent); //Never called!
                }
            }
        );

The reason I'm doing this and not using an Image button instead is because the background of the "button" is state based (gradient changes) but also the image and in order to combine to the two on click / on focus, I used a linearlayout which has an ImageView in itself.. any suggestions on why the clickListener is not working on the linearLayout?

thx

like image 731
Rickster Avatar asked Apr 13 '11 15:04

Rickster


1 Answers

(adding my response as a new answer so that I can use the PRE tag.)

The easy way is to set the same click listener on the image view and the text view.

View.OnClickListener activityLauncher = new View.OnClickListener() {... }
layout.setOnClickListener(activityLauncher);
imageView.setOnClickListener(activityLauncher);
textView.imageView.setOnClickListener(activityLauncher);
like image 137
pzulw Avatar answered Sep 28 '22 03:09

pzulw