How do I make links in a TextView clickable?
A1 .
Linkify.addLinks(TextView, Linkify.ALL);
A2.In Xml
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autoLink="web"/>
A3. In Xml
<TextView
android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/txtCredits" android:id="@+id/infoTxtCredits"
android:autoLink="all"
android:linksClickable="true">
</TextView>
A4. For all phone number,email,...
The only complete solution to have EVERYTHING clickable that I have found is the following:
Spanned text = Html.fromHtml(myString);
URLSpan[] currentSpans = text.getSpans(0, text.length(), URLSpan.class);
SpannableString buffer = new SpannableString(text);
Linkify.addLinks(buffer, Linkify.ALL);
for (URLSpan span : currentSpans) {
int end = text.getSpanEnd(span);
int start = text.getSpanStart(span);
buffer.setSpan(span, start, end, 0);
}
textView.setText(buffer);
textView.setMovementMethod(LinkMovementMethod.getInstance());
And the TextView should NOT have android:autolink
. There's no need for android:linksClickable="true"
either; it's true by default.A5.one line code
TextView textView = (TextView)findViewById(R.id.textView1);
textView.setText("some url is www.google.com phone 7504567890 another url lkgndflg.com ");
// Makes the textView's Phone and URL (hyperlink) select and go.
Linkify.addLinks(textView, Linkify.WEB_URLS | Linkify.PHONE_NUMBERS);
Comments
Post a Comment