Example usage for android.text SpannableStringBuilder setSpan

List of usage examples for android.text SpannableStringBuilder setSpan

Introduction

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

Prototype

public void setSpan(Object what, int start, int end, int flags) 

Source Link

Document

Mark the specified range of text with the specified object.

Usage

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 w  w  w. j ava 2 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.zulip.android.util.CustomHtmlToSpannedConverter.java

private static void endFont(SpannableStringBuilder text) {
    int len = text.length();
    Object obj = getLast(text, Font.class);
    int where = text.getSpanStart(obj);

    text.removeSpan(obj);/*from  w w w  . j av a  2s.com*/

    if (where != len) {
        Font f = (Font) obj;

        if (!TextUtils.isEmpty(f.mColor)) {
            if (f.mColor.startsWith("@")) {
                Resources res = Resources.getSystem();
                String name = f.mColor.substring(1);
                int colorRes = res.getIdentifier(name, "color", "android");
                if (colorRes != 0) {
                    ColorStateList colors = res.getColorStateList(colorRes);
                    text.setSpan(new TextAppearanceSpan(null, 0, 0, colors, null), where, len,
                            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
            } else {
                int c = getHtmlColor(f.mColor);
                if (c != -1) {
                    text.setSpan(new ForegroundColorSpan(c | 0xFF000000), where, len,
                            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
            }
        }

        if (f.mFace != null) {
            text.setSpan(new TypefaceSpan(f.mFace), where, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
}

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 w w. j a va2  s .c  o m*/

    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:de.vanita5.twittnuker.util.ThemeUtils.java

public static void applyParagraphSpacing(TextView textView, float multiplier) {
    final SpannableStringBuilder builder = SpannableStringBuilder.valueOf(textView.getText());
    int prevLineBreak, currLineBreak = 0;
    for (int i = 0, j = builder.length(); i < j; i++) {
        if (builder.charAt(i) == '\n') {
            prevLineBreak = currLineBreak;
            currLineBreak = i;//w  w w.j  ava 2s  .  c  o  m
            if (currLineBreak > 0) {
                builder.setSpan(new ParagraphSpacingSpan(multiplier), prevLineBreak, currLineBreak,
                        Spanned.SPAN_INCLUSIVE_INCLUSIVE);
            }
        }
    }
    textView.setText(builder);
}

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);/*from  w  w w  .j  a va2s  . c  o m*/
    }

    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: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 ww  .j  a v  a2  s. c om*/
    super.onTitleChanged(title, color);
}

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();
    ;//from  w w w .  j a  v  a 2 s  .  c om
    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:com.none.tom.simplerssreader.view.ReadMoreTextView.java

@SuppressWarnings("SameParameterValue")
public void setText(final CharSequence text, final int maxEms) {
    if (!TextUtils.isEmpty(text) && text.length() > maxEms) {
        final SpannableStringBuilder ssb = new SpannableStringBuilder(text.subSequence(0, maxEms));

        ssb.append('\u2026');

        final int textLength = ssb.length();

        ssb.append('\n');
        ssb.append(getContext().getString(R.string.read_more));

        final int readMoreLength = ssb.length() - textLength;

        ssb.setSpan(new ReadMoreClickableSpan(), textLength, textLength + readMoreLength,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        super.setText(ssb);

        setMaxEms(maxEms + readMoreLength);
        setMovementMethod(LinkMovementMethod.getInstance());

        mText = text;//from  www  .j a  v a2 s .c o  m
    } else {
        super.setText(text);
    }
}

From source file:com.popdeem.sdk.uikit.adapter.PDUIFeedRecyclerViewAdapter.java

@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
    final PDFeed item = this.mItems.get(position);

    boolean isCheckin = item.getImageUrlString().contains("default");
    if (isCheckin) {
        holder.sharedImageView.setVisibility(View.GONE);
        holder.sharedImageView.setImageDrawable(null);
    } else {//from w w  w.j  a  v a2 s. c  o  m

        holder.sharedImageView.setVisibility(View.VISIBLE);

        Glide.with(context).load(item.getImageUrlString()).error(null).dontAnimate()
                .into(holder.sharedImageView);

    }

    if (item.getUserProfilePicUrlString().isEmpty()) {

        Glide.with(context).load(R.drawable.pduikit_default_user).dontAnimate().dontAnimate().fitCenter()
                .into(holder.profileImageView);
    } else {
        Glide.with(context).load(item.getUserProfilePicUrlString()).placeholder(R.drawable.pduikit_default_user)
                .error(R.drawable.pduikit_default_user).dontAnimate().fitCenter().into(holder.profileImageView);
    }

    //        Spannable actionText = getRedemptionText(holder.context, item.getUserFirstName(), item.getUserLastName(), item.getDescriptionString(), item.getBrandName(), isCheckin);
    holder.userNameTextView.setText(getNameForItem(item.getUserFirstName(), item.getUserLastName()));
    SpannableStringBuilder str = new SpannableStringBuilder(
            getNameForItem(item.getUserFirstName(), item.getUserLastName()) + " " + item.getCaption());
    str.setSpan(new android.text.style.StyleSpan(android.graphics.Typeface.BOLD), 0,
            getNameForItem(item.getUserFirstName(), item.getUserLastName()).length(),
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    holder.userCommentTextView.setText(str);

    holder.timeTextView.setText(item.getTimeAgo());
}

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

private static void endHeader(SpannableStringBuilder text) {
    int len = text.length();
    Object obj = getLast(text, Header.class);

    int where = text.getSpanStart(obj);

    text.removeSpan(obj);//from  ww  w  . ja v a  2s.  c o m

    // Back off not to change only the text, not the blank line.
    while (len > where && text.charAt(len - 1) == '\n') {
        len--;
    }

    if (where != len) {
        Header h = (Header) obj;

        text.setSpan(new RelativeSizeSpan(HEADER_SIZES[h.mLevel]), where, len,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        text.setSpan(new StyleSpan(Typeface.BOLD), where, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
}