SufficientlySecure / html-textview

TextView to display simple HTML
Apache License 2.0
2.56k stars 512 forks source link

Cardview not clickable with HtmlTextView as Child #157

Open SkyGanfor opened 5 years ago

SkyGanfor commented 5 years ago

I have a simple cardview that contains a htmltextview. But upon compiling, I'm not able to click the cardview and the htmltextview is in focus instead. I tried defining the htmltextview as not clickable or focusable, but to no success. Is there a way to click a cardview that contains the htmltextview? (htmltextview matches cardviews width and height).

<androidx.cardview.widget.CardView xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/newscenter_cardView"
        android:layout_width="match_parent"
        android:layout_height="299dp"
        android:foreground="?android:attr/selectableItemBackground"
        app:cardBackgroundColor="@android:color/white"
        app:cardMaxElevation="1dp"
        app:cardPreventCornerOverlap="true"
        app:cardUseCompatPadding="true"
        app:contentPadding="10dp"
        app:contentPaddingBottom="0dp">

            <org.sufficientlysecure.htmltextview.HtmlTextView
                android:id="@+id/newscenter_htmlview"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:clickable="false"
                android:focusable="false"
                android:textAppearance="@android:style/TextAppearance.Small" />

    </androidx.cardview.widget.CardView>
davidmaulick002 commented 5 years ago

I am having a similar issue. The HtmlText does not allow for duplicateParentState=True attribute so allow for clickable

johnnyfivedev commented 5 years ago

I have the same problem. It's due to setMovementMethod(..) being called in setHtml(..) in source code. If I got it right this call makes every HtmlTextView you use clickable by default therefore makes it intercept click events.

public void setHtml(@NonNull String html, @Nullable Html.ImageGetter imageGetter) {
        final HtmlTagHandler htmlTagHandler = new HtmlTagHandler(getPaint());
        htmlTagHandler.setClickableTableSpan(clickableTableSpan);
        htmlTagHandler.setDrawTableLinkSpan(drawTableLinkSpan);
        htmlTagHandler.setListIndentPx(indent);

        html = htmlTagHandler.overrideTags(html);

        if (removeTrailingWhiteSpace) {
            setText(removeHtmlBottomPadding(Html.fromHtml(html, imageGetter, htmlTagHandler)));
        } else {
            setText(Html.fromHtml(html, imageGetter, htmlTagHandler));
        }

        // make links work
        setMovementMethod(LocalLinkMovementMethod.getInstance());
    }

Solution was to use setMovementMethod(null); after setHtml().

StackOverflow answer

letuananh035 commented 4 years ago

You must disable forcus cardview, set setOnTouchListener in htmlTextView with call onTouchEvent of CardView

htmlTextView.setOnTouchListener((v, event) -> {
                    itemView.findViewById(R.id.layout_child).onTouchEvent(event);
                    return false;
 });