1

Native Android Spanned.getSpans(......,SyleSpan.class) function return type StyleSpan[]

Xamarin ISpanned.GetSpans(......) function returns type Java.lang.Object[] though it returns <T> (T=StyleSpan in my case) in native android. Therefore there is a loss of information since the Mono interface doesn't expose what it would have been exposed if I had used the native SDK.

Since propery Style (getStyle() in native android) is only available in StyleSpan there is no way to read that a given StyleSpan read through GetSpans is bold or italic.

Any ideas how I determine bold or italic?

Is this a limitation in the mono interface?

SadeepDarshana
  • 1,057
  • 18
  • 34

1 Answers1

4

You can do everything. ;) There is just no comfortable generic wrapper for the GetSpans method.

ISpanned ss = ...;
var spans = ss.GetSpans(0, 20, Class.FromType(typeof(SyleSpan)));
foreach (SyleSpan span in spans)
{
    // do what you want
    if(span.Style == TypefaceStyle.Bold)
    {
        Debug.WriteLine("Xamarin can find bold spans, too :)");
    }
}

if you want to access it generic:

public static class ISpannedExtension
{
    public static TSpan[] GetSpans<TSpan>(this ISpanned ss, int startIndex, int length)
    {
        return ss.GetSpans(startIndex, length, Class.FromType(typeof(TSpan)))
            .Cast<TSpan>()
            .ToArray();
    }
}

// usage
ISpanned ss = ...;
var spans = ss.GetSpans<SyleSpan>(0, 20);   
Sven-Michael Stübe
  • 14,560
  • 4
  • 52
  • 103
  • working perfectly thanks. I actually tried to cast this (which should work), but instead casting each Java.Lang.Object into StyleSpan I had casted array Object[] into StyleSpan[] which obviously didn't work. So when casting fails I thought they are fundamentally Lang.Objects rather than StyleSpans. Only realized the mistake after reading this answer. – SadeepDarshana Jun 28 '16 at 18:49