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:Main.java

public static void makeTextViewHyperlink(TextView tv) {
    SpannableStringBuilder ssb = new SpannableStringBuilder();
    ssb.append(tv.getText());// w w  w. j  a  v a  2  s . c  o m
    ssb.setSpan(new URLSpan("#"), 0, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    tv.setText(ssb, TextView.BufferType.SPANNABLE);
}

From source file:Main.java

/**
 * Returns a CharSequence containing a bulleted and properly indented list.
 *
 * @param leadingMargin//w w w  .java 2 s.c  o  m
 *            In pixels, the space between the left edge of the bullet and
 *            the left edge of the text.
 * @param lines
 *            An array of CharSequences. Each CharSequences will be a
 *            separate line/bullet-point.
 * @return
 */
public static CharSequence makeBulletList(int leadingMargin, CharSequence... lines) {
    SpannableStringBuilder sb = new SpannableStringBuilder();
    for (int i = 0; i < lines.length; i++) {
        CharSequence line = lines[i] + (i < lines.length - 1 ? "\n" : "");
        Spannable spannable = new SpannableString(line);
        spannable.setSpan(new BulletSpan(leadingMargin), 0, spannable.length(),
                Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        sb.append(spannable);
    }
    return sb;
}

From source file:Main.java

/**
 * Returns a CharSequence that concatenates the specified array of CharSequence
 * objects and then applies a list of zero or more tags to the entire range.
 *
 * @param content an array of character sequences to apply a style to
 * @param tags the styled span objects to apply to the content
 *        such as android.text.style.StyleSpan
 *
 *//* w ww . j  a  v a 2  s  .c  o  m*/
private static CharSequence apply(CharSequence[] content, Object... tags) {
    SpannableStringBuilder text = new SpannableStringBuilder();
    openTags(text, tags);
    for (CharSequence item : content) {
        text.append(item);
    }
    closeTags(text, tags);
    return text;
}

From source file:Main.java

public static void showChangelog(final Context context, final String title, final String appname,
        final int resChanges, final int resNotes) {
    final SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(context);
    final String v0 = p.getString(PREFS_LAST_RUN, "");
    String v1 = null;//w ww.j  a  v a  2  s .c  o  m
    try {
        v1 = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
    } catch (NameNotFoundException e) {
        Log.e(TAG, "package not found: " + context.getPackageName(), e);
    }
    p.edit().putString(PREFS_LAST_RUN, v1).commit();
    if (v0.length() == 0) {
        Log.d(TAG, "first boot, skip changelog");
        // return;
    }
    if (v0.equals(v1)) {
        Log.d(TAG, "no changes");
        return;
    }

    String[] changes = context.getResources().getStringArray(resChanges);
    String[] notes = resNotes > 0 ? context.getResources().getStringArray(resNotes) : null;

    final SpannableStringBuilder sb = new SpannableStringBuilder();
    for (String s : notes) {
        SpannableString ss = new SpannableString(s + "\n");
        int j = s.indexOf(":");
        if (j > 0) {
            if (!TextUtils.isEmpty(s)) {
                ss.setSpan(new StyleSpan(Typeface.BOLD), 0, j, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
        sb.append(ss);
        sb.append("\n");
    }
    if (notes != null && notes.length > 0) {
        sb.append("\n");
    }
    for (String s : changes) {
        s = appname + " " + s.replaceFirst(": ", ":\n* ").replaceAll(", ", "\n* ") + "\n";
        SpannableString ss = new SpannableString(s);
        int j = s.indexOf(":");
        if (j > 0) {
            ss.setSpan(new StyleSpan(Typeface.BOLD), 0, j, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        sb.append(ss);
        sb.append("\n");
    }
    sb.setSpan(new RelativeSizeSpan(0.75f), 0, sb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    changes = null;
    notes = null;

    AlertDialog.Builder b = new AlertDialog.Builder(context);
    b.setTitle(title);
    b.setMessage(sb);
    b.setCancelable(true);
    b.setPositiveButton(android.R.string.ok, null);
    b.show();
}

From source file:com.robomorphine.fragment.AboutDialogFragment.java

private CharSequence linkify(String text, String link) {
    SpannableStringBuilder builder = new SpannableStringBuilder();
    URLSpan span = new URLSpan(link);

    builder.append(text);/*from  ww w.  j av  a2s. c  o m*/
    builder.setSpan(span, 0, builder.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
    return builder;
}

From source file:Main.java

/**
 * Returns the bulleted list based on the given {@code lines}.
 * If one of lines starts with {@link BulletListUtil#BAD_FIRST_SYMBOLS}, then such symbol is
 * removed from there (sometimes bad lines are received from the Food2Work).
 * Also if line consists of the upper case words, then this line is used like a header and is
 * underlined.//from w  w  w .ja  v a 2  s . c om
 *
 * @param leadingMargin In pixels, the space between the left edge of the bullet and the left
 *                         edge of the text
 * @param lines List of strings. Each string will be a separate item in the bulleted list
 * @return The bulleted list based on the given {@code lines}
 */
public static CharSequence makeBulletList(List<String> lines, int leadingMargin) {
    List<Spanned> spanned = new ArrayList<>(lines.size());
    for (String line : lines) {
        if (!line.trim().isEmpty()) {
            Spanned spannedLine = Html.fromHtml(removeBadFirstCharacters(line.trim()));
            spanned.add(spannedLine);
        }
    }

    SpannableStringBuilder sb = new SpannableStringBuilder();
    for (int i = 0; i < spanned.size(); i++) {
        CharSequence line = spanned.get(i) + (i < spanned.size() - 1 ? "\n" : "");
        boolean underlineNeeded = isUpperCase(line);
        Spannable spannable = new SpannableString(line);
        if (underlineNeeded) {
            spannable.setSpan(new UnderlineSpan(), 0, spannable.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        } else {
            spannable.setSpan(new BulletSpan(leadingMargin), 0, spannable.length(),
                    Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        }
        sb.append(spannable);
    }
    return sb;
}

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

@Override
public CharSequence format(Context context, AccessibilityNodeInfoCompat node, AccessibilityEvent event) {
    SpannableStringBuilder output = new SpannableStringBuilder();

    final CharSequence nodeText = AccessibilityNodeInfoUtils.getNodeText(node);
    final CharSequence roleText = Role.getRoleDescriptionOrDefault(context, node);

    StringBuilderUtils.append(output, nodeText, roleText);

    return output;
}

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

@Override
public CharSequence format(Context context, AccessibilityNodeInfoCompat node, AccessibilityEvent event) {
    final SpannableStringBuilder output = new SpannableStringBuilder();
    final CharSequence text = (!TextUtils.isEmpty(node.getText())) ? node.getText()
            : AccessibilityEventUtils.getEventAggregateText(event);
    final CharSequence contentDescription = node.getContentDescription();
    final CharSequence roleText = Role.getRoleDescriptionOrDefault(context, node);

    // Prepend any contentDescription, if present
    StringBuilderUtils.appendWithSeparator(output, contentDescription);

    // Append node or event text
    StringBuilderUtils.append(output, text, roleText);

    // The text should contain the current state.  Explicitly speak state for ToggleButtons.
    if (TextUtils.isEmpty(text) || Role.getRole(node) == Role.ROLE_TOGGLE_BUTTON) {
        final CharSequence state = context
                .getString(node.isChecked() ? R.string.value_checked : R.string.value_not_checked);
        StringBuilderUtils.appendWithSeparator(output, state);
    }/* w  ww  .  j  av  a  2s.c  o  m*/

    return output;
}

From source file:com.google.android.marvin.mytalkback.speechrules.RuleSeekBar.java

@Override
public CharSequence format(Context context, AccessibilityNodeInfoCompat node, AccessibilityEvent event) {
    final SpannableStringBuilder output = new SpannableStringBuilder();
    final CharSequence text = super.format(context, node, event);
    final CharSequence formattedText = context.getString(R.string.template_seek_bar, text);

    StringBuilderUtils.appendWithSeparator(output, formattedText);

    // TODO: We need to be getting this information from the node.
    if ((event != null) && (event.getItemCount() > 0)) {
        final int percent = (100 * event.getCurrentItemIndex()) / event.getItemCount();
        final CharSequence formattedPercent = context.getString(R.string.template_percent, percent);

        StringBuilderUtils.appendWithSeparator(output, formattedPercent);
    }/*from w  w w.  ja v a 2 s .  c  o  m*/

    return output;
}

From source file:com.android.talkback.formatter.TouchExplorationSystemUiFormatter.java

@Override
public boolean format(AccessibilityEvent event, TalkBackService context, Utterance utterance) {
    final SpannableStringBuilder recordText = new SpannableStringBuilder();
    final List<CharSequence> entries = AccessibilityEventCompat.getRecord(event, 0).getText();

    // Reverse the entries so that time is read aloud first.
    Collections.reverse(entries);

    for (final CharSequence entry : entries) {
        StringBuilderUtils.appendWithSeparator(recordText, entry);
    }/*from   w  w w  .  j  a  v  a2  s. c o  m*/

    // Don't populate with empty text. This should never happen!
    if (TextUtils.isEmpty(recordText))
        return false;

    // Don't speak the same utterance twice.
    if (TextUtils.equals(mLastUtteranceText, recordText))
        return false;

    utterance.addSpoken(recordText);
    utterance.addHaptic(R.array.view_hovered_pattern);
    utterance.addAuditory(R.raw.focus);

    mLastUtteranceText.clear();
    mLastUtteranceText.append(recordText);

    return true;
}