LinearLayout linear click viewer is never called

Trying to get an onclick listener that works on linearlayout but never called it :(. Enabled clickable and focsuable (both modes) and still can't get a click responder. 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 an 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 why I do this, and not use the "Image" button, is because the background of the "button" is based on the state (gradient change), as well as the image and in order to combine with them by clicking / on focus, I used linearlayout, which has an ImageView on its own .. any suggestions on why clickListener is not working on linearLayout?

THX

+6
source share
3 answers

Did a click go into ImageView instead of LinearLayout? Try clicking in the pad area (if any) or try placing the click listener in ImageView1.

+5
source

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

A simple way is to install the same click listener on the image view and text view.

 View.OnClickListener activityLauncher = new View.OnClickListener() {... } layout.setOnClickListener(activityLauncher); imageView.setOnClickListener(activityLauncher); textView.imageView.setOnClickListener(activityLauncher); 
+3
source

The width of your LinearLayout is "0dip" , you will not see anything on the screen.

If the width changes to "FULL_PARENT" , this works. Please check your code carefully.

-3
source

Source: https://habr.com/ru/post/885835/


All Articles