0

I have Xamarin.Android project and use MVVMCross. I need to add some underlining text in my .axml layout.

I found some ways to do it but its don't work for me.

I have this layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<include
    layout="@layout/toolbar" />
<TextView
    android:id="@+id/TextView01"
    android:textSize="16sp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_below="@+id/toolbar"
    local:MvxBind="Text Strings[Text01]" />
<TextView
    android:id="@+id/textLink"
    android:textSize="13sp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:textColor="#1565C0"
    android:layout_below="@id/TextView01"
    local:MvxBind="Text Strings[textLink]; Click MyCommand" />
</RelativeLayout>
  1. The string.

    <string name="your_string"><u>Underlined text</u></string>
    

I can't use this way because I use two languages and I have the folder Locales with two .txt files. So the project doesn't take strings from strings.xml

  1. Paint.

        TextView MyLink = FindViewById<TextView>(Resource.Id.MyLink);
        MyLink.PaintFlags = (MyLink.PaintFlags | Android.Graphics.PaintFlags.UnderlineText);
    
  2. FromHtml.

    txtView.setText(Html.fromHtml("<u>underlined</u> text"));
    

Doesn't work for me, besides I see the green underline and comment that this decision is deprecated.

How can I mark my TextView as underlined?

NaSt
  • 301
  • 1
  • 5
  • 16
  • Look at https://stackoverflow.com/questions/19046614/how-to-underline-text-in-textview-with-some-different-color-than-that-of-text – XH6 user Mar 21 '18 at 15:07

3 Answers3

2

Option 2 looks like Java instead of xamarin c#, try:

txtView.PaintFlags = (txtView.PaintFlags | Android.Graphics.PaintFlags.UnderlineText);

And use the correct Id.

FabriBertani
  • 1,606
  • 13
  • 24
1

Create own textView and use it instead of standart textView in axml

public class UnderlineTextView : TextView

private void Initialize()
{
    //this.PaintFlags = this.PaintFlags | Android.Graphics.PaintFlags.UnderlineText;
    //this.Text = "This text will be underlined";

    String underlineData = this.Text;
    SpannableString content = new SpannableString(underlineData);
    content.SetSpan(new UnderlineSpan(), 0, underlineData.Length, 0);
    // 0 specify start index and underlineData.length() specify end index of styling
    this.TextFormatted = content;
}
  • I'm trying to use it. I created the `UnderlineTextView.cs` inside `Helpers`. But I have underlined `UnderlineTextView` with the error: `CS1729 'TextView' does not contain a constructor that takes 0 arguments` – NaSt Mar 22 '18 at 05:16
  • When you inherit you need also inherit constructors from textView! – Andrii Panochyshyn Mar 22 '18 at 08:23
1

You should be using txtView.TextFormatted = Html.fromHtml("<u>underlined</u> text"); for it to work as you're expecting.

It's often ideal to use data binding when you're using the MVVM pattern. An approach using data binding with MvvmCross is to use a ValueConverter that accepts string input and outputs a SpannableString which you can use with the TextFormatted MvvmCross binding.

public class UnderlinedStringValueConverter : MvxValueConverter<string, SpannableString>
{
    protected override SpannableString Convert(string value, Type targetType, object parameter, CultureInfo culture)
    {
        var spannable = new SpannableString(value ?? string.Empty);
        spannable.SetSpan(new UnderlineSpan(), 0, spannable.Length(), 0);
        return spannable;
    }
}

Then in your TextView AXML:

<TextView
  ....
  local:MvxBind="TextFormatted UnderlinedString(Strings[textLink]);" />
Trevor Balcom
  • 3,766
  • 2
  • 32
  • 51