Discussion:
[mono-android] Underline text in TextView
DiCo
2012-04-12 11:38:53 UTC
Permalink
Hi,
I need to set underline text in my TextView dynamically in activity.cs
and i don't know how to do that...

I didn't find the exmaple for mono for android C#,
example on Java (like using SpannableString doesn't work)

Can anybody help ?

--
View this message in context: http://mono-for-android.1047100.n5.nabble.com/Underline-text-in-TextView-tp5635390p5635390.html
Sent from the Mono for Android mailing list archive at Nabble.com.
Jonathan Pryor
2012-04-12 15:43:01 UTC
Permalink
I didn't find the exmaple for mono for android C#, example on Java (like using SpannableString doesn't work)
What was your example Java code and equivalent C# code? I would expect that to work.

We have a sample that uses string formatting via resources, so not directly equivalent, but it does demonstrate display of formatted strings:

https://github.com/xamarin/monodroid-samples/tree/master/SkeletonApp
https://github.com/xamarin/monodroid-samples/blob/master/SkeletonApp/Resources/values/strings.xml#L27
https://github.com/xamarin/monodroid-samples/blob/master/SkeletonApp/SkeletonApp.cs#L31

Thanks,
- Jon
DiCo
2012-04-13 08:10:23 UTC
Permalink
Jon, thanks.

You gave me idea - and i found the way ))

Example that doesn't work in my case:

TextView textView = (TextView) view.findViewById(R.id.textview);
SpannableString content = new SpannableString("Content");
content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
textView.setText(content);
and
TextView textView = (TextView) view.findViewById(R.id.textview);
textView.setText(Html.fromHtml(getString(R.string.your_string_here)));
In my case TextView.SetText Method doesn't have any ovveride version with
ISpanned as parameter.
myTextView.TextFormatted = Html.FromHtml(MyString);
where MyString is like '<u>Underline Text</u>'




--
View this message in context: http://mono-for-android.1047100.n5.nabble.com/Underline-text-in-TextView-tp5635390p5637630.html
Sent from the Mono for Android mailing list archive at Nabble.com.
Jonathan Pryor
2012-04-13 16:06:38 UTC
Permalink
Post by DiCo
TextView textView = (TextView) view.findViewById(R.id.textview);
SpannableString content = new SpannableString("Content");
content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
textView.setText(content);
and
TextView textView = (TextView) view.findViewById(R.id.textview);
textView.setText(Html.fromHtml(getString(R.string.your_string_here)));
In my case TextView.SetText Method doesn't have any ovveride version with ISpanned as parameter.
A variation on your approach: the TextView.TextFormatted [0] property takes an ICharSequence, which ISpanned implements [1], so this should be valid:

using (SpannableString content = new SpannableString ("Content"))
using (UnderlineSpan underline = new UnderlineSpan ()) {
content.SetSpan(underline, 0, content.Length(), 0);
myTextView.TextFormatted = content;
}

- Jon

[0] http://androidapi.xamarin.com/?link=P%3aAndroid.Widget.TextView.TextFormatted
[1] http://androidapi.xamarin.com/?link=T:Android.Text.ISpanned

Loading...