Example usage for android.text Spanned length

List of usage examples for android.text Spanned length

Introduction

In this page you can find the example usage for android.text Spanned length.

Prototype

int length();

Source Link

Document

Returns the length of this character sequence.

Usage

From source file:Main.java

public static CharSequence fixHtml(String brokenHtml) {
    // There's some terrible bug that displays all the text as the 
    // opening tag if a tag is the first thing in the string
    // so we hack around it so it begins with something else
    // when we convert it

    // terrible hack, just add some chars
    Spanned html = Html.fromHtml("aa" + brokenHtml);
    // after we have the good html, remove the chars
    CharSequence fixedText = html.subSequence(2, html.length());
    return fixedText;
}

From source file:com.googlecode.eyesfree.brailleback.DisplaySpans.java

/**
 * Returns a span in {@code spanned} that is {@link Object#equals}
 * to {@code obj}./*from w  w  w .j  av a2 s  . co m*/
 */
public static Object getEqualSpan(Spanned spanned, Object obj) {
    Object[] spans = spanned.getSpans(0, spanned.length(), obj.getClass());
    for (Object span : spans) {
        if (obj.equals(span)) {
            return span;
        }
    }
    return null;
}

From source file:Main.java

private static Object getLast(Spanned text, Class kind) {
    /*//from www . ja v a  2s.c o  m
     * This knows that the last returned object from getSpans()
     * will be the most recently added.
     */
    Object[] objs = text.getSpans(0, text.length(), kind);

    if (objs.length == 0) {
        return null;
    } else {
        return objs[objs.length - 1];
    }
}

From source file:Main.java

/**
 * @see android.text.Html/* w ww  .  jav  a 2 s.com*/
 */
private static Object getLast(Spanned text, Class<?> kind) {
    /*
     * This knows that the last returned object from getSpans()
     * will be the most recently added.
     */
    Object[] objs = text.getSpans(0, text.length(), kind);
    if (objs.length == 0) {
        return null;
    }
    return objs[objs.length - 1];
}

From source file:com.googlecode.eyesfree.brailleback.DisplaySpans.java

/**
 * Utility function to log what accessibiility nodes are attached
 * to what parts of the character sequence.
 *//*  w  ww  .  java  2s  .co m*/
public static void logNodes(CharSequence chars) {
    if (!(chars instanceof Spanned)) {
        LogUtils.log(DisplaySpans.class, Log.VERBOSE, "Not a Spanned");
        return;
    }
    Spanned spanned = (Spanned) chars;
    AccessibilityNodeInfoCompat spans[] = spanned.getSpans(0, spanned.length(),
            AccessibilityNodeInfoCompat.class);
    for (AccessibilityNodeInfoCompat node : spans) {
        LogUtils.log(DisplaySpans.class, Log.VERBOSE,
                chars.subSequence(spanned.getSpanStart(node), spanned.getSpanEnd(node)).toString());
        LogUtils.log(DisplaySpans.class, Log.VERBOSE, node.getInfo().toString());
    }
}

From source file:com.keylesspalace.tusky.util.LinkHelper.java

/**
 * Finds links, mentions, and hashtags in a piece of text and makes them clickable, associating
 * them with callbacks to notify when they're clicked.
 *
 * @param view the returned text will be put in
 * @param content containing text with mentions, links, or hashtags
 * @param mentions any '@' mentions which are known to be in the content
 * @param listener to notify about particular spans that are clicked
 *///from w w w  .j a  v a 2  s .co m
public static void setClickableText(TextView view, Spanned content, @Nullable Status.Mention[] mentions,
        final LinkListener listener) {

    SpannableStringBuilder builder = new SpannableStringBuilder(content);
    URLSpan[] urlSpans = content.getSpans(0, content.length(), URLSpan.class);
    for (URLSpan span : urlSpans) {
        int start = builder.getSpanStart(span);
        int end = builder.getSpanEnd(span);
        int flags = builder.getSpanFlags(span);
        CharSequence text = builder.subSequence(start, end);
        if (text.charAt(0) == '#') {
            final String tag = text.subSequence(1, text.length()).toString();
            ClickableSpan newSpan = new ClickableSpan() {
                @Override
                public void onClick(View widget) {
                    listener.onViewTag(tag);
                }

                @Override
                public void updateDrawState(TextPaint ds) {
                    super.updateDrawState(ds);
                    ds.setUnderlineText(false);
                }
            };
            builder.removeSpan(span);
            builder.setSpan(newSpan, start, end, flags);
        } else if (text.charAt(0) == '@' && mentions != null && mentions.length > 0) {
            String accountUsername = text.subSequence(1, text.length()).toString();
            /* There may be multiple matches for users on different instances with the same
             * username. If a match has the same domain we know it's for sure the same, but if
             * that can't be found then just go with whichever one matched last. */
            String id = null;
            for (Status.Mention mention : mentions) {
                if (mention.localUsername.equalsIgnoreCase(accountUsername)) {
                    id = mention.id;
                    if (mention.url.contains(getDomain(span.getURL()))) {
                        break;
                    }
                }
            }
            if (id != null) {
                final String accountId = id;
                ClickableSpan newSpan = new ClickableSpan() {
                    @Override
                    public void onClick(View widget) {
                        listener.onViewAccount(accountId);
                    }

                    @Override
                    public void updateDrawState(TextPaint ds) {
                        super.updateDrawState(ds);
                        ds.setUnderlineText(false);
                    }
                };
                builder.removeSpan(span);
                builder.setSpan(newSpan, start, end, flags);
            }
        } else {
            ClickableSpan newSpan = new CustomURLSpan(span.getURL());
            builder.removeSpan(span);
            builder.setSpan(newSpan, start, end, flags);
        }
    }
    view.setText(builder);
    view.setLinksClickable(true);
    view.setMovementMethod(LinkMovementMethod.getInstance());
}

From source file:com.zulip.android.util.CustomHtmlToSpannedConverter.java

/**
<<<<<<< e370c9bc00e8f6b33b1d12a44b4c70a7f063c8b9
 * Parses Spanned text for existing html links and reapplies them after the text has been Linkified
=======//from w ww .jav a 2 s.co  m
 * Parses Spanned text for existing html links and reapplies them.
>>>>>>> Issue #168 bugfix for links not being clickable, adds autoLink feature including web, phone, map and email
 * @see <a href="https://developer.android.com/reference/android/text/util/Linkify.html">Linkify</a>
 *
 * @param spann
 * @param mask bitmask to define which kinds of links will be searched and applied (e.g. <a href="https://developer.android.com/reference/android/text/util/Linkify.html#ALL">Linkify.ALL</a>)
 * @return
 */
public static Spanned linkifySpanned(@NonNull final Spanned spann, final int mask) {
    URLSpan[] existingSpans = spann.getSpans(0, spann.length(), URLSpan.class);
    List<Pair<Integer, Integer>> links = new ArrayList<>();

    for (URLSpan urlSpan : existingSpans) {
        links.add(new Pair<>(spann.getSpanStart(urlSpan), spann.getSpanEnd(urlSpan)));
    }

    Linkify.addLinks((Spannable) spann, mask);

    // add the links back in
    for (int i = 0; i < existingSpans.length; i++) {
        ((Spannable) spann).setSpan(existingSpans[i], links.get(i).first, links.get(i).second,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    return spann;
}

From source file:com.zulip.android.util.CustomHtmlToSpannedConverter.java

private static Object getLast(Spanned text, Class kind) {
    /*/*from  w  w  w  .j  ava 2s.co m*/
     * This knows that the last returned object from getSpans() will be the
     * most recently added.
     */
    Object[] objs = text.getSpans(0, text.length(), kind);

    if (objs.length == 0) {
        return null;
    } else {
        return objs[objs.length - 1];
    }
}

From source file:com.googlecode.eyesfree.brailleback.DisplayManager.java

private static int findCursorPosition(Content content) {
    Spanned spanned = content.getSpanned();
    if (spanned == null) {
        return -1;
    }//from w ww .  j  av  a 2 s  .  c om
    DisplaySpans.SelectionSpan[] selectionSpans = spanned.getSpans(0, spanned.length(),
            DisplaySpans.SelectionSpan.class);
    if (selectionSpans.length > 0) {
        return spanned.getSpanStart(selectionSpans[0]);
    }
    DisplaySpans.FocusSpan[] focusSpans = spanned.getSpans(0, spanned.length(), DisplaySpans.FocusSpan.class);
    if (focusSpans.length > 0) {
        return spanned.getSpanStart(focusSpans[0]);
    }
    return -1;
}

From source file:com.google.android.apps.common.testing.accessibility.framework.ClickableSpanInfoCheck.java

@Override
public List<AccessibilityInfoCheckResult> runCheckOnInfo(AccessibilityNodeInfo info, Context context,
        Bundle metadata) {//w  w w . j  av a  2s  . c om
    List<AccessibilityInfoCheckResult> results = new ArrayList<AccessibilityInfoCheckResult>(1);
    AccessibilityNodeInfoCompat compatInfo = new AccessibilityNodeInfoCompat(info);
    if (AccessibilityNodeInfoUtils.nodeMatchesAnyClassByType(context, compatInfo, TextView.class)) {
        if (info.getText() instanceof Spanned) {
            Spanned text = (Spanned) info.getText();
            ClickableSpan[] clickableSpans = text.getSpans(0, text.length(), ClickableSpan.class);
            for (ClickableSpan clickableSpan : clickableSpans) {
                if (clickableSpan instanceof URLSpan) {
                    String url = ((URLSpan) clickableSpan).getURL();
                    if (url == null) {
                        results.add(new AccessibilityInfoCheckResult(this.getClass(),
                                AccessibilityCheckResultType.ERROR, "URLSpan has null URL", info));
                    } else {
                        Uri uri = Uri.parse(url);
                        if (uri.isRelative()) {
                            // Relative URIs cannot be resolved.
                            results.add(new AccessibilityInfoCheckResult(this.getClass(),
                                    AccessibilityCheckResultType.ERROR,
                                    "URLSpan should not contain relative links", info));
                        }
                    }
                } else { // Non-URLSpan ClickableSpan
                    results.add(new AccessibilityInfoCheckResult(this.getClass(),
                            AccessibilityCheckResultType.ERROR,
                            "URLSpan should be used in place of ClickableSpan for improved accessibility",
                            info));
                }
            }
        }
    } else {
        results.add(new AccessibilityInfoCheckResult(this.getClass(), AccessibilityCheckResultType.NOT_RUN,
                "View must be a TextView", info));
    }
    return results;
}