Skip to content
This repository has been archived by the owner on Sep 7, 2020. It is now read-only.

Cardview not clickable with HtmlTextView as Child #157

Open
SkyGanfor opened this issue Feb 18, 2019 · 3 comments
Open

Cardview not clickable with HtmlTextView as Child #157

SkyGanfor opened this issue Feb 18, 2019 · 3 comments

Comments

@SkyGanfor
Copy link

SkyGanfor commented Feb 18, 2019

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
Copy link

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

@johnnyfivedev
Copy link

johnnyfivedev commented Mar 4, 2019

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
Copy link

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants