Example usage for android.text SpannableString SpannableString

List of usage examples for android.text SpannableString SpannableString

Introduction

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

Prototype

public SpannableString(CharSequence source) 

Source Link

Document

For the backward compatibility reasons, this constructor copies all spans including android.text.NoCopySpan .

Usage

From source file:info.guardianproject.otr.app.im.app.MessageView.java

private SpannableString formatTimeStamp(Date date, int messageType, DateFormat format,
        MessageView.DeliveryState delivery, EncryptionState encryptionState) {

    StringBuilder deliveryText = new StringBuilder();
    deliveryText.append(format.format(date));
    deliveryText.append(' ');

    if (delivery != null) {
        //this is for delivery
        if (delivery == DeliveryState.DELIVERED) {

            deliveryText.append(DELIVERED_SUCCESS);

        } else if (delivery == DeliveryState.UNDELIVERED) {

            deliveryText.append(DELIVERED_FAIL);

        }/*from  ww w . j av a 2 s  .  c  o  m*/

    }

    if (messageType != Imps.MessageType.POSTPONED)
        deliveryText.append(DELIVERED_SUCCESS);//this is for sent, so we know show 2 checks like WhatsApp!

    SpannableString spanText = null;

    if (encryptionState == EncryptionState.ENCRYPTED) {
        deliveryText.append('X');
        spanText = new SpannableString(deliveryText.toString());
        int len = spanText.length();

        spanText.setSpan(new ImageSpan(getContext(), R.drawable.lock16), len - 1, len,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    } else if (encryptionState == EncryptionState.ENCRYPTED_AND_VERIFIED) {
        deliveryText.append('X');
        spanText = new SpannableString(deliveryText.toString());
        int len = spanText.length();

        spanText.setSpan(new ImageSpan(getContext(), R.drawable.lock16), len - 1, len,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    } else {
        spanText = new SpannableString(deliveryText.toString());
        int len = spanText.length();

    }

    //   spanText.setSpan(new StyleSpan(Typeface.SANS_SERIF), 0, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    // spanText.setSpan(new RelativeSizeSpan(0.8f), 0, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    //    spanText.setSpan(new ForegroundColorSpan(R.color.soft_grey),
    //        0, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    return spanText;
}

From source file:com.andrewshu.android.reddit.comments.CommentsListActivity.java

/**
 * Mark the OP submitter comments/*from w w  w.j av  a  2  s .c o  m*/
 */
void markSubmitterComments() {
    if (getOpThingInfo() == null || mCommentsAdapter == null)
        return;

    SpannableString authorSS = new SpannableString(getOpThingInfo().getAuthor() + " [S]");
    ForegroundColorSpan fcs;
    if (Util.isLightTheme(mSettings.getTheme()))
        fcs = new ForegroundColorSpan(getResources().getColor(R.color.blue));
    else
        fcs = new ForegroundColorSpan(getResources().getColor(R.color.pale_blue));
    authorSS.setSpan(fcs, 0, authorSS.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    for (int i = 0; i < mCommentsAdapter.getCount(); i++) {
        ThingInfo ci = mCommentsAdapter.getItem(i);
        // if it's the OP, mark his name
        if (getOpThingInfo().getAuthor().equalsIgnoreCase(ci.getAuthor()))
            ci.setSSAuthor(authorSS);
    }
}

From source file:com.gmail.taneza.ronald.carbs.main.MainActivity.java

private void showAboutDialog() {
    StringBuilder sb = new StringBuilder();
    sb.append(String.format(Locale.getDefault(), "%s%n", getText(R.string.app_name)));
    sb.append(String.format(Locale.getDefault(), "%s: %s%n%n", getText(R.string.about_version_label),
            getVersion()));/*from w  w  w. j  ava  2s .  c  om*/
    sb.append(String.format(Locale.getDefault(), "%s", getText(R.string.about_info)));

    SpannableString aboutString = new SpannableString(sb.toString());
    Linkify.addLinks(aboutString, Linkify.WEB_URLS | Linkify.EMAIL_ADDRESSES);

    LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    TextView aboutBodyView = (TextView) layoutInflater.inflate(R.layout.dialog_about, null);
    aboutBodyView.setText(aboutString);
    aboutBodyView.setMovementMethod(new LinkMovementMethod());

    new AlertDialog.Builder(this).setTitle(R.string.about).setView(aboutBodyView)
            .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // do nothing
                }
            }).show();
}

From source file:org.catrobat.catroid.ui.fragment.LookFragment.java

private void updateActionModeTitle() {
    int numberOfSelectedItems = adapter.getAmountOfCheckedItems();

    if (numberOfSelectedItems == 0) {
        actionMode.setTitle(actionModeTitle);
    } else {/*from w ww  .  j  a va 2s . co  m*/
        String appendix = multipleItemAppendixActionMode;

        if (numberOfSelectedItems == 1) {
            appendix = singleItemAppendixActionMode;
        }

        String numberOfItems = Integer.toString(numberOfSelectedItems);
        String completeTitle = actionModeTitle + " " + numberOfItems + " " + appendix;

        int titleLength = actionModeTitle.length();

        Spannable completeSpannedTitle = new SpannableString(completeTitle);
        completeSpannedTitle.setSpan(
                new ForegroundColorSpan(getResources().getColor(R.color.actionbar_title_color)),
                titleLength + 1, titleLength + (1 + numberOfItems.length()),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        actionMode.setTitle(completeSpannedTitle);
    }
}

From source file:com.forrestguice.suntimeswidget.SuntimesUtils.java

public static SpannableString createColorSpan(String text, String toColorize, int color) {
    SpannableString span = new SpannableString(text);
    int start = text.indexOf(toColorize);
    int end = start + toColorize.length();
    span.setSpan(new ForegroundColorSpan(color), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    return span;//w w  w .  j  a  v  a  2 s  .  c  om
}

From source file:com.cw.litenote.note.Note_adapter.java

private String getHtmlStringWithViewPort(int position, int viewPort) {
    int mStyle = Note.mStyle;

    System.out.println("Note_adapter / _getHtmlStringWithViewPort");
    String strTitle = db_page.getNoteTitle(position, true);
    String strBody = db_page.getNoteBody(position, true);
    String linkUri = db_page.getNoteLinkUri(position, true);

    // replace note title
    //TitleBody,YouTube linkWeb linkTitlelinktitle,Gray?
    boolean bSetGray = false;
    if (Util.isEmptyString(strTitle) && Util.isEmptyString(strBody)) {
        if (Util.isYouTubeLink(linkUri)) {
            strTitle = Util.getYouTubeTitle(linkUri);
            bSetGray = true;//from w  w  w. j av  a 2 s  .com
        } else if (linkUri.startsWith("http")) {
            strTitle = mWebTitle;
            bSetGray = true;
        }
    }

    Long createTime = db_page.getNoteCreatedTime(position, true);
    String head = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" + "<html><head>"
            + "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />";

    if (viewPort == VIEW_PORT_BY_NONE) {
        head = head + "<head>";
    } else if (viewPort == VIEW_PORT_BY_DEVICE_WIDTH) {
        head = head + "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">" + "<head>";
    } else if (viewPort == VIEW_PORT_BY_SCREEN_WIDTH) {
        //           int screen_width = UtilImage.getScreenWidth(act);
        int screen_width = 640;
        head = head + "<meta name=\"viewport\" content=\"width=" + String.valueOf(screen_width)
                + ", initial-scale=1\">" + "<head>";
    }

    String separatedLineTitle = (!Util.isEmptyString(strTitle)) ? "<hr size=2 color=blue width=99% >" : "";
    String separatedLineBody = (!Util.isEmptyString(strBody)) ? "<hr size=1 color=black width=99% >" : "";

    // title
    if (!Util.isEmptyString(strTitle)) {
        Spannable spanTitle = new SpannableString(strTitle);
        Linkify.addLinks(spanTitle, Linkify.ALL);
        spanTitle.setSpan(new AlignmentSpan.Standard(Alignment.ALIGN_CENTER), 0, spanTitle.length(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        //ref http://stackoverflow.com/questions/3282940/set-color-of-textview-span-in-android
        if (bSetGray) {
            ForegroundColorSpan foregroundSpan = new ForegroundColorSpan(Color.GRAY);
            spanTitle.setSpan(foregroundSpan, 0, spanTitle.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }

        strTitle = Html.toHtml(spanTitle);
    } else
        strTitle = "";

    // body
    if (!Util.isEmptyString(strBody)) {
        Spannable spanBody = new SpannableString(strBody);
        Linkify.addLinks(spanBody, Linkify.ALL);
        strBody = Html.toHtml(spanBody);
    } else
        strBody = "";

    // set web view text color
    String colorStr = Integer.toHexString(ColorSet.mText_ColorArray[mStyle]);
    colorStr = colorStr.substring(2);

    String bgColorStr = Integer.toHexString(ColorSet.mBG_ColorArray[mStyle]);
    bgColorStr = bgColorStr.substring(2);

    return head + "<body color=\"" + bgColorStr + "\">" + "<br>" + //Note: text mode needs this, otherwise title is overlaid
            "<p align=\"center\"><b>" + "<font color=\"" + colorStr + "\">" + strTitle + "</font>" + "</b></p>"
            + separatedLineTitle + "<p>" + "<font color=\"" + colorStr + "\">" + strBody + "</font>" + "</p>"
            + separatedLineBody + "<p align=\"right\">" + "<font color=\"" + colorStr + "\">"
            + Util.getTimeString(createTime) + "</font>" + "</p>" + "</body></html>";
}

From source file:im.ene.lab.attiq.ui.activities.ItemDetailActivity.java

@SuppressWarnings("unused")
public void onEventMainThread(ItemDetailEvent event) {
    Article article = event.article;/*from ww w .ja  v a  2  s . co  m*/
    String userName = null;
    if (article != null) {
        mArticle = article;
        mRealm.beginTransaction();
        mRealm.copyToRealmOrUpdate(mArticle);
        mRealm.commitTransaction();

        User user = article.getUser();

        mArticleName.setText(article.getTitle());
        mSpannableTitle = new SpannableString(article.getTitle());
        userName = user.getId();
        mSpannableSubtitle = new SpannableString(userName);

        updateTitle();

        buildArticleComments(article);

        buildArticleMenu(article);

        final String html;
        try {
            if (PrefUtil.isMathJaxEnabled()) {
                html = IOUtil.readAssets("html/article_mathjax.html");
            } else {
                html = IOUtil.readAssets("html/article.html");
            }

            Document doc = Jsoup.parse(html);
            Element elem = doc.getElementById("content");
            elem.append(article.getRenderedBody());
            String result = doc.outerHtml();
            mContentView.loadDataWithBaseURL(article.getUrl(), result, "text/html", "utf-8", null);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    final CharSequence subTitle;
    if (article != null && !UIUtil.isEmpty(userName)) {
        subTitle = Html.fromHtml(getString(R.string.item_user_info, userName, userName,
                TimeUtil.beautify(article.getCreatedAt())));
    } else {
        subTitle = getString(R.string.item_detail_subtitle, userName);
    }

    mArticleDescription.setText(subTitle);
}

From source file:com.forrestguice.suntimeswidget.SuntimesUtils.java

public static SpannableString createBoldSpan(String text, String toBold) {
    SpannableString span = new SpannableString(text);
    int start = text.indexOf(toBold);
    int end = start + toBold.length();
    span.setSpan(new android.text.style.StyleSpan(android.graphics.Typeface.BOLD), start, end,
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    return span;//  www .  ja va 2s.c  om
}

From source file:com.forrestguice.suntimeswidget.SuntimesUtils.java

public static SpannableString createBoldColorSpan(String text, String toBold, int color) {
    SpannableString span = new SpannableString(text);
    int start = text.indexOf(toBold);
    int end = start + toBold.length();
    span.setSpan(new android.text.style.StyleSpan(android.graphics.Typeface.BOLD), start, end,
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    span.setSpan(new ForegroundColorSpan(color), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    return span;/*from  w  w w .  ja v  a2  s. c om*/
}

From source file:at.jclehner.rxdroid.DrugListActivity.java

@TargetApi(11)
private void updateDateString() {
    if (mCurrentDate == null)
        return;/*w w  w  . ja va 2s .co m*/

    final SpannableString dateString = new SpannableString(
            DateFormat.getDateFormat(this).format(mCurrentDate.getTime()));

    if (isShowingCurrentDate())
        Util.applyStyle(dateString, new UnderlineSpan());

    Util.applyStyle(dateString, new RelativeSizeSpan(0.75f));
    getSupportActionBar().setSubtitle(dateString);
}