Example usage for android.text SpannableStringBuilder SpannableStringBuilder

List of usage examples for android.text SpannableStringBuilder SpannableStringBuilder

Introduction

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

Prototype

public SpannableStringBuilder() 

Source Link

Document

Create a new SpannableStringBuilder with empty contents

Usage

From source file:com.googlecode.eyesfree.brailleback.NodeBrailler.java

/**
 * Converts the source of {@code event} and its surroundings to
 * annotated text to put on the braille display.
 * Returns the new content, or {@code null} if the event doesn't have
 * a source node.//from   ww w.  j av a  2 s  .co  m
 */
public DisplayManager.Content brailleNode(AccessibilityNodeInfoCompat node) {
    DisplayManager.Content content = mSelfBrailleManager.contentForNode(node);
    if (content == null) {
        ArrayList<AccessibilityNodeInfoCompat> toFormat = new ArrayList<AccessibilityNodeInfoCompat>();
        findNodesToFormat(node, toFormat);
        LogUtils.log(this, Log.VERBOSE, "Going to format %d nodes", toFormat.size());
        SpannableStringBuilder sb = new SpannableStringBuilder();
        for (AccessibilityNodeInfoCompat n : toFormat) {
            formatSubtree(n, sb);
        }
        content = new DisplayManager.Content(sb);
        content.setFirstNode(toFormat.get(0)).setLastNode(toFormat.get(toFormat.size() - 1));
        AccessibilityNodeInfoUtils.recycleNodes(toFormat);
    }
    return content;
}

From source file:com.schautup.fragments.AboutDialogFragment.java

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Get app version
    PackageManager pm = getActivity().getPackageManager();
    String packageName = getActivity().getPackageName();
    String versionName;/*from   ww w  . j  a v a 2s  .c o m*/
    String appName = getString(R.string.application_name);
    try {
        PackageInfo info = pm.getPackageInfo(packageName, 0);
        versionName = info.versionName;
    } catch (PackageManager.NameNotFoundException e) {
        versionName = VERSION_UNAVAILABLE;
    }

    // About.
    SpannableStringBuilder aboutBody = new SpannableStringBuilder();
    aboutBody.append(Html.fromHtml(getString(R.string.about_body, appName, versionName)));

    // Licenses.
    SpannableString licensesLink = new SpannableString(getString(R.string.about_licenses));
    licensesLink.setSpan(new ClickableSpan() {
        @Override
        public void onClick(View view) {
            showOpenSourceLicenses(getActivity());
        }
    }, 0, licensesLink.length(), 0);
    aboutBody.append("\n\n");
    aboutBody.append(licensesLink);

    // End User License Agreement.
    SpannableString eulaLink = new SpannableString(getString(R.string.about_eula));
    eulaLink.setSpan(new ClickableSpan() {
        @Override
        public void onClick(View view) {
            showEula(getActivity());
        }
    }, 0, eulaLink.length(), 0);
    aboutBody.append("\n\n");
    aboutBody.append(eulaLink);

    // Show "About" dialog.
    LayoutInflater layoutInflater = (LayoutInflater) getActivity()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    TextView aboutBodyView = (TextView) layoutInflater.inflate(R.layout.dialog_about, null);
    aboutBodyView.setText(aboutBody);
    aboutBodyView.setMovementMethod(new LinkMovementMethod());

    return new AlertDialog.Builder(getActivity()).setTitle(R.string.lbl_about).setView(aboutBodyView)
            .setPositiveButton(R.string.btn_ok, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    dialog.dismiss();
                }
            }).create();
}

From source file:org.telegram.ui.Cells.BotHelpCell.java

public void setText(String text) {
    if (text == null || text.length() == 0) {
        setVisibility(GONE);/*from   w w w .java2s. c  om*/
        return;
    }
    if (text != null && oldText != null && text.equals(oldText)) {
        return;
    }
    oldText = text;
    setVisibility(VISIBLE);
    if (AndroidUtilities.isTablet()) {
        width = (int) (AndroidUtilities.getMinTabletSide() * 0.7f);
    } else {
        width = (int) (Math.min(AndroidUtilities.displaySize.x, AndroidUtilities.displaySize.y) * 0.7f);
    }
    SpannableStringBuilder stringBuilder = new SpannableStringBuilder();
    String help = LocaleController.getString("BotInfoTitle", R.string.BotInfoTitle);
    stringBuilder.append(help);
    stringBuilder.append("\n\n");
    stringBuilder.append(text);
    MessageObject.addLinks(stringBuilder);
    stringBuilder.setSpan(new TypefaceSpan(AndroidUtilities.getTypeface("fonts/rmedium.ttf")), 0, help.length(),
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    Emoji.replaceEmoji(stringBuilder, textPaint.getFontMetricsInt(), AndroidUtilities.dp(20), false);
    try {
        textLayout = new StaticLayout(stringBuilder, textPaint, width, Layout.Alignment.ALIGN_NORMAL, 1.0f,
                0.0f, false);
        width = 0;
        height = textLayout.getHeight() + AndroidUtilities.dp(4 + 18);
        int count = textLayout.getLineCount();
        for (int a = 0; a < count; a++) {
            width = (int) Math.ceil(Math.max(width, textLayout.getLineWidth(a) + textLayout.getLineLeft(a)));
        }
    } catch (Exception e) {
        FileLog.e("tmessage", e);
    }
    width += AndroidUtilities.dp(4 + 18);
}

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   ww w  .  ja v  a2 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.android.screenspeak.speechrules.RuleEditText.java

@Override
public CharSequence getHintText(Context context, AccessibilityNodeInfoCompat node) {
    // Disabled items don't have any hint text.
    if (!node.isEnabled()) {
        return context.getString(R.string.value_disabled);
    }/*from w  ww  .j  a v  a  2 s. co  m*/

    final SpannableStringBuilder builder = new SpannableStringBuilder();
    StringBuilderUtils.appendWithSeparator(builder,
            NodeHintHelper.getHintString(context, R.string.template_hint_edit_text));
    final CharSequence defaultHint = NodeHintHelper.getDefaultHintString(context, node);

    if (!TextUtils.isEmpty(defaultHint)) {
        StringBuilderUtils.appendWithSeparator(builder, defaultHint);
    }

    return builder;
}

From source file:net.sf.fakenames.fddemo.PermissionActivity.java

private CharSequence toLabel(int uid, String[] packages) {
    final SpannableStringBuilder ssb = new SpannableStringBuilder();

    if (packages != null) {
        try {//from  w  w w  .ja va  2 s  .c om
            final PackageManager pm = getPackageManager();

            if (packages.length == 1) {
                final ApplicationInfo soleInfo = pm.getApplicationInfo(packages[0], 0);
                if (soleInfo != null) {
                    final CharSequence label = soleInfo.loadLabel(pm);

                    if (!TextUtils.isEmpty(label)) {
                        ssb.append(label);
                        setBold(ssb, 0, ssb.length());
                        return ssb;
                    }
                }

                ssb.append(packages[0]);
                setBold(ssb, 0, ssb.length());
                return ssb;
            } else {
                Arrays.sort(packages);

                int named = 0;

                ssb.append("UID ").append(String.valueOf(uid)).append(" (");

                final ApplicationInfo firstInfo = pm.getApplicationInfo(packages[0], 0);

                if (firstInfo != null) {
                    final CharSequence label = firstInfo.loadLabel(pm);

                    if (!TextUtils.isEmpty(label)) {
                        ++named;

                        int old = ssb.length();
                        ssb.append(label);
                        setBold(ssb, old, ssb.length());
                    }
                }

                final int maxSummary = Math.min(packages.length, 3);
                int i;
                for (i = 1; i < maxSummary; ++i) {
                    final ApplicationInfo appInfo = pm.getApplicationInfo(packages[i], 0);

                    if (appInfo != null) {
                        final CharSequence label = appInfo.loadLabel(pm);

                        if (!TextUtils.isEmpty(label)) {
                            ++named;

                            ssb.append(", ");
                            int old = ssb.length();
                            ssb.append(label);
                            setBold(ssb, old, ssb.length());
                        }
                    }
                }

                if (named == 0) {
                    ssb.append(String.valueOf(packages.length)).append(" packages)");
                    setBold(ssb, 0, ssb.length());
                } else {
                    if (packages.length <= 4) {
                        ssb.append(")");
                    } else {
                        ssb.append(" and ");
                        ssb.append(String.valueOf(packages.length - named));
                        ssb.append(" others)");
                    }
                }

                return ssb;
            }
        } catch (PackageManager.NameNotFoundException ignored) {
        }
    }

    ssb.append("UID ").append(String.valueOf(uid));
    setBold(ssb, 0, ssb.length());
    return ssb;
}

From source file:com.android.talkback.speechrules.RuleNonTextViews.java

@Override
public CharSequence format(Context context, AccessibilityNodeInfoCompat node, AccessibilityEvent event) {
    final CharSequence text = AccessibilityNodeInfoUtils.getNodeText(node);
    CharSequence labelText = null;
    if (!TextUtils.isEmpty(text)) {
        labelText = text;/*  w w w .  j a v a2s  . co m*/
    } else {
        if (mLabelManager != null) {
            // Check to see if a custom label exists for the unlabeled control.
            Label label = mLabelManager.getLabelForViewIdFromCache(node.getViewIdResourceName());
            if (label != null) {
                labelText = label.getText();
            }
        }
    }

    // If the node's role is ROLE_IMAGE and the node is selectable, there's a non-trivial chance
    // that the node acts like a button (but we can't be sure). In this case, it is safest to
    // not append any role text to avoid confusing the user.
    CharSequence roleText;
    if (Role.getRole(node) == Role.ROLE_IMAGE
            && AccessibilityNodeInfoUtils.supportsAction(node, AccessibilityNodeInfoCompat.ACTION_SELECT)) {
        roleText = node.getRoleDescription(); // But don't fall back to default text.
    } else {
        roleText = Role.getRoleDescriptionOrDefault(context, node);
    }

    CharSequence unlabelledState;
    if (labelText == null) {
        unlabelledState = context.getString(R.string.value_unlabelled);
    } else {
        unlabelledState = null;
    }

    SpannableStringBuilder output = new SpannableStringBuilder();
    StringBuilderUtils.append(output, labelText, roleText);
    StringBuilderUtils.append(output, unlabelledState);

    return output;
}

From source file:de.azapps.mirakel.new_ui.adapter.AutoCompleteAdapter.java

@Override
public void bindView(View view, Context context, Cursor cursor) {
    final AutocompleteViewHolder viewHolder = (AutocompleteViewHolder) view.getTag();
    final SearchObject searchObject = new SearchObject(cursor);
    // This is ok for tasks and a fallback variant for tags
    viewHolder.taskName.setText(searchObject.getName());
    switch (searchObject.getAutocompleteType()) {
    case TASK:/*w  ww .  j  a v  a 2 s .  c  o  m*/
        if (searchObject.isDone()) {
            viewHolder.taskName
                    .setPaintFlags(viewHolder.taskName.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
        } else {
            viewHolder.taskName
                    .setPaintFlags(viewHolder.taskName.getPaintFlags() & ~Paint.STRIKE_THRU_TEXT_FLAG);
        }
        break;
    case TAG:
        final Tag tag = new Tag(searchObject.getObjId(), searchObject.getName(),
                searchObject.getBackgroundColor(), false);
        TagSpan tagSpan = new TagSpan(tag, context);
        SpannableStringBuilder stringBuilder = new SpannableStringBuilder();
        stringBuilder.append(new SpannableString(tag.getName()));
        stringBuilder.setSpan(tagSpan, 0, tag.getName().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        viewHolder.taskName.setText(stringBuilder, TextView.BufferType.SPANNABLE);
        break;
    }
}

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

public CustomHtmlToSpannedConverter(String source, Html.ImageGetter imageGetter, Html.TagHandler tagHandler,
        Parser parser, Html.ImageGetter emojiGetter, String baseUri, Context context) {
    mSource = source;/*from w w  w .  java 2  s  . c o  m*/
    mSpannableStringBuilder = new SpannableStringBuilder();
    mImageGetter = imageGetter;
    mTagHandler = tagHandler;
    mReader = parser;
    mEmojiGetter = emojiGetter;
    mBaseUri = baseUri;
    userMentionColor = ContextCompat.getColor(context, R.color.dark_red);
    userMentionSelfColor = ContextCompat.getColor(context, R.color.dark_blue);
}

From source file:codepath.watsiapp.utils.Util.java

/**
 * Sets a hyperlink style to the textview.
 *///from  ww w  . jav a2  s  .  c o m
public static void makeTextViewHyperlink(TextView tv) {
    SpannableStringBuilder ssb = new SpannableStringBuilder();
    ssb.append(tv.getText());
    ssb.setSpan(new URLSpan("#"), 0, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    tv.setText(ssb, TextView.BufferType.SPANNABLE);
}