Have Hyperlink Text In Textview
I want to have few text as hyperlink in my TextView like the following image: Also, similar to above Image I want the hyperlink to open the respective setting window. How is this
Solution 1:
Use SpannableString
for this
SpannableStringss=newSpannableString("Your string value");
ClickableSpanclickableTerms=newClickableSpan() {
@OverridepublicvoidonClick(View textView) {
// show toast here
}
@OverridepublicvoidupdateDrawState(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.
Solution 2:
Try this with the help of HTML anchor tag
TextView textView =(TextView)findViewById(R.id.textView);
textView.setClickable(true);
textView.setMovementMethod(LinkMovementMethod.getInstance());
String text = "No intenet connection. Make sure that <ahref='any url'> Wi-Fi</a> or <ahref='any url'> cellular mobile data is turned on</a>, then try again";
textView.setText(Html.fromHtml(text));
Solution 3:
Use the autoLink
property of TextView
<TextView
android:id="@+id/YOUR_TEXTVIEW"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="all"
/>
And Programatically u can create the hyper link below the text by the help of the SpannableString
like below example:-
SpannableString styledString
= new SpannableString("Large\n\n"// index 0 - 5+"Bold\n\n"// index 7 - 11+"Underlined\n\n"// index 13 - 23+"Italic\n\n"// index 25 - 31+"Strikethrough\n\n"// index 33 - 46+"Colored\n\n"// index 48 - 55+"Highlighted\n\n"// index 57 - 68+"K Superscript\n\n"// "Superscript" index 72 - 83 +"K Subscript\n\n"// "Subscript" index 87 - 96+"Url\n\n"// index 98 - 101+"Clickable\n\n"); // index 103 - 112// make the text twice as large
styledString.setSpan(new RelativeSizeSpan(2f), 0, 5, 0);
// make text bold
styledString.setSpan(new StyleSpan(Typeface.BOLD), 7, 11, 0);
// underline text
styledString.setSpan(new UnderlineSpan(), 13, 23, 0);
// make text italic
styledString.setSpan(new StyleSpan(Typeface.ITALIC), 25, 31, 0);
styledString.setSpan(new StrikethroughSpan(), 33, 46, 0);
// change text color
styledString.setSpan(new ForegroundColorSpan(Color.GREEN), 48, 55, 0);
// highlight text
styledString.setSpan(new BackgroundColorSpan(Color.CYAN), 57, 68, 0);
// superscript
styledString.setSpan(new SuperscriptSpan(), 72, 83, 0);
// make the superscript text smaller
styledString.setSpan(new RelativeSizeSpan(0.5f), 72, 83, 0);
// subscript
styledString.setSpan(new SubscriptSpan(), 87, 96, 0);
// make the subscript text smaller
styledString.setSpan(new RelativeSizeSpan(0.5f), 87, 96, 0);
// url
styledString.setSpan(new URLSpan("http://www.google.com"), 98, 101, 0);
// clickable textClickableSpan clickableSpan = new ClickableSpan() {
@Overridepublic void onClick(View widget) {
// We display a Toast. You could do anything you want here.Toast.makeText(YOUR_CONTEXT, "Clicked", Toast.LENGTH_SHORT).show();
}
};
Solution 4:
You can do that:
yourTextView.setText(Html.fromHtml("<bThis is normal text </b>" + "<a href=\"http://www.google.com\">This is cliclable text</a> "));
yourTextView.setMovementMethod(LinkMovementMethod.getInstance());
Also you can use Spannable String
with Clickable Span
.
Post a Comment for "Have Hyperlink Text In Textview"