I need to make a TextView as hyperlink but with the custom colour. How can it possible?

How to work with TextFormatted property of a TextView control?
I am working with a TextView control in android Xmarine c# project. Here I need to make a textview as hyperlink to open another page. I have given text colour as orange for that TextView. But If I use the below mentioned code for the Textview Control, the colour turned into blue automatically.

string DATA = "https://forums.xamarin.com";
TextView.TextFormatted = Html.FromHtml("<a href=" + DATA + ">" + "ABCD" + "</a>");
TextView.MovementMethod = LinkMovementMethod.Instance;

Here I need to make a textview as hyperlink but with the custom colour. How can it possible?

Best Answer

Answers

  • soumenhaldersoumenhalder Member ✭✭

    @AmmarMheir said:
    in Xamarin.Android, you can set the Hyperlink color directly within the TextView object via:
    TextView.SetLinkTextColor(Android.Graphics.Color.Red);
    This changes the Hyperlink color to red for example.

    https://developer.xamarin.com/api/member/Android.Widget.TextView.SetLinkTextColor/p/Android.Graphics.Color/

    Thank you @AmmarMheir
    this is working perfect. Can you please help me with one more thing , i.e. Colour is coming Orange now(As i made it as orange). but the text is coming with an underline.

    Can we remove this underline from code behind for TextView control, as I don't want to give underline.

  • JustinTothJustinToth USMember ✭✭

    @soumenhalder Here is how you remove the underline:

    1. Create a custom span class to handle stripping out the underlines.

      class URLSpanNoUnderline : URLSpan {
              public URLSpanNoUnderline (string url) : base (url) {
              }
      
              public override void UpdateDrawState (TextPaint ds) {
                  base.UpdateDrawState (ds);
                  ds.UnderlineText = false;
              }
          }
      
    2. Create an extension method to find all url spans and replace them with our custom spans.

      public static void StripUnderlinesFromLinks (this TextView textView) {
                  var spannable = new SpannableStringBuilder (textView.TextFormatted);
                  var spans = spannable.GetSpans (0, spannable.Length (), Java.Lang.Class.FromType (typeof (URLSpan)));
                  foreach (URLSpan span in spans) {
                      var start = spannable.GetSpanStart (span);
                      var end = spannable.GetSpanEnd (span);
                      spannable.RemoveSpan(span);
                      var newSpan = new URLSpanNoUnderline (span.URL);
                      spannable.SetSpan(newSpan, start, end, 0);
                  }
                  textView.TextFormatted = spannable;
              }
      
  • LearnEverythingLearnEverything USMember ✭✭✭

    To create hyperlink is just a few line of code no need xml

     TextView uriText = new TextView(this);
                uriText.TextFormatted = HtmlCompat.FromHtml("<a href=\"http://www.google.com\">google</a>", HtmlCompat.FromHtmlModeLegacy);
                uriText.MovementMethod = new Android.Text.Method.LinkMovementMethod();
    
                var layout = FindViewById<LinearLayout>(Resource.Id.rootAbout);
                layout.AddView(uriText);
    

    Result

Sign In or Register to comment.