Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Have HyperLink Text in TextView

I want to have few text as hyperlink in my TextView like the following image:

enter image description here

Also, similar to above Image I want the hyperlink to open the respective setting window. How is this possible?

EDIT: Just to make my question more specific, I want to open cellular setting when those text are clicked. So, I don't know what will be the URL for that to use href tag in String object.

Thanks in Advance!

like image 335
Hello World Avatar asked Jun 11 '26 20:06

Hello World


2 Answers

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.

like image 139
Sohail Zahid Avatar answered Jun 13 '26 10:06

Sohail Zahid


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 <a href='any url'> Wi-Fi</a> or <a href='any url'> cellular mobile data is turned on</a>, then try again";
textView.setText(Html.fromHtml(text));
like image 38
Reena Avatar answered Jun 13 '26 09:06

Reena