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:com.groksolutions.grok.mobile.annotation.AnnotationListFragment.java

/**
 * Format annotation message body for list item display
 *
 * @param annotation The annotation to format
 *
 * @return The formatted annotation text suitable for use with {@link android.widget.TextView}
 *///from www .j a v a2s. c om
SpannableStringBuilder formatAnnotationBody(Annotation annotation) {
    int pos;

    // Format text
    SpannableStringBuilder text = new SpannableStringBuilder();

    // <br/> My Message
    pos = text.length();
    text.append(annotation.getMessage());
    text.setSpan(new RelativeSizeSpan(1.0f), pos, text.length(), 0);
    text.setSpan(new StyleSpan(Typeface.NORMAL), pos, text.length(), 0);

    // Attach hidden annotation ID to the text.
    // This ID is used by the actions attached to the list
    text.setSpan(new android.text.Annotation("id", annotation.getId()), 0, text.length(), 0);

    return text;
}

From source file:com.uwetrottmann.wpdisplay.ui.DisplayFragment.java

private void setText(TextView view, int labelResId, String value) {
    SpannableStringBuilder builder = new SpannableStringBuilder();

    builder.append(getString(labelResId));
    builder.setSpan(new TextAppearanceSpan(getActivity(), R.style.TextAppearance_AppCompat_Caption), 0,
            builder.length(), 0);//from   w ww  . j  a v a2  s.c  o  m

    builder.append("\n");

    int lengthOld = builder.length();
    builder.append(value);
    builder.setSpan(new TextAppearanceSpan(getActivity(), R.style.TextAppearance_AppCompat_Display1), lengthOld,
            builder.length(), 0);

    view.setText(builder);
}

From source file:com.uwetrottmann.wpdisplay.ui.DisplayFragment.java

private void setTemperature(TextView view, int labelResId, double value) {
    SpannableStringBuilder builder = new SpannableStringBuilder();

    builder.append(getString(labelResId));
    builder.setSpan(new TextAppearanceSpan(getActivity(), R.style.TextAppearance_AppCompat_Caption), 0,
            builder.length(), 0);/*  ww w.  j  a  v  a 2 s .  c om*/

    builder.append("\n");

    int lengthOld = builder.length();
    builder.append(String.format(Locale.getDefault(), "%.1f", value));
    builder.setSpan(new TextAppearanceSpan(getActivity(), R.style.TextAppearance_AppCompat_Display3), lengthOld,
            builder.length(), 0);

    lengthOld = builder.length();
    builder.append(getString(R.string.unit_celsius));
    builder.setSpan(new TextAppearanceSpan(getActivity(), R.style.TextAppearance_App_Unit), lengthOld,
            builder.length(), 0);

    view.setText(builder);
}

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

static CharSequence formatAttachmentTag(final String author, final String attachmentType) {
    final Context context = Factory.get().getApplicationContext();
    final TextAppearanceSpan notificationSecondaryText = new TextAppearanceSpan(context,
            R.style.NotificationSecondaryText);
    final SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder();
    if (!TextUtils.isEmpty(author)) {
        final TextAppearanceSpan notificationSenderSpan = new TextAppearanceSpan(context,
                R.style.NotificationSenderText);
        spannableStringBuilder.append(author);
        spannableStringBuilder.setSpan(notificationSenderSpan, 0, author.length(), 0);
        final String separator = context.getString(R.string.notification_separator);
        spannableStringBuilder.append(separator);
    }//from w  ww .  j  a v  a2 s  .  c  om
    final int start = spannableStringBuilder.length();
    // The default attachment type is an image, since that's what was originally
    // supported. When there's no content type, assume it's an image.
    int message = R.string.notification_picture;
    if (ContentType.isAudioType(attachmentType)) {
        message = R.string.notification_audio;
    } else if (ContentType.isVideoType(attachmentType)) {
        message = R.string.notification_video;
    } else if (ContentType.isVCardType(attachmentType)) {
        message = R.string.notification_vcard;
    }
    spannableStringBuilder.append(context.getText(message));
    spannableStringBuilder.setSpan(notificationSecondaryText, start, spannableStringBuilder.length(), 0);
    return spannableStringBuilder;
}

From source file:com.github.michalbednarski.intentslab.providerlab.proxy.LogViewerFragment.java

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    setListAdapter(new CursorAdapter(getActivity(), data, false) {
        @Override/*www.j  a v a  2  s  .  co m*/
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            return ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
                    .inflate(android.R.layout.simple_list_item_2, parent, false);
        }

        @Override
        public void bindView(View view, Context context, Cursor cursor) {
            // First row: method and app name
            final String appName = getActivity().getPackageManager().getNameForUid(cursor.getInt(3));
            final String methodName = methodIdToName(cursor.getInt(1));
            ((TextView) view.findViewById(android.R.id.text1)).setText(methodName + "() by " + appName);

            // Second row: uri and exception
            final String uri = cursor.getString(2);
            String exception = cursor.getString(4);
            SpannableStringBuilder text2 = new SpannableStringBuilder(uri);
            if (exception != null) {
                text2.append("\n");
                final int start = text2.length();
                text2.append(exception);
                final int end = text2.length();
                text2.setSpan(new ForegroundColorSpan(Color.RED), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            } else {
                // TODO: result / empty result warning
            }
            ((TextView) view.findViewById(android.R.id.text2)).setText(text2);
        }
    });
}

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

/**
 * Updates the policy viewer according to the ownership
 *//*  w w  w.java  2s .c o 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.appeaser.sublimepicker.Sampler.java

private SpannableStringBuilder applyBoldStyle(String text) {
    SpannableStringBuilder ss = new SpannableStringBuilder(text);
    ss.setSpan(new StyleSpan(Typeface.BOLD), 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    return ss;//from  w  ww.  ja  v a  2 s .com
}

From source file:com.oasisfeng.nevo.decorators.StackDecorator.java

@Override
public void apply(final StatusBarNotificationEvo evolved) throws RemoteException {
    final Collection<StatusBarNotificationEvo> history = getArchivedNotifications(evolved.getKey(),
            KMaxNumLines);//from   w  w w.j a  v  a 2  s.  c  om
    if (history.size() <= 1)
        return;
    final INotification evolved_n = evolved.notification();
    final IBundle evolved_extras = evolved_n.extras();
    if (evolved_extras.containsKey(EXTRA_TEXT_LINES))
        return; // Never stack already inbox-styled notification.

    final Calendar calendar = Calendar.getInstance();
    final List<CharSequence> lines = new ArrayList<>(KMaxNumLines);
    long previous_when = 0;
    final long latest_when = evolved_n.getWhen();
    for (final StatusBarNotificationEvo sbn : history) {
        final INotification n = sbn.notification();
        final CharSequence text = n.extras().getCharSequence(NotificationCompat.EXTRA_TEXT);
        if (text == null)
            continue;

        final long when = n.getWhen();
        if (when == latest_when || Math.abs(when - previous_when) <= KMinIntervalToShowTimestamp)
            lines.add(text);
        else { // Add time-stamp
            final SpannableStringBuilder line = new SpannableStringBuilder();
            calendar.setTimeInMillis(when);
            final String time_text = String.format((Locale) null, "%1$02d:%2$02d ", calendar.get(HOUR_OF_DAY),
                    calendar.get(MINUTE));
            line.append(time_text);
            line.append(text);
            line.setSpan(new StyleSpan(Typeface.BOLD), 0, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            lines.add(line);
        }
        previous_when = when;
    }
    if (lines.isEmpty())
        return;
    Collections.reverse(lines); // Latest first, since earliest lines will be trimmed by InboxStyle.

    final CharSequence title = evolved_extras.getCharSequence(NotificationCompat.EXTRA_TITLE);
    evolved_extras.putCharSequence(NotificationCompat.EXTRA_TITLE_BIG, title);
    evolved_extras.putCharSequenceArray(EXTRA_TEXT_LINES, lines);

    evolved_n.setBigContentView(buildBigContentView(evolved.getPackageName(), title, lines));
}

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

private static void endSpan(SpannableStringBuilder text, ThemeColors colors, boolean openSpoilers) {
    int len = text.length();
    Object obj = getLast(text, Span.class);
    int where = text.getSpanStart(obj);
    text.removeSpan(obj);/*w w  w  .  ja  v a  2 s .c o  m*/

    if (where != len) {
        Span s = (Span) obj;

        if (s.mStyle != null) {
            List<Object> styleSpans = parseStyleAttributes(s.mStyle);
            for (Object span : styleSpans) {
                text.setSpan(span, where, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }

        if (colors != null && s.mIsAibquote) {
            text.setSpan(new ForegroundColorSpan(colors.quoteForeground), where, len,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }

        if (colors != null && s.mIsAibspoiler) {
            if (openSpoilers) {
                text.setSpan(new ForegroundColorSpan(colors.spoilerForeground), where, len,
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                text.setSpan(new BackgroundColorSpan(colors.spoilerBackground), where, len,
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            } else {
                text.setSpan(new SpoilerSpan(colors.spoilerForeground, colors.spoilerBackground), where, len,
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }

        if (s.mIsUnderline) {
            text.setSpan(new UnderlineSpan(), where, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }

        if (s.mIsStrike) {
            text.setSpan(new StrikethroughSpan(), where, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }

    }
}

From source file:com.example.activity.ProfileActivity.java

private void setPlayer() {
    ImageLoader.getInstance().displayImage(playerModel.player.avatar_url, head, BeeFrameworkApp.options_head);
    name.setText(playerModel.player.name);
    location.setText(playerModel.player.location);

    shots_count.setText(playerModel.player.shots_count + "");
    likes_count.setText(playerModel.player.likes_count + "");
    following_count.setText(playerModel.player.following_count + "");
    followers_count.setText(playerModel.player.followers_count + "");

    net.setText(playerModel.player.website_url);

    CharSequence text = net.getText();
    if (text instanceof Spannable) {
        int end = text.length();
        Spannable sp = (Spannable) net.getText();
        URLSpan[] spans = sp.getSpans(0, end, URLSpan.class);
        SpannableStringBuilder style = new SpannableStringBuilder(text);
        style.clearSpans();// should clear old spans
        for (URLSpan span : spans) {
            JayceSpan mySpan = new JayceSpan(span.getURL());
            style.setSpan(mySpan, sp.getSpanStart(span), sp.getSpanEnd(span),
                    Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        }/*  w w  w  . jav a 2 s  .c  om*/
        net.setText(style);
    }
}