Example usage for android.text SpannableStringBuilder append

List of usage examples for android.text SpannableStringBuilder append

Introduction

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

Prototype

public SpannableStringBuilder append(char text) 

Source Link

Usage

From source file:piuk.blockchain.android.util.WalletUtils.java

public static Editable formatAddress(final String address, final int groupSize, final int lineSize) {
    final SpannableStringBuilder builder = new SpannableStringBuilder();

    final int len = address.length();
    for (int i = 0; i < len; i += groupSize) {
        final int end = i + groupSize;
        final String part = address.substring(i, end < len ? end : len);

        builder.append(part);
        builder.setSpan(new TypefaceSpan("monospace"), builder.length() - part.length(), builder.length(),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        if (end < len) {
            final boolean endOfLine = end % lineSize == 0;
            builder.append(endOfLine ? "\n" : Constants.THIN_SPACE);
        }//from   w  w w  .  j a v  a 2  s.  co m
    }

    return builder;
}

From source file:org.thoughtcrime.SMP.notifications.MessageNotifier.java

private static void sendSingleThreadNotification(Context context, MasterSecret masterSecret,
        NotificationState notificationState, boolean signal) {
    if (notificationState.getNotifications().isEmpty()) {
        ((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE)).cancel(NOTIFICATION_ID);
        return;//from  ww  w .j a va 2s. c  o m
    }

    List<NotificationItem> notifications = notificationState.getNotifications();
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    Recipient recipient = notifications.get(0).getIndividualRecipient();
    Drawable recipientPhoto = recipient.getContactPhoto();
    int largeIconTargetSize = context.getResources().getDimensionPixelSize(R.dimen.contact_photo_target_size);

    if (recipientPhoto != null) {
        Bitmap recipientPhotoBitmap = BitmapUtil.createFromDrawable(recipientPhoto, largeIconTargetSize,
                largeIconTargetSize);
        if (recipientPhotoBitmap != null)
            builder.setLargeIcon(recipientPhotoBitmap);
    }

    builder.setSmallIcon(R.drawable.icon_notification);
    builder.setColor(context.getResources().getColor(R.color.textsecure_primary));
    builder.setContentTitle(recipient.toShortString());
    builder.setContentText(notifications.get(0).getText());
    builder.setContentIntent(notifications.get(0).getPendingIntent(context));
    builder.setContentInfo(String.valueOf(notificationState.getMessageCount()));
    builder.setPriority(NotificationCompat.PRIORITY_HIGH);
    builder.setNumber(notificationState.getMessageCount());
    builder.setCategory(NotificationCompat.CATEGORY_MESSAGE);
    builder.setDeleteIntent(
            PendingIntent.getBroadcast(context, 0, new Intent(DeleteReceiver.DELETE_REMINDER_ACTION), 0));
    if (recipient.getContactUri() != null)
        builder.addPerson(recipient.getContactUri().toString());

    long timestamp = notifications.get(0).getTimestamp();
    if (timestamp != 0)
        builder.setWhen(timestamp);

    if (masterSecret != null) {
        Action markAsReadAction = new Action(R.drawable.check,
                context.getString(R.string.MessageNotifier_mark_as_read),
                notificationState.getMarkAsReadIntent(context, masterSecret));
        builder.addAction(markAsReadAction);
        builder.extend(new NotificationCompat.WearableExtender().addAction(markAsReadAction));
    }

    SpannableStringBuilder content = new SpannableStringBuilder();

    ListIterator<NotificationItem> iterator = notifications.listIterator(notifications.size());
    while (iterator.hasPrevious()) {
        NotificationItem item = iterator.previous();
        content.append(item.getBigStyleSummary());
        content.append('\n');
    }

    builder.setStyle(new BigTextStyle().bigText(content));

    setNotificationAlarms(context, builder, signal, notificationState.getRingtone(),
            notificationState.getVibrate());

    if (signal) {
        builder.setTicker(notifications.get(0).getTickerText());
    }

    ((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE)).notify(NOTIFICATION_ID,
            builder.build());
}

From source file:com.google.android.apps.common.testing.accessibility.framework.AccessibilityCheckUtils.java

/**
 * Retrieve text for a {@link View}, which may include text from the children of the {@code View}.
 * This text is an approximation of, but not identical to, what TalkBack would speak for the
 * {@link View}. One difference is that there are no separators between the speakable text from
 * different nodes./*from  w w  w  . j  a va 2 s  .co m*/
 * <p>
 * TalkBack also will not speak {@link View}s that aren't visible. This method assumes that the
 * {@link View} passed in is visible. The visibility of the rest of child nodes is inferred from
 * {@code view.getVisibility}.
 *
 * @param view The {@link View} whose text should be returned.
 *
 * @return Speakable text derived from the {@link View} and its children. Returns an empty string
 *         if there is no such text, and {@code null} if {@code view == null}.
 */
static CharSequence getSpeakableTextForView(View view) {
    if (view == null) {
        return null;
    }

    View labelForThisView = ViewAccessibilityUtils.getLabelForView(view);
    if (labelForThisView != null) {
        return getSpeakableTextForView(labelForThisView);
    }

    SpannableStringBuilder returnStringBuilder = new SpannableStringBuilder("");

    // Accessibility importance is considered only on Jelly Bean and above
    if ((Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN)
            || ViewAccessibilityUtils.isImportantForAccessibility(view)) {
        if (!TextUtils.isEmpty(view.getContentDescription())) {
            // contentDescription always wins out over other properties
            return view.getContentDescription();
        }
        if (view instanceof TextView) {
            if (!TextUtils.isEmpty(((TextView) view).getText())) {
                returnStringBuilder.append(((TextView) view).getText());
            } else if (!TextUtils.isEmpty(((TextView) view).getHint())) {
                returnStringBuilder.append(((TextView) view).getHint());
            }
        }
    }

    if (view instanceof ViewGroup) {
        ViewGroup group = (ViewGroup) view;
        // TODO(sjrush): Only evaluate child views if they're importantForAccessibility.
        for (int i = 0; i < group.getChildCount(); ++i) {
            View childView = group.getChildAt(i);
            if ((childView.getVisibility() == View.VISIBLE)
                    && !ViewAccessibilityUtils.isActionableForAccessibility(childView)) {
                returnStringBuilder.append(getSpeakableTextForView(childView));
            }
        }
    }

    if (view instanceof CompoundButton) {
        if (((CompoundButton) view).isChecked()) {
            StringBuilderUtils.appendWithSeparator(returnStringBuilder, "Checked");
        } else {
            StringBuilderUtils.appendWithSeparator(returnStringBuilder, "Not checked");
        }
    }
    return returnStringBuilder;
}

From source file:com.zulip.android.util.CustomHtmlToSpannedConverter.java

private static void handleP(SpannableStringBuilder text) {
    int len = text.length();

    if (len >= 1 && text.charAt(len - 1) == '\n') {
        if (len >= 2 && text.charAt(len - 2) == '\n') {
            return;
        }//  w  w  w.  j a  v a2s. c  o m

        text.append("\n");
        return;
    }

    if (len != 0) {
        text.append("\n\n");
    }
}

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

private static void handleBr(SpannableStringBuilder text) {
    text.append("\n");
}

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

private static void handleTr(SpannableStringBuilder text, boolean open) {
    text.append(open ? "| " : "\n");
}

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

private static void handleTd(SpannableStringBuilder text, boolean open) {
    if (!open)// ww  w. j  a  va 2 s . c  o m
        text.append(" | ");
}

From source file:com.zulip.android.util.CustomHtmlToSpannedConverter.java

private static void startImg(SpannableStringBuilder text, Attributes attributes, Html.ImageGetter img) {
    String src = attributes.getValue("", "src");
    Drawable d = null;/*from ww w .j  ava2  s. c o m*/

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

    if (d == null) {
        // don't draw anything
        return;
    }

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

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

From source file:io.github.hidroh.materialistic.widget.preference.HelpListView.java

@Override
protected void onFinishInflate() {
    super.onFinishInflate();
    ((TextView) findViewById(R.id.item_new).findViewById(R.id.rank)).append(makeAsteriskSpan());
    SpannableString spannable = new SpannableString("+5");
    spannable.setSpan(new SuperscriptSpan(), 0, spannable.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
    spannable.setSpan(new RelativeSizeSpan(0.6f), 0, spannable.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
    spannable.setSpan(new ForegroundColorSpan(ContextCompat.getColor(getContext(), R.color.greenA700)), 0,
            spannable.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
    ((TextView) findViewById(R.id.item_promoted).findViewById(R.id.rank)).append(spannable);
    TextView comments = (TextView) findViewById(R.id.item_new_comments).findViewById(R.id.comment);
    SpannableStringBuilder sb = new SpannableStringBuilder("46");
    sb.append(makeAsteriskSpan());
    comments.setText(sb);/*from w  w w .ja v  a  2 s .co  m*/
}

From source file:com.todoroo.astrid.adapter.UpdateAdapter.java

public static Spanned getUpdateComment(final AstridActivity context, UserActivity activity, User user,
        String linkColor, String fromView) {
    String userDisplay;/*w ww .j  a v  a  2 s  .  c  om*/
    if (activity.getValue(UserActivity.USER_UUID).equals(Task.USER_ID_SELF)) {
        userDisplay = context.getString(R.string.update_string_user_self);
    } else if (user == null) {
        userDisplay = context.getString(R.string.ENA_no_user);
    } else {
        userDisplay = user.getDisplayName(USER_NAME, USER_FIRST_NAME, USER_LAST_NAME);
    }
    if (TextUtils.isEmpty(userDisplay))
        userDisplay = context.getString(R.string.ENA_no_user);
    String targetName = activity.getValue(UserActivity.TARGET_NAME);
    String action = activity.getValue(UserActivity.ACTION);
    String message = activity.getValue(UserActivity.MESSAGE);

    int commentResource = 0;
    if (UserActivity.ACTION_TASK_COMMENT.equals(action)) {
        if (fromView.equals(FROM_TASK_VIEW) || TextUtils.isEmpty(targetName))
            commentResource = R.string.update_string_default_comment;
        else
            commentResource = R.string.update_string_task_comment;
    } else if (UserActivity.ACTION_TAG_COMMENT.equals(action)) {
        if (fromView.equals(FROM_TAG_VIEW) || TextUtils.isEmpty(targetName))
            commentResource = R.string.update_string_default_comment;
        else
            commentResource = R.string.update_string_tag_comment;
    }

    if (commentResource == 0)
        return Html.fromHtml(String.format("%s %s", userDisplay, message)); //$NON-NLS-1$

    String original = context.getString(commentResource, userDisplay, targetName, message);
    int taskLinkIndex = original.indexOf(TARGET_LINK_PREFIX);

    if (taskLinkIndex < 0)
        return Html.fromHtml(original);

    String[] components = original.split(" "); //$NON-NLS-1$
    SpannableStringBuilder builder = new SpannableStringBuilder();
    StringBuilder htmlStringBuilder = new StringBuilder();

    for (String comp : components) {
        Matcher m = TARGET_LINK_PATTERN.matcher(comp);
        if (m.find()) {
            builder.append(Html.fromHtml(htmlStringBuilder.toString()));
            htmlStringBuilder.setLength(0);

            String linkType = m.group(1);
            CharSequence link = getLinkSpan(context, activity, targetName, linkColor, linkType);
            if (link != null) {
                builder.append(link);
                if (!m.hitEnd()) {
                    builder.append(comp.substring(m.end()));
                }
                builder.append(' ');
            }
        } else {
            htmlStringBuilder.append(comp);
            htmlStringBuilder.append(' ');
        }
    }

    if (htmlStringBuilder.length() > 0)
        builder.append(Html.fromHtml(htmlStringBuilder.toString()));

    return builder;
}