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:org.pencilsofpromise.rss.RSSFragment.java

private Spanned removeContentSpanObjects(String sb) {
    SpannableStringBuilder spannedStr = (SpannableStringBuilder) Html.fromHtml(sb.toString().trim());
    Object[] spannedObjects = spannedStr.getSpans(0, spannedStr.length(), Object.class);
    for (int i = 0; i < spannedObjects.length; i++) {

        if (spannedObjects[i] instanceof ImageSpan)
            spannedStr.replace(spannedStr.getSpanStart(spannedObjects[i]),
                    spannedStr.getSpanEnd(spannedObjects[i]), "");

    }//from  w w  w . j  ava 2 s.c  o m
    return spannedStr;
}

From source file:com.xinheng.crash.CrashListAdapter.java

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
    String crash = mData[position];
    holder.log = crash;/*from   w  w  w.  j  a  va 2s  .  c  o  m*/
    if (crash != null) {
        setSpaceState(holder, /*show = */ crash.startsWith("at "));

        if (crash.startsWith("Caused by")) {
            holder.title.setTypeface(null, Typeface.BOLD);
            holder.title.setTextColor(0xdeffffff);
        } else {
            holder.title.setTypeface(null, Typeface.NORMAL);
            holder.title.setTextColor(0xffef4545);
        }

        if (mPackageName != null && crash.contains(mPackageName) && crash.indexOf("(") > -1) {
            holder.itemView.setSelected(true);
            int indexOfC = crash.indexOf("(");
            String atPackage = crash.substring(0, indexOfC);
            SpannableStringBuilder builder = new SpannableStringBuilder(atPackage)
                    .append(StringStyleUtils.format(holder.title.getContext(), " " + crash.substring(indexOfC),
                            R.style.LineTextAppearance));
            CharSequence title = builder.subSequence(0, builder.length());
            holder.title.setText(title);

        } else {
            holder.itemView.setSelected(false);
            holder.title.setText(crash);
        }
    }
}

From source file:cn.wjh1119.bestnews.ui.fragment.DetailFragment.java

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    if (data != null && data.moveToFirst()) {

        //?????view
        FetchDetailDataFromSqlTask fetchDetailDataFromSqlTask = new FetchDetailDataFromSqlTask();
        fetchDetailDataFromSqlTask//from ww w .jav  a2  s. c  om
                .setOnDataFinishedListener(new FetchDetailDataFromSqlTask.OnDataFinishedListener() {
                    @Override
                    public void onDataSuccessfully(HashMap data) {

                        String title = data.get("title").toString();
                        String source = data.get("source").toString();
                        String pubDate = data.get("pubDate").toString();
                        String content = data.get("content").toString();
                        String link = data.get("link").toString();
                        //                    BitmapDrawable drawable = (BitmapDrawable) data.get("drawable");
                        Object imageTextFromSql = data.get("imageUrl");
                        String imageUrl = null;
                        if (imageTextFromSql != null) {
                            imageUrl = data.get("imageUrl").toString();
                        }

                        mTitleView.setText(title);
                        mTitleView.setFocusable(true);
                        mTitleView.setFocusableInTouchMode(true);
                        mTitleView.requestFocus();
                        TextPaint paint = mTitleView.getPaint();
                        paint.setFakeBoldText(true);
                        mSourceView.setText(source);
                        mPubDateView.setText(pubDate);
                        mContentView.setText(content);

                        if (imageUrl != null) {
                            Bitmap bitmap = imageManager.showCacheBitmap(imageUrl);
                            if (bitmap != null) {
                                mImageView.setImageBitmap(bitmap);

                            } else {
                                imageManager.downloadImage(imageUrl, new ImageManager.onImageLoaderListener() {

                                    @Override
                                    public void onImageLoader(Bitmap bitmap, String url) {
                                        if (bitmap != null) {
                                            mImageView.setImageBitmap(bitmap);
                                        } else {
                                            mImageView.setImageResource(R.mipmap.picture_fail_loading);
                                        }
                                    }
                                });
                            }
                        }

                        SpannableStringBuilder ssb = new SpannableStringBuilder("??");
                        ssb.setSpan(new URLSpan(link), 0, ssb.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);

                        ForegroundColorSpan span = new ForegroundColorSpan(Color.BLUE);
                        ssb.setSpan(span, 0, ssb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

                        mLinkView.setText(ssb);
                        // ??MovementMethod
                        mLinkView.setMovementMethod(LinkMovementMethod.getInstance());
                        // ??ClickableSpan
                        mLinkView.setHighlightColor(0xff8FABCC);
                    }

                    @Override
                    public void onDataFailed() {

                    }
                });
        fetchDetailDataFromSqlTask.execute(data);
    }
    AppCompatActivity activity = (AppCompatActivity) getActivity();
    Toolbar toolbarView = (Toolbar) getView().findViewById(R.id.toolbar_detail);

    // We need to start the enter transition after the data has loaded
    if (mTransitionAnimation) {
        activity.supportStartPostponedEnterTransition();

        if (null != toolbarView) {
            activity.setSupportActionBar(toolbarView);
            activity.getSupportActionBar().setDisplayShowTitleEnabled(false);
            activity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
    } else {
        if (null != toolbarView) {
            Menu menu = toolbarView.getMenu();
            if (null != menu)
                menu.clear();
        }
    }
}

From source file:com.android.mms.ui.ConversationListItem.java

public final void bind(Context context, final Conversation conversation) {
    //if (DEBUG) Log.v(TAG, "bind()");
    boolean sameItem = mConversation != null && mConversation.getThreadId() == conversation.getThreadId();

    mConversation = conversation;/*from   w ww .j a v  a 2s.c  om*/

    LayoutParams attachmentLayout = (LayoutParams) mAttachmentView.getLayoutParams();
    boolean hasError = conversation.hasError();
    // When there's an error icon, the attachment icon is left of the error icon.
    // When there is not an error icon, the attachment icon is left of the date text.
    // As far as I know, there's no way to specify that relationship in xml.
    if (hasError) {
        attachmentLayout.addRule(RelativeLayout.LEFT_OF, R.id.error);
    } else {
        attachmentLayout.addRule(RelativeLayout.LEFT_OF, R.id.date);
    }

    boolean hasAttachment = conversation.hasAttachment();
    mAttachmentView.setVisibility(hasAttachment ? VISIBLE : GONE);

    // Date
    mDateView.setText(MessageUtils.formatTimeStampString(context, conversation.getDate()));

    // From.
    mFromView.setText(formatMessage());

    // Register for updates in changes of any of the contacts in this conversation.
    ContactList contacts = conversation.getRecipients();

    if (Log.isLoggable(LogTag.CONTACT, Log.DEBUG)) {
        Log.v(TAG, "bind: contacts.addListeners " + this);
    }
    Contact.addListener(this);

    // Subject
    SmileyParser parser = SmileyParser.getInstance();
    final String snippet = conversation.getSnippet();
    if (mConversation.hasUnreadMessages()) {
        SpannableStringBuilder buf = new SpannableStringBuilder(parser.addSmileySpans(snippet));
        buf.setSpan(STYLE_BOLD, 0, buf.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        mSubjectView.setText(buf);
    } else {
        mSubjectView.setText(parser.addSmileySpans(snippet));
    }

    // Transmission error indicator.
    mErrorIndicator.setVisibility(hasError ? VISIBLE : GONE);

    updateAvatarView();
    mAvatarView.setChecked(isChecked(), sameItem);
}

From source file:me.drakeet.library.ui.CrashListAdapter.java

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
    String crash = mData[position];
    holder.log = crash;/*from   w  w  w.  j a  v a  2  s .c o m*/
    if (crash != null) {
        setSpaceState(holder, /*show = */ crash.startsWith("at "));

        if (crash.startsWith("Caused by")) {
            holder.title.setTypeface(null, Typeface.BOLD);
            holder.title.setTextColor(0xdeffffff);
        } else {
            holder.title.setTypeface(null, Typeface.NORMAL);
            holder.title.setTextColor(0xffef4545);
        }

        if (mPackageName != null && crash.contains(mPackageName)) {
            holder.itemView.setSelected(true);
            int indexOfC = crash.indexOf("(");
            if (indexOfC >= 0) {
                String atPackage = crash.substring(0, indexOfC);
                SpannableStringBuilder builder = new SpannableStringBuilder(atPackage)
                        .append(StringStyleUtils.format(holder.title.getContext(),
                                " " + crash.substring(indexOfC), R.style.LineTextAppearance));
                CharSequence title = builder.subSequence(0, builder.length());
                holder.title.setText(title);
            } else {
                holder.title.setText(crash);
            }
        } else {
            holder.itemView.setSelected(false);
            holder.title.setText(crash);
        }
    }
}

From source file:fyp.hkust.facet.activity.SettingsActivity.java

private void convertPreferenceToUseCustomFont(Preference somePreference) {
    Typeface fontType = FontManager.getTypeface(getApplicationContext(), FontManager.APP_FONT);

    CustomTypeFaceSpan customTypefaceSpan = new CustomTypeFaceSpan("", fontType);

    SpannableStringBuilder ss;
    if (somePreference.getTitle() != null) {
        ss = new SpannableStringBuilder(somePreference.getTitle().toString());
        ss.setSpan(new StyleSpan(Typeface.BOLD), 0, ss.length(), 0);
        ss.setSpan(customTypefaceSpan, 0, ss.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
        somePreference.setTitle(ss);/*w w  w . j a  v a 2 s .com*/
    }

    if (somePreference.getSummary() != null) {
        ss = new SpannableStringBuilder(somePreference.getSummary().toString());
        ss.setSpan(customTypefaceSpan, 0, ss.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
        somePreference.setSummary(ss);
    }
}

From source file:com.android.mms.ui.ConversationListItem.java

private CharSequence formatMessage() {
    final int color = android.R.styleable.Theme_textColorSecondary;
    String from = mConversation.getRecipients().formatNames(", ");
    if (MessageUtils.isWapPushNumber(from)) {
        String[] mAddresses = from.split(":");
        from = mAddresses[mContext.getResources().getInteger(R.integer.wap_push_address_index)];
    }//www .  j  ava2  s.co m

    /**
     * Add boolean to know that the "from" haven't the Arabic and '+'.
     * Make sure the "from" display normally for RTL.
     */
    boolean isEnName = false;
    boolean isLayoutRtl = (TextUtils
            .getLayoutDirectionFromLocale(Locale.getDefault()) == View.LAYOUT_DIRECTION_RTL);
    if (isLayoutRtl && from != null) {
        if (from.length() >= 1) {
            Pattern pattern = Pattern.compile("[^-]+");
            Matcher matcher = pattern.matcher(from);
            isEnName = matcher.matches();
            if (isEnName && from.charAt(0) != '\u202D') {
                from = "\u202D" + from + "\u202C";
            }
        }
    }

    SpannableStringBuilder buf = new SpannableStringBuilder(from);

    if (mConversation.getMessageCount() > 1) {
        int before = buf.length();
        if (isLayoutRtl && isEnName) {
            buf.insert(1, mConversation.getMessageCount() + " ");
            buf.setSpan(new ForegroundColorSpan(mContext.getResources().getColor(R.color.message_count_color)),
                    1, buf.length() - before, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        } else {
            buf.append(mContext.getResources().getString(R.string.message_count_format,
                    mConversation.getMessageCount()));
            buf.setSpan(new ForegroundColorSpan(mContext.getResources().getColor(R.color.message_count_color)),
                    before, buf.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        }
    }
    if (mConversation.hasDraft()) {
        if (isLayoutRtl && isEnName) {
            int before = buf.length();
            buf.insert(1, '\u202E' + mContext.getResources().getString(R.string.draft_separator) + '\u202C');
            buf.setSpan(new ForegroundColorSpan(mContext.getResources().getColor(R.drawable.text_color_black)),
                    1, buf.length() - before + 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
            before = buf.length();
            int size;
            buf.insert(1, mContext.getResources().getString(R.string.has_draft));
            size = android.R.style.TextAppearance_Small;
            buf.setSpan(new TextAppearanceSpan(mContext, size), 1, buf.length() - before + 1,
                    Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
            buf.setSpan(new ForegroundColorSpan(mContext.getResources().getColor(R.drawable.text_color_red)), 1,
                    buf.length() - before + 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        } else {
            buf.append(mContext.getResources().getString(R.string.draft_separator));
            int before = buf.length();
            int size;
            buf.append(mContext.getResources().getString(R.string.has_draft));
            size = android.R.style.TextAppearance_Small;
            buf.setSpan(new TextAppearanceSpan(mContext, size, color), before, buf.length(),
                    Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
            buf.setSpan(new ForegroundColorSpan(mContext.getResources().getColor(R.drawable.text_color_red)),
                    before, buf.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        }
    }

    // Unread messages are shown in bold
    if (mConversation.hasUnreadMessages()) {
        buf.setSpan(STYLE_BOLD, 0, buf.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
    }
    return buf;
}

From source file:com.bdevlin.apps.ui.fragments.HelpListFragment.java

private void updateText() {
    // mHelpText = (TextView) getView().findViewById(R.id.help_text);
    mHelpText = (WebView) getView().findViewById(R.id.webView1);
    mHelpText.getSettings().setJavaScriptEnabled(false);

    CharSequence text = Utils.bold(Utils.italic(getResources().getString(R.string.about_eula)),
            Utils.color(Color.RED, getResources().getString(R.string.about_licenses)));
    CharSequence yourHtml = "<table>table</table>";

    CharSequence content = getContent();
    ;// www.  j  av a2s  . c o  m
    SpannedString sstr = SpannedString.valueOf(content);
    SpannedString message = SpannedString.valueOf(mMessage);
    SpannedString message2 = SpannedString.valueOf(text);
    SpannedString message3 = SpannedString.valueOf(yourHtml);

    SpannableStringBuilder aboutBody = new SpannableStringBuilder();
    aboutBody.append(Html.fromHtml(Html.toHtml(message)));
    aboutBody.append(Html.fromHtml(Html.toHtml(message2)));
    aboutBody.append(Html.fromHtml(Html.toHtml(message3)));
    aboutBody.append(Html.fromHtml(Html.toHtml(sstr)));
    aboutBody.setSpan(new StyleSpan(Typeface.ITALIC), 0, aboutBody.length(), 0);
    // mHelpText.setText(aboutBody);
    // mHelpText.loadData(Html.toHtml(aboutBody), "text/html", null);

    mHelpText.loadUrl("file:///android_asset/" + mTitle + ".html", null);

    //mHelpText.loadUrl("http://www.choosemyplate.gov/tools-supertracker");

}

From source file:de.vanita5.twittnuker.activity.support.ThemedFragmentActivity.java

@Override
protected void onTitleChanged(CharSequence title, int color) {
    final SpannableStringBuilder builder = new SpannableStringBuilder(title);
    final int themeResId = getCurrentThemeResourceId();
    final int themeColor = getThemeColor(), contrastColor = ColorUtils.getContrastYIQ(themeColor, 192);
    if (!ThemeUtils.isDarkTheme(themeResId)) {
        builder.setSpan(new ForegroundColorSpan(contrastColor), 0, builder.length(),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }//from   w  w w . j  a va  2s.  co  m
    super.onTitleChanged(title, color);
}

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

private static void startImg(SpannableStringBuilder text, Attributes attributes, HtmlParser.ImageGetter img) {
    String src = attributes.getValue("", "src");
    Drawable d = null;//from   w  w w.j av  a2 s .c om

    if (img != null) {
        d = img.getDrawable(src);
    }

    if (d == null) {
        d = ResourcesCompat.getDrawable(Resources.getSystem(), android.R.drawable.ic_menu_report_image, null);
        d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
    }

    int len = text.length();
    text.append("\uFFFC");

    text.setSpan(new ImageSpan(d, src), len, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}