简体   繁体   中英

How to make a anchor tag clickable which is inside paragraph tag?

Below is my string

String txtLink = "<p>Apply directly in our <a 
href=\"https://theorem.applytojob.com/apply/TyJy6YCsOs/Experienced- 
Backend-Engineer-Ruby?source=GitHub+Jobs\">careers page</a></p>";

I need the anchor tag to be clickable in Android. What I have tried so far is

txtJobLink.setText(""+Html.fromHtml(txtLink));
txtJobLink.setMovementMethod(BetterLinkMovementMethod.newInstance());
BetterLinkMovementMethod.linkify(Linkify.ALL,txtJobLink);
BetterLinkMovementMethod.linkifyHtml(txtJobLink);
txtJobLink.setLinksClickable(true);

If you have added added autoLink and linksClickable properties in TextView in your xml file then please remove it.

And Change your Activity code like:

String txtLink = "<p>Apply directly in our <a href=\"https://theorem.applytojob.com/apply/TyJy6YCsOs/Experienced-Backend-Engineer-Ruby?source=GitHub+Jobs\">careers page</a></p>";

TextView txtJobLink = findViewById(R.id.txtJobLink);
txtJobLink.setMovementMethod(LinkMovementMethod.getInstance());

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    txtJobLink.setText(Html.fromHtml(txtLink, Html.FROM_HTML_MODE_LEGACY));
} else {
    txtJobLink.setText(Html.fromHtml(txtLink));
}

So now your layout will be like:

在此输入图像描述

When you will click on "careers page", it will open associated url in web browser.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM