Android: textview hyperlink
You could have two separate TextViews and you could align them accordingly in your layout if needed:
Text1.setText(
Html.fromHtml(
"<a href=\"http://www.google.com\">google</a> "));
Text1.setMovementMethod(LinkMovementMethod.getInstance());
Text2.setText(
Html.fromHtml(
"<a href=\"http://www.stackoverflow.com\">stackoverflow</a> "));
Text2.setMovementMethod(LinkMovementMethod.getInstance());
Then if you want to strip the "link underline". Create a class:
public class URLSpanNoUnderline extends URLSpan {
public URLSpanNoUnderline(String url) {
super(url);
}
@Override public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setUnderlineText(false);
}
}
Then add this method in your main Activity class where you have the TextViews
private void stripUnderlines(TextView textView) {
Spannable s = new SpannableString(textView.getText());
URLSpan[] spans = s.getSpans(0, s.length(), URLSpan.class);
for (URLSpan span: spans) {
int start = s.getSpanStart(span);
int end = s.getSpanEnd(span);
s.removeSpan(span);
span = new URLSpanNoUnderline(span.getURL());
s.setSpan(span, start, end, 0);
}
textView.setText(s);
}
And then just call this after you initialised the TextViews (in your onCreate):
stripUnderlines(Text1);
stripUnderlines(Text2);
Make a hyperlink textview in android
Try this, and let me know what happen..
Using java code:
TextView textView =(TextView)findViewById(R.id.textView);
textView.setClickable(true);
textView.setMovementMethod(LinkMovementMethod.getInstance());
String text = "<a href='http://www.google.com'> Google </a>";
textView.setText(Html.fromHtml(text));
From API level >= 24 onwards Html.fromHtml(String source)
is deprecated instead use fromHtml(String, int)
,
textView.setText(Html.fromHtml(text, Html.FROM_HTML_MODE_COMPACT));
Or in layout xml file, inside your TextView widget attributes
android:autoLink="web"
android:linksClickable="true"
How to make links in a TextView clickable
Buried in the API demos, I found the solution to my problem:
File Link.java:
// text2 has links specified by putting <a> tags in the string
// resource. By default these links will appear but not
// respond to user input. To make them active, you need to
// call setMovementMethod() on the TextView object.
TextView t2 = (TextView) findViewById(R.id.text2);
t2.setMovementMethod(LinkMovementMethod.getInstance());
I removed most of the attributes on my TextView to match what was in the demo.
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/txtCredits"/>
That solved it. It is pretty difficult to uncover and fix.
Important: Don't forget to remove autoLink="web"
if you are calling setMovementMethod()
.
Have HyperLink Text in TextView
Use SpannableString
for this
SpannableString ss = new SpannableString("Your string value");
ClickableSpan clickableTerms = new ClickableSpan() {
@Override
public void onClick(View textView) {
// show toast here
}
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setUnderlineText(true);
}
};
ss.setSpan(clickableTerms, 4, 10, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
myTextView.setText(ss);
myTextView.setMovementMethod(LinkMovementMethod.getInstance());
myTextView.setHighlightColor(Color.TRANSPARENT);
You can make multiple words clickable by this method.
Part of TextView clickable with a link
check below point before testing
- Check your link starts with
http://
orhttps://
- Place your String in CDATA tag (like
<string name="some_text"><![CDATA[PLACE_YOUR_HTML_STRING_HERE]]></string>
) - Check you have added internet Permission in your
manifest.xml
file<uses-permission android:name="android.permission.INTERNET"/>
- then dynamically html text can be set using below code into textView
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
textView.setText(Html.fromHtml(getString(R.string.html_string), Html.FROM_HTML_MODE_COMPACT));
} else {
textView.setText(Html.fromHtml(getString(R.string.html_string)));
}
Linkify.addLinks(textView, Linkify.ALL);
textView.setMovementMethod(LinkMovementMethod.getInstance());
Android TextView with Clickable Links: how to capture clicks?
Based upon another answer, here's a function setTextViewHTML() which parses the links out of a HTML string and makes them clickable, and then lets you respond to the URL.
protected void makeLinkClickable(SpannableStringBuilder strBuilder, final URLSpan span)
{
int start = strBuilder.getSpanStart(span);
int end = strBuilder.getSpanEnd(span);
int flags = strBuilder.getSpanFlags(span);
ClickableSpan clickable = new ClickableSpan() {
public void onClick(View view) {
// Do something with span.getURL() to handle the link click...
}
};
strBuilder.setSpan(clickable, start, end, flags);
strBuilder.removeSpan(span);
}
protected void setTextViewHTML(TextView text, String html)
{
CharSequence sequence = Html.fromHtml(html);
SpannableStringBuilder strBuilder = new SpannableStringBuilder(sequence);
URLSpan[] urls = strBuilder.getSpans(0, sequence.length(), URLSpan.class);
for(URLSpan span : urls) {
makeLinkClickable(strBuilder, span);
}
text.setText(strBuilder);
text.setMovementMethod(LinkMovementMethod.getInstance());
}
TextView link text is not visible
The cause might be the equality of text color and background. Try to set another color to the links using:
android:textColorLink="@color/someColor"
Android Textview with clickable email and link
I think you need to set the android:linksClickable="true"
attribute.
If set to false, keeps the movement method from being set to the link movement method even if autoLink causes links to be found.
Set the sms_deposit_information
in strings.xml in the form of
<string name="sms_deposit_information"><![CDATA[<p>abc@xyz.com</p>]]></string>
Then do the below programmatically
TextView textView = findViewById(R.id.description);
textView.setText(Html.fromHtml(getString(R.string.sms_deposit_information)));
textView.setMovementMethod(LinkMovementMethod.getInstance());
Related Topics
How to Downgrade Android Emulator
How to Open an Android Studio Project in Eclipse
Error Strictmode$Androidblockguardpolicy.Onnetwork
Realmchangelistener Does Not Get Called When Realm Gets Updated in Notificationlistenerservice
Activity Declaration in Androidmanifest.Xml
Googlesignatureverifier Signature Not Valid Message (Google Play Services 9.0.0)
Error: Status{Statuscode=Developer_Error, Resolution=Null}
How to Open a PDF via Intent from Sd Card
How to Set Time to Device Programmatically
Is There Any Limit of Bundle in Android
How to Get Available Wifi Networks and Display Them in a List in Android
How to Scale/Resize Text to Fit a Textview
Geocoder.Getfromlocation Throws Ioexception on Android Emulator
How to Get Sms Sent Confirmation for Each Contact/Person in Android