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:org.smssecure.smssecure.contacts.ContactSelectionListAdapter.java

@Override
public void bindView(View view, Context context, Cursor cursor) {
    final DataHolder contactData = (DataHolder) view.getTag(R.id.contact_info_tag);
    final ViewHolder holder = (ViewHolder) view.getTag(R.id.holder_tag);
    if (holder == null) {
        Log.w(TAG, "ViewHolder was null. This should not happen.");
        return;//from w ww .  j av a 2s  .c  o  m
    }
    if (contactData == null) {
        Log.w(TAG, "DataHolder was null. This should not happen.");
        return;
    }
    if (ID_COLUMN < 0) {
        populateColumnIndices(cursor);
    }

    contactData.type = cursor.getInt(TYPE_COLUMN);
    contactData.name = cursor.getString(NAME_COLUMN);
    contactData.number = cursor.getString(NUMBER_COLUMN);
    contactData.numberType = cursor.getInt(NUMBER_TYPE_COLUMN);
    contactData.label = cursor.getString(LABEL_COLUMN);
    contactData.id = cursor.getLong(ID_COLUMN);

    if (contactData.type != ContactsDatabase.PUSH_TYPE) {
        holder.name.setTextColor(drawables.getColor(1, 0xff000000));
        holder.number.setTextColor(drawables.getColor(1, 0xff000000));
    } else {
        holder.name.setTextColor(drawables.getColor(0, 0xa0000000));
        holder.number.setTextColor(drawables.getColor(0, 0xa0000000));
    }

    if (selectedContacts.containsKey(contactData.id)) {
        holder.checkBox.setChecked(true);
    } else {
        holder.checkBox.setChecked(false);
    }

    holder.name.setText(contactData.name);

    if (contactData.number == null || contactData.number.isEmpty()) {
        holder.name.setEnabled(false);
        holder.number.setText("");
    } else if (contactData.type == ContactsDatabase.PUSH_TYPE) {
        holder.number.setText(contactData.number);
    } else {
        final CharSequence label = ContactsContract.CommonDataKinds.Phone.getTypeLabel(context.getResources(),
                contactData.numberType, contactData.label);
        final CharSequence numberWithLabel = contactData.number + "  " + label;
        final Spannable numberLabelSpan = new SpannableString(numberWithLabel);
        numberLabelSpan.setSpan(new ForegroundColorSpan(drawables.getColor(2, 0xff444444)),
                contactData.number.length(), numberWithLabel.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        holder.number.setText(numberLabelSpan);
    }
    holder.contactPhoto.setImageDrawable(ContactPhotoFactory.getLoadingPhoto(context));
    if (contactData.id > -1)
        loadBitmap(contactData.number, holder.contactPhoto);
}

From source file:com.ubercab.client.feature.notification.handler.TripNotificationHandler.java

private NotificationCompat.InboxStyle buildStyleInbox(NotificationCompat.Builder paramBuilder,
          TripNotificationData paramTripNotificationData, String paramString) {
      NotificationCompat.InboxStyle localInboxStyle = new NotificationCompat.InboxStyle(paramBuilder)
              .setSummaryText(paramString);
      Iterator localIterator = paramTripNotificationData.getFareSplitClients().iterator();
      while (localIterator.hasNext()) {
          TripNotificationData.FareSplitClient localFareSplitClient = (TripNotificationData.FareSplitClient) localIterator
                  .next();/*from w w  w . j a  va  2 s . co m*/
          SpannableString localSpannableString = new SpannableString(localFareSplitClient.getName());
          localSpannableString.setSpan(new StyleSpan(1), 0, localSpannableString.length(), 33);
          SpannableStringBuilder localSpannableStringBuilder = new SpannableStringBuilder();
          localSpannableStringBuilder.append(localSpannableString);
          localSpannableStringBuilder.append(" ");
          localSpannableStringBuilder.append(localFareSplitClient.getDisplayStatus(getContext()));
          localInboxStyle.addLine(localSpannableStringBuilder);
      }
      return localInboxStyle;
  }

From source file:de.busse_apps.hmintpmd.gui.ResultFragment.java

private void setValueText(double value, int level) {
    String valueString;//from  www.j  a v  a 2s  . com
    if (level < WSIKMCalculator.MAX_LEVEL) {
        valueString = String.format(Locale.getDefault(), "%.1f", value);
    } else {
        valueString = "\u221E";
    }
    SpannableString msg = new SpannableString(valueString + " kDoil");
    msg.setSpan(new ForegroundColorSpan(WSIKMCalculator.getColorForLevel(getActivity(), level)), 0,
            valueString.length(), 0);
    mResultValue.setText(msg);
}

From source file:org.catrobat.catroid.ui.ScratchConverterActivity.java

private void appendColoredBetaLabelToTitle(final int color) {
    final String title = getString(R.string.title_activity_scratch_converter);
    final String beta = getString(R.string.beta).toUpperCase(Locale.getDefault());
    final SpannableString spanTitle = new SpannableString(title + " " + beta);
    final int begin = title.length() + 1;
    final int end = begin + beta.length();
    spanTitle.setSpan(new ForegroundColorSpan(color), begin, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    getActionBar().setTitle(spanTitle);/*  w  w  w  .j  ava 2 s.com*/
}

From source file:cn.dreamtobe.emoji.ellipsize.helper.SpanEllipsizeEndHelper.java

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public static CharSequence matchMaxWidth(SpannableString targetText, TextView textView) {
    if (targetText.length() <= 0) {
        return targetText;
    }/*  ww  w.j a  v  a 2s.c om*/

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
        return targetText;
    }

    if (textView == null) {
        return targetText;
    }

    final int maxWidth = textView.getMaxWidth();

    if (maxWidth <= 0 || maxWidth >= Integer.MAX_VALUE) {
        return targetText;
    }

    if (textView.getEllipsize() != TextUtils.TruncateAt.END) {
        return targetText;
    }

    //TODO Multi-lines support
    if (textView.getMaxLines() != 1) {
        return targetText;
    }

    final String maxWidthKey = getMaxWidthKey(targetText, textView);
    SpannableString tmpText = SPAN_MAXWIDTH_CACHE.get(maxWidthKey);
    if (tmpText != null) {
        removeClickableSpan(tmpText);
        return tmpText;
    }

    TextPaint textPaint = textView.getPaint();
    if (textPaint == null) {
        return targetText;
    }

    final int totalWidth = (int) textPaint.measureText(targetText, 0, targetText.length());
    if (totalWidth <= maxWidth) {
        return targetText;
    }

    final long startTime = System.currentTimeMillis();
    // deal maxwitdh

    final int dotWidth = (int) textPaint.measureText("...");

    tmpText = targetText;

    int start = 0;
    int end = targetText.length();

    // targetX is maxWidth - "...".length
    int targetX = maxWidth - dotWidth;

    //dichotomy: get x most touch targetX
    int middle = targetText.length();
    int x = 0;
    while (start <= end) {
        // tx = targetX, tl = targetLength

        // width:  0           x
        // length: 0         middle           end
        //         -------------|-------------
        middle = (start + end) / 2;

        int emojiDraW = 0;
        int emojiStrW = 0;

        int emojiExcursion = 1;

        final Object[] tmpSpans = tmpText.getSpans(0, middle, Object.class);
        if (tmpSpans != null) {
            for (Object tmpSpan : tmpSpans) {
                final int tmpStart = tmpText.getSpanStart(tmpSpan);
                final int tmpEnd = tmpText.getSpanEnd(tmpSpan);

                //middle in (tmpStart, tmpEnd)
                if (tmpStart < middle && tmpEnd > middle) {
                    middle = tmpEnd;
                    emojiExcursion = tmpEnd - tmpStart;
                }
            }

            // TextPaint#measure do not attention span, so adjust by ourselves
            for (Object tmpSpan : tmpSpans) {
                final int tmpStart = tmpText.getSpanStart(tmpSpan);
                final int tmpEnd = tmpText.getSpanEnd(tmpSpan);

                // TODO support other span
                if (tmpStart < middle && tmpSpan instanceof ImageSpan) {
                    emojiDraW += ((ImageSpan) tmpSpan).getDrawable().getBounds().width();
                    emojiStrW += textPaint.measureText(tmpText, tmpStart, tmpEnd);
                }
            }

        }

        x = (int) textPaint.measureText(tmpText, 0, middle);
        x = x - emojiStrW + emojiDraW;

        //            x = (int) (textPaint.measureText(pureStr, 0, pureStr.length()) + emojiWidth);

        //            Log.d(TAG, String.format("targetX: %d, currentX: %d, currentLength: %d, totalLength: %d, emojiStrW[%d], emojiDraW[%d]", targetX, x, middle, targetText.length(), emojiStrW, emojiDraW));

        if (x > targetX) {
            // width:  0       tx        x
            // length: start   tl      middle         end
            //             ----|---------|-------------
            // TO:     start   |       *end
            //             ----|--------|--------------
            end = middle - emojiExcursion;
        } else if (x < targetX) {
            // width:  0               x       tx
            // length: start         middle    tl     end
            //           --------------|-------|------
            // TO:                      *start  |       end
            //           ---------------|------|------
            start = middle + 1;
        } else {
            break;
        }
    }

    // adjust x larger targetX
    while (x > targetX && middle > 0) {
        x = (int) textPaint.measureText(tmpText, 0, --middle);
    }

    // adjust x middle emoji span
    final Object[] ajustSpans = tmpText.getSpans(0, tmpText.length(), Object.class);
    for (Object adjustSpan : ajustSpans) {
        final int adjustStart = tmpText.getSpanStart(adjustSpan);
        final int adjustEnd = tmpText.getSpanEnd(adjustSpan);

        //[adjustStart, adjustEnd)
        if (middle >= adjustStart && middle < adjustEnd) {
            middle = adjustStart - 1;
            break;
        }
    }

    // finnal middle

    // sub sequence [0, middle + 1) & remove [middle +1, length] spans
    tmpText = (SpannableString) tmpText.subSequence(0, middle + 1);
    //        Log.d(TAG, String.format("sub Sequence[0, %d), [%s] to [%s]", middle + 1, targetText, tmpText));

    // add ...
    final SpannableString maxWidthSS = new SpannableString(tmpText + "...");

    final Object[] maxWidthSpans = tmpText.getSpans(0, tmpText.length(), Object.class);
    if (maxWidthSpans != null) {
        for (Object maxWidthSpan : maxWidthSpans) {
            final int mwSpanStart = tmpText.getSpanStart(maxWidthSpan);
            final int mwSpanEnd = tmpText.getSpanEnd(maxWidthSpan);
            final int mwSpanFlag = tmpText.getSpanFlags(maxWidth);

            maxWidthSS.setSpan(maxWidthSpan, mwSpanStart, mwSpanEnd, mwSpanFlag);
        }
    }

    targetText = maxWidthSS;

    SPAN_MAXWIDTH_CACHE.put(maxWidthKey, targetText);
    Log.d(TAG, String.format("deal maxWidth %d", System.currentTimeMillis() - startTime));

    return targetText;
}

From source file:com.jbirdvegas.mgerrit.cards.PatchSetPropertiesCard.java

private void setImageCaption(TextView textView, int resID, String authorName) {
    String title = mContext.getResources().getString(resID);
    if (title == null || authorName == null)
        return;/*from   w  w  w.  ja v a  2s  . c  o m*/

    SpannableString text = new SpannableString(title + "\n" + authorName);
    text.setSpan(new TextAppearanceSpan(mContext, R.style.CardText_CommitOwnerText), 0, title.length(), 0);

    int end = title.length() + 1;
    text.setSpan(new TextAppearanceSpan(mContext, R.style.CardText_CommitOwnerDetails), end,
            end + authorName.length(), 0);

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

From source file:com.dwdesign.tweetings.adapter.UserAutoCompleteAdapter.java

@Override
public CharSequence convertToString(final Cursor cursor) {
    if (mCursorClosed)
        return null;
    if (isHash && mLookupText != null) {
        String statusText = cursor.getString(mStatusText);
        SpannableString spannable = new SpannableString(statusText);
        final Matcher matcher = Regex.VALID_HASHTAG.matcher(spannable);
        while (matcher.find()) {
            final String url = matcherGroup(matcher, Regex.VALID_HASHTAG_GROUP_HASHTAG_FULL);
            if (url.toLowerCase().startsWith(mLookupText.toLowerCase())) {
                return url;
            }/*from  w w  w.ja v  a2s  .  co m*/
        }
    } else {
        return "@" + cursor.getString(mScreenNameIdx);
    }
    return null;
}

From source file:com.androzic.About.java

private void updateAboutInfo(final View view) {
    // version/*from w w  w.  j  ava2s  .com*/
    String versionName = null;
    int versionBuild = 0;
    try {
        versionName = getActivity().getPackageManager().getPackageInfo(getActivity().getPackageName(),
                0).versionName;
        versionBuild = getActivity().getPackageManager().getPackageInfo(getActivity().getPackageName(),
                0).versionCode;
    } catch (NameNotFoundException ex) {
        versionName = "unable to retreive version";
    }
    final TextView version = (TextView) view.findViewById(R.id.version);
    version.setText(getString(R.string.version, versionName, versionBuild));

    // home links
    StringBuilder links = new StringBuilder();
    links.append("<a href=\"");
    links.append(": http://androzic.com");
    links.append("\">");
    links.append(getString(R.string.homepage));
    links.append("</a><br /><a href=\"");
    links.append(getString(R.string.faquri));
    links.append("\">");
    links.append(getString(R.string.faq));
    links.append("</a><br /><a href=\"");
    links.append(getString(R.string.featureuri));
    links.append("\">");
    links.append(getString(R.string.feedback));
    links.append("</a>");
    final TextView homelinks = (TextView) view.findViewById(R.id.homelinks);
    homelinks.setText(Html.fromHtml(links.toString()));
    homelinks.setMovementMethod(LinkMovementMethod.getInstance());

    // community links
    StringBuilder communities = new StringBuilder();
    communities.append("<a href=\"");
    communities.append(getString(R.string.googleplusuri));
    communities.append("\">");
    communities.append(getString(R.string.googleplus));
    communities.append("</a><br /><a href=\"");
    communities.append(getString(R.string.facebookuri));
    communities.append("\">");
    communities.append(getString(R.string.facebook));
    communities.append("</a><br /><a href=\"");
    communities.append(getString(R.string.twitteruri));
    communities.append("\">");
    communities.append(getString(R.string.twitter));
    communities.append("</a>");
    final TextView communitylinks = (TextView) view.findViewById(R.id.communitylinks);
    communitylinks.setText(Html.fromHtml(communities.toString()));
    communitylinks.setMovementMethod(LinkMovementMethod.getInstance());

    // donations
    StringBuilder donations = new StringBuilder();
    donations.append("<a href=\"");
    donations.append(getString(R.string.playuri));
    donations.append("\">");
    donations.append(getString(R.string.donate_google));
    donations.append("</a><br /><a href=\"");
    donations.append(getString(R.string.paypaluri));
    donations.append("\">");
    donations.append(getString(R.string.donate_paypal));
    donations.append("</a>");

    final TextView donationlinks = (TextView) view.findViewById(R.id.donationlinks);
    donationlinks.setText(Html.fromHtml(donations.toString()));
    donationlinks.setMovementMethod(LinkMovementMethod.getInstance());

    Androzic application = Androzic.getApplication();
    if (application.isPaid) {
        view.findViewById(R.id.donations).setVisibility(View.GONE);
        view.findViewById(R.id.donationtext).setVisibility(View.GONE);
        donationlinks.setVisibility(View.GONE);
    }

    // license
    final SpannableString message = new SpannableString(
            Html.fromHtml(getString(R.string.app_eula).replace("/n", "<br/>")));
    Linkify.addLinks(message, Linkify.WEB_URLS);
    final TextView license = (TextView) view.findViewById(R.id.license);
    license.setText(message);
    license.setMovementMethod(LinkMovementMethod.getInstance());

    // credits
    String[] names = getResources().getStringArray(R.array.credit_names);
    String[] merits = getResources().getStringArray(R.array.credit_merits);

    StringBuilder credits = new StringBuilder();
    for (int i = 0; i < names.length; i++) {
        credits.append("<b>");
        credits.append(merits[i]);
        credits.append("</b> &mdash; ");
        credits.append(names[i]);
        credits.append("<br />");
    }

    final TextView creditlist = (TextView) view.findViewById(R.id.credits);
    creditlist.setText(Html.fromHtml(credits.toString()));

    // dedication
    final TextView dedicated = (TextView) view.findViewById(R.id.dedicated);
    dedicated.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            clicks = 1;
            dedicated.setVisibility(View.GONE);
            View photo = view.findViewById(R.id.photo);
            photo.setVisibility(View.VISIBLE);
            photo.setOnClickListener(redirect);
        }
    });
}

From source file:org.sufficientlysecure.localcalendar.ui.MainActivity.java

private void showAbout() {
    SpannableString s = new SpannableString(getText(R.string.about));
    Linkify.addLinks(s, Linkify.ALL);//  w  w w.j a  va2 s  .c o  m
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage(s);
    AlertDialog alert = builder.create();
    alert.show();
    ((TextView) alert.findViewById(android.R.id.message)).setMovementMethod(LinkMovementMethod.getInstance());
}

From source file:com.aniruddhc.acemusic.player.BlacklistManagerActivity.BlacklistManagerActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.blacklist_manager, menu);

    ActionBar actionBar = getActionBar();
    actionBar.setBackgroundDrawable(UIElementsHelper.getGeneralActionBarBackground(mContext));
    SpannableString s = new SpannableString(getResources().getString(R.string.blacklist_manager));
    s.setSpan(new TypefaceSpan(this, "RobotoCondensed-Light"), 0, s.length(),
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    actionBar.setTitle(s);//  w  ww .j  a va2 s .c  o m

    return super.onCreateOptionsMenu(menu);
}