'How to recognize and select specific html element in Android?

in my Android application I am showing the result from a Website using API. However, this result is retrieved in html form. For example, the API sends a simple "Hello World" text in this form <p>Hello World</p> or a URL link in this form <a class="ps-media__link ps-media-link" href="https://www.example.com/" rel="nofollow noopener" target="_blank">https://www.example.com/</a> or it may include both <p></p> and <a></a> tag inside. So, what I am trying to do is to check if the result is URL and if it is URL then it should be clickable and open with Webview(like it happens on Instagram when the user clicks on the link and shows up the website). Here is what I have tried in my code:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            holder.postTxt.setText(Html.fromHtml(post.getPost(), Html.FROM_HTML_MODE_COMPACT));
        } else {
            holder.postTxt.setText(Html.fromHtml(post.getPost()));
        }

EDIT: Here is the Textview inside the xml file:

<TextView
            android:id="@+id/post_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="25dp"
            android:layout_marginTop="10dp"
            android:layout_marginRight="25dp"
            android:text="Hello World"
            android:textColor="@color/black"
            android:textSize="15sp"
            android:textColorLink="@color/purple_500"
            android:clickable="true"/>

This method recognizes what type of html element is but it isn't clickable when it comes to links. SO, what should I do in order to make the textview clickable ONLY when it is a link? I am using Android Studio. Any answer will be appreciated, thanks in advance!!



Solution 1:[1]

set LinkMovementMethod for your TextView

textView.setMovementMethod(LinkMovementMethod.getInstance());

be aware that Html.fromHtml isn't handling all HTML tags, only few basic of them. some list and examples in HERE. if you need more complex html parsing/drawing/handling just use WebView and one of setData methods

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 snachmsm