Skip to content Skip to sidebar Skip to footer

Android - Listview Doesn't Receive Onitemclick For Textview With Clickable Links

I have a ListView that contains a TextView in each row besides a number of other views. The TextView renders html content which may contain links. The below code appears in the li

Solution 1:

This is actually a BUG. To resolve this you can add android:descendantFocusability="blocksDescendants" you your ListView's rows layout xml. For e.g

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:descendantFocusability="blocksDescendants"android:orientation="vertical" >

 <TextView
    android:id="@+id/lblStatusMessage"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:autoLink="web"
    android:focusable="false"
    android:textSize="15sp" />
</LinearLayout>

Source : this and this

Good Luck :)

Solution 2:

Focusable views inside a ListView item will disable the ability to select ListView items. Applying android:focusable="false" to the TextView will allow OnItemClick to work again. You may also need to apply android:focusableInTouchMode="false" to make the trackball ignore the links because clicking the trackball over a focusable element in a ListView can both click the link and the ListView item.

Solution 3:

Solution 4:

I had the same issue and none of these answers worked for me. Eventually, I managed to fix the problem by removing the attribute android:inputType="textMultiLine".

Solution 5:

TextView that only responds to touch events of links. Based on https://stackoverflow.com/a/7327332/1768722

publicclassAutoLinkTextViewextendsTextView {

            publicAutoLinkTextView(Context context, AttributeSet attrs, int defStyle) {
                super(context, attrs, defStyle);
                init();
            }

            publicAutoLinkTextView(Context context, AttributeSet attrs) {
                super(context, attrs);
                init();
            }

            publicAutoLinkTextView(Context context) {
                super(context);
                init();
            }

            privatevoidinit() {
                this.setAutoLinkMask(Linkify.ALL);
            }

            /**
             * @Linkify applies to a movementMethod to the textView @LinkMovementMethod.
             *          That movement method thought it implements a scrolling
             *          vertically method it overrides any other scrolling method the
             *          parent has.
             * 
             *          Although touchEvent can be dispached to the parent, the specific
             *          parent ScrollView needed the whole sequence ACTION_DOWN ,
             *          ACTION_MOVE, ACTION_UP to perform (sweep detection). So the
             *          solution to this problem is after applying @Linkify we need to
             *          remove the textView's scrolling method and handle the @LinkMovementMethod
             *          link detection action in onTouchEvent of the textView.
             */@OverridepublicbooleanonTouchEvent(MotionEvent event) {
                finalTextViewwidget= (TextView) this;
                finalObjecttext= widget.getText();
                if (text instanceof Spannable) {
                    finalSpannablebuffer= (Spannable) text;
                    finalintaction= event.getAction();

                    if (action == MotionEvent.ACTION_UP
                            || action == MotionEvent.ACTION_DOWN) {
                        intx= (int) event.getX();
                        inty= (int) event.getY();

                        x -= widget.getTotalPaddingLeft();
                        y -= widget.getTotalPaddingTop();

                        x += widget.getScrollX();
                        y += widget.getScrollY();

                        finalLayoutlayout= widget.getLayout();
                        finalintline= layout.getLineForVertical(y);
                        finalintoff= layout.getOffsetForHorizontal(line, x);

                        final ClickableSpan[] link = buffer.getSpans(off, off,
                                ClickableSpan.class);

                        if (link.length != 0) {
                            if (action == MotionEvent.ACTION_UP) {
                                link[0].onClick(widget);
                            } elseif (action == MotionEvent.ACTION_DOWN) {
                                Selection.setSelection(buffer,
                                        buffer.getSpanStart(link[0]),
                                        buffer.getSpanEnd(link[0]));
                            }
                            returntrue;
                        }
                    }

                }
                returnfalse;
            }

            @OverridepublicvoidsetText(CharSequence text, BufferType type) {
                super.setText(text, type);
                this.setMovementMethod(null);
            }
        }

Post a Comment for "Android - Listview Doesn't Receive Onitemclick For Textview With Clickable Links"