Example usage for android.text SpannableStringBuilder length

List of usage examples for android.text SpannableStringBuilder length

Introduction

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

Prototype

public int length() 

Source Link

Document

Return the number of chars in the buffer.

Usage

From source file:com.liferay.mobile.screens.viewsets.westeros.auth.signup.SignUpView.java

private void initClickableTermsAndConditions() {
    TextView textView = (TextView) findViewById(R.id.terms);
    textView.setMovementMethod(LinkMovementMethod.getInstance());

    SpannableStringBuilder ssb = new SpannableStringBuilder("I accept the terms and conditions");

    ssb.setSpan(new ClickableSpan() {
        @Override/*from   w  ww.  j  ava2 s. c  om*/
        public void onClick(View widget) {
            SignUpScreenlet signUpScreenlet = getSignUpScreenlet();
            signUpScreenlet.performUserAction(SignUpScreenlet.TERMS_AND_CONDITIONS);
        }
    }, 13, ssb.length(), 0);

    ssb.setSpan(new StyleSpan(Typeface.BOLD), 13, ssb.length(), 0);
    ssb.setSpan(new ForegroundColorSpan(ContextCompat.getColor(getContext(), android.R.color.white)), 13,
            ssb.length(), 0);

    textView.setText(ssb, TextView.BufferType.SPANNABLE);
}

From source file:com.android.screenspeak.formatter.EventSpeechRule.java

/**
 * Returns the text content of a given <code>node</code> by performing a
 * preorder traversal of the tree rooted at that node. </p> Note: Android
 * Java implementation is not compatible with Java 5.0 which provides such a
 * method.// w ww  .ja va  2  s.c o m
 *
 * @param node The node.
 * @return The text content.
 */
private static String getTextContent(Node node) {
    SpannableStringBuilder builder = sTempBuilder;
    getTextContentRecursive(node, builder);
    String text = builder.toString();
    builder.delete(0, builder.length());
    return text;
}

From source file:com.nttec.everychan.ui.presentation.HtmlParser.java

private static void startSpan(SpannableStringBuilder text, Attributes attributes) {
    String style = attributes.getValue("", "style");
    String classAttr = attributes.getValue("", "class");
    boolean isAibquote = classAttr != null && (classAttr.equals("unkfunc") || classAttr.equals("quote"));
    boolean isAibspoiler = classAttr != null && classAttr.equals("spoiler");
    boolean isUnderline = classAttr != null && classAttr.equals("u");
    boolean isStrike = classAttr != null && classAttr.equals("s");

    int len = text.length();
    text.setSpan(new Span(style, isAibquote, isAibspoiler, isUnderline, isStrike), len, len,
            Spannable.SPAN_MARK_MARK);//  www . java2 s.  c  o m
}

From source file:com.tct.mail.browse.SendersView.java

public static SpannableStringBuilder createMessageInfo(Context context, Conversation conv,
        final boolean resourceCachingRequired) {
    SpannableStringBuilder messageInfo = new SpannableStringBuilder();

    try {//from  w w  w  .  j av a2s. com
        ConversationInfo conversationInfo = conv.conversationInfo;
        int sendingStatus = conv.sendingState;
        boolean hasSenders = false;
        // This covers the case where the sender is "me" and this is a draft
        // message, which means this will only run once most of the time.
        for (ParticipantInfo p : conversationInfo.participantInfos) {
            if (!TextUtils.isEmpty(p.name)) {
                hasSenders = true;
                break;
            }
        }
        getSenderResources(context, resourceCachingRequired);
        int count = conversationInfo.messageCount;
        int draftCount = conversationInfo.draftCount;
        if (count > 1) {
            messageInfo.append(count + "");
        }
        messageInfo.setSpan(
                CharacterStyle.wrap(conv.read ? sMessageInfoReadStyleSpan : sMessageInfoUnreadStyleSpan), 0,
                messageInfo.length(), 0);
        if (draftCount > 0) {
            // If we are showing a message count or any draft text and there
            // is at least 1 sender, prepend the sending state text with a
            // comma.
            if (hasSenders || count > 1) {
                messageInfo.append(sSendersSplitToken);
            }
            SpannableStringBuilder draftString = new SpannableStringBuilder();
            if (draftCount == 1) {
                draftString.append(sDraftSingularString);
            } else {
                draftString.append(sDraftPluralString)
                        .append(String.format(sDraftCountFormatString, draftCount));
            }
            draftString.setSpan(CharacterStyle.wrap(sDraftsStyleSpan), 0, draftString.length(),
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            messageInfo.append(draftString);
        }
        // TS: chao.zhang 2015-09-29 EMAIL FEATURE-585337 MOD_S
        //NOTE:currently,we DONOT show the status in senderView, with our ERGO,show the status on the side if Subject.diable it!!!
        //boolean showState = sendingStatus == UIProvider.ConversationSendingState.SENDING ||
        //        sendingStatus == UIProvider.ConversationSendingState.RETRYING ||
        //       sendingStatus == UIProvider.ConversationSendingState.SEND_ERROR;
        boolean showState = false;
        // TS: chao.zhang 2015-09-29 EMAIL FEATURE-585337 MOD_E
        if (showState) {
            // If we are showing a message count or any draft text, prepend
            // the sending state text with a comma.
            if (count > 1 || draftCount > 0) {
                messageInfo.append(sSendersSplitToken);
            }

            SpannableStringBuilder stateSpan = new SpannableStringBuilder();

            if (sendingStatus == UIProvider.ConversationSendingState.SENDING) {
                stateSpan.append(sSendingString);
                stateSpan.setSpan(sSendingStyleSpan, 0, stateSpan.length(), 0);
            } else if (sendingStatus == UIProvider.ConversationSendingState.RETRYING) {
                stateSpan.append(sQueuedString);
                stateSpan.setSpan(sQueuedStyleSpan, 0, stateSpan.length(), 0);
            } else if (sendingStatus == UIProvider.ConversationSendingState.SEND_ERROR) {
                stateSpan.append(sFailedString);
                stateSpan.setSpan(sFailedStyleSpan, 0, stateSpan.length(), 0);
            }
            messageInfo.append(stateSpan);
        }

        // Prepend a space if we are showing other message info text.
        if (count > 1 || (draftCount > 0 && hasSenders) || showState) {
            messageInfo.insert(0, sMessageCountSpacerString);
        }
    } finally {
        if (!resourceCachingRequired) {
            clearResourceCache();
        }
    }

    return messageInfo;
}

From source file:com.vuze.android.remote.spanbubbles.SpanTags.java

public void setTagBubbles(final SpannableStringBuilder ss, String text, String token) {
    if (ss.length() > 0) {
        // hack to ensure descent is always added by TextView
        ss.append("\u200B");
    }/*from  www . j a  v  a  2 s.  c o m*/

    if (tvTags == null) {
        Log.e(TAG, "no tvTags");
        return;
    }

    TextPaint p = tvTags.getPaint();

    // Start and end refer to the points where the span will apply
    int tokenLen = token.length();
    int base = 0;

    if (showIcon && tagDrawables == null) {
        createDrawTagables();
    }

    while (true) {
        int start = text.indexOf(token, base);
        int end = text.indexOf(token, start + tokenLen);

        if (start < 0 || end < 0) {
            break;
        }

        base = end + tokenLen;

        final int fSpanStart = start;
        final int fSpanEnd = end + tokenLen;

        String id = text.substring(start + tokenLen, end);

        Map mapTag = null;
        try {
            long tagUID = Long.parseLong(id);
            mapTag = sessionInfo.getTag(tagUID);
        } catch (Throwable ignore) {
        }

        final String word = MapUtils.getMapString(mapTag, "name", "" + id);
        final Map fMapTag = mapTag;

        final DrawableTag imgDrawable = new DrawableTag(context, p, word, showIcon ? tagDrawables : null,
                mapTag, drawCount) {

            @Override
            public boolean isTagPressed() {
                if (!AndroidUtils.usesNavigationControl()) {
                    return false;
                }
                int selectionEnd = tvTags.getSelectionEnd();
                if (selectionEnd < 0) {
                    return false;
                }
                int selectionStart = tvTags.getSelectionStart();
                return selectionStart == fSpanStart && selectionEnd == fSpanEnd;
            }

            @Override
            public int getTagState() {
                if (listener == null) {
                    return TAG_STATE_SELECTED;
                }
                return listener.getTagState(fMapTag, word);
            }
        };

        if (countFontRatio > 0) {
            imgDrawable.setCountFontRatio(countFontRatio);
        }

        imgDrawable.setBounds(0, 0, imgDrawable.getIntrinsicWidth(), imgDrawable.getIntrinsicHeight());
        //         Log.d(TAG, "State=" + Arrays.toString(imgDrawable.getState()));

        if (listener != null && showIcon) {
            int tagState = listener.getTagState(mapTag, word);
            int[] state = makeState(tagState, mapTag == null, false);
            imgDrawable.setState(state);
        }

        ImageSpan imageSpan = new ImageSpan(imgDrawable, DynamicDrawableSpan.ALIGN_BASELINE) {

            @Override
            public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
                int size = super.getSize(paint, text, start, end, fm);
                int width = -1;
                if (tvTags.getLayoutParams().width == ViewGroup.LayoutParams.WRAP_CONTENT) {
                    if (tvTags.getParent() instanceof ViewGroup) {
                        width = ((ViewGroup) tvTags.getParent()).getWidth();
                    }
                } else {
                    width = tvTags.getWidth();
                }
                if (width <= 0) {
                    return size;
                }
                return Math.min(size, width);
            }
        };

        ss.setSpan(imageSpan, start, end + tokenLen, 0);

        if (listener != null) {
            ClickableSpan clickSpan = new ClickableSpan() {

                @Override
                public void onClick(View widget) {
                    listener.tagClicked(fMapTag, word);

                    if (AndroidUtils.hasTouchScreen()) {
                        Selection.removeSelection((Spannable) tvTags.getText());
                    }
                    widget.invalidate();
                }
            };

            ss.setSpan(clickSpan, start, end + tokenLen, 0);
        }

    }
}

From source file:com.todoroo.astrid.tags.TagsControlSet.java

private CharSequence buildTagString() {
    List<TagData> sortedTagData = orderByName.sortedCopy(selectedTags);
    List<SpannableString> tagStrings = Lists.transform(sortedTagData, tagToString(Float.MAX_VALUE));
    SpannableStringBuilder builder = new SpannableStringBuilder();
    for (SpannableString tagString : tagStrings) {
        if (builder.length() > 0) {
            builder.append(SPACE);/*from w  w w.ja  va 2s  . com*/
        }
        builder.append(tagString);
    }
    return builder;
}

From source file:com.microsoft.rightsmanagement.ui.widget.UserPolicyViewerFragment.java

/**
 * Updates the policy viewer according to the ownership
 *///from w w  w .j  ava  2s .co m
private void updateViewAccordingToOwnership() {
    UserPolicyModel userPolicyModel = mUserPolicyDataProvider.getUserPolicyModel();
    // if this is not owner just show the rights with no header
    if (userPolicyModel.isIssuedToOwner()) {
        Logger.d(TAG, "user is the owner of user policy");
        mUpperTitleTextView.setText(getString(R.string.policy_viewer_owner_content));
        mOwnerNameTextView.setVisibility(View.GONE);
        setPolicyEditingButtonViewState(mUserPolicyDataProvider.isUserPolicyEditingEnabled());
    } else {
        Logger.d(TAG, "user is not the owner of user policy");
        //ignore input of allowing edit enabled and hide policy edit button 
        setPolicyEditingButtonViewState(false);
        mUpperTitleTextView.setText(R.string.policy_viewer_non_owner_content);
        SpannableStringBuilder sb = new SpannableStringBuilder();
        String grantedBy = getString(R.string.granted_by_string);
        sb.append(grantedBy);
        sb.append(" ");
        sb.append(userPolicyModel.getOwner());
        sb.setSpan(new StyleSpan(Typeface.BOLD), grantedBy.length(), sb.length(), 0);
        mOwnerNameTextView.setText(sb);
        drawRights();
    }
}

From source file:com.tct.mail.browse.SubjectAndFolderView.java

public void setFolders(ConversationViewHeaderCallbacks callbacks, Account account, Conversation conv) {
    mVisibleFolders = true;//from   w  ww  .j  a  va2s  .com
    final BidiFormatter bidiFormatter = getBidiFormatter();
    if (TextUtils.isEmpty(mSubject)) {
        mSubject = conv.subject;
    }
    final SpannableStringBuilder sb = new SpannableStringBuilder(bidiFormatter.unicodeWrap(mSubject));
    sb.append('\u0020');
    final Settings settings = account.settings;
    final int start = sb.length();
    if (settings.importanceMarkersEnabled && conv.isImportant()) {
        sb.append(".\u0020");
        sb.setSpan(new DynamicDrawableSpan(DynamicDrawableSpan.ALIGN_BASELINE) {
            @Override
            public Drawable getDrawable() {
                Drawable d = getContext().getResources()
                        .getDrawable(R.drawable.ic_email_caret_none_important_unread);
                d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
                return d;
            }
        }, start, start + 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
    }

    //TS: yanhua.chen 2015-12-22 EMAIL BUGFIX_1178365 MOD_S
    //        mFolderDisplayer.loadConversationFolders(conv, null /* ignoreFolder */, -1 /* ignoreFolderType */);
    //        mFolderDisplayer.appendFolderSpans(sb, bidiFormatter);

    //        final int end = sb.length();
    //        sb.setSpan(new ChangeLabelsSpan(callbacks), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    setText(sb);
    //TS: yanhua.chen 2015-11-4 EMAIL BUGFIX_851207 MOD_S
    //        if(isNeedMove()){
    //            setMovementMethod(LinkMovementMethod.getInstance());
    //        }
    //TS: yanhua.chen 2015-11-4 EMAIL BUGFIX_851207 MOD_E
    //TS: yanhua.chen 2015-12-22 EMAIL BUGFIX_1178365 MOD_E
}

From source file:com.android.messaging.datamodel.BugleNotifications.java

/**
 * buildBoldedMessage - build a formatted message where the title is bold, there's a
 * separator, then the message./*from  ww w  . java2s. c  om*/
 */
private static CharSequence buildBoldedMessage(final String title, final CharSequence message,
        final Uri attachmentUri, final String attachmentType, final int separatorId) {
    final Context context = Factory.get().getApplicationContext();
    final SpannableStringBuilder spanBuilder = new SpannableStringBuilder();

    // Boldify the title (which is the sender's name)
    if (!TextUtils.isEmpty(title)) {
        spanBuilder.append(title);
        spanBuilder.setSpan(new StyleSpan(Typeface.BOLD), 0, title.length(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    if (!TextUtils.isEmpty(message)) {
        if (spanBuilder.length() > 0) {
            spanBuilder.append(context.getString(separatorId));
        }
        spanBuilder.append(message);
    }
    if (attachmentUri != null) {
        if (spanBuilder.length() > 0) {
            final String separator = context.getString(R.string.notification_separator);
            spanBuilder.append(separator);
        }
        spanBuilder.append(formatAttachmentTag(null, attachmentType));
    }
    return spanBuilder;
}

From source file:com.android.messaging.datamodel.BugleNotifications.java

static CharSequence formatInboxMessage(final String sender, final CharSequence message, final Uri attachmentUri,
        final String attachmentType) {
    final Context context = Factory.get().getApplicationContext();
    final TextAppearanceSpan notificationSenderSpan = new TextAppearanceSpan(context,
            R.style.NotificationSenderText);

    final TextAppearanceSpan notificationTertiaryText = new TextAppearanceSpan(context,
            R.style.NotificationTertiaryText);

    final SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder();
    if (!TextUtils.isEmpty(sender)) {
        spannableStringBuilder.append(sender);
        spannableStringBuilder.setSpan(notificationSenderSpan, 0, sender.length(), 0);
    }/*from   w  w w.  j  ava2  s  .co m*/
    final String separator = context.getString(R.string.notification_separator);

    if (!TextUtils.isEmpty(message)) {
        if (spannableStringBuilder.length() > 0) {
            spannableStringBuilder.append(separator);
        }
        final int start = spannableStringBuilder.length();
        spannableStringBuilder.append(message);
        spannableStringBuilder.setSpan(notificationTertiaryText, start, start + message.length(), 0);
    }
    if (attachmentUri != null) {
        if (spannableStringBuilder.length() > 0) {
            spannableStringBuilder.append(separator);
        }
        spannableStringBuilder.append(formatAttachmentTag(null, attachmentType));
    }
    return spannableStringBuilder;
}