Example usage for android.text Spannable setSpan

List of usage examples for android.text Spannable setSpan

Introduction

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

Prototype

public void setSpan(Object what, int start, int end, int flags);

Source Link

Document

Attach the specified markup object to the range start…end of the text, or move the object to that range if it was already attached elsewhere.

Usage

From source file:Main.java

private static void addStyleSpan(@Nonnull final Spannable text, final CharacterStyle style) {
    text.removeSpan(style);//from   ww w.  java2s.  c o  m
    text.setSpan(style, 0, text.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}

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

/**
 * Marks the whole of {@code spannable} as containing the content
 * coming from {@code node}.  A copy of {@code node} is stored.
 *
 * @see #recycleSpans/*from  w  w  w.  jav a 2s.  c  om*/
 */
public static void setAccessibilityNode(Spannable spannable, AccessibilityNodeInfoCompat node) {
    spannable.setSpan(AccessibilityNodeInfoCompat.obtain(node), 0, spannable.length(),
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}

From source file:com.benext.thibault.appsample.notification.builder.NotificationBuilder.java

private static Spannable getSpannableString(Context context, int strId, int typeface) {

    Spannable startersTitle = new SpannableString(context.getString(strId));
    startersTitle.setSpan(new StyleSpan(typeface), 0, startersTitle.length(),
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    return startersTitle;
}

From source file:com.dimasdanz.kendalipintu.util.CommonUtilities.java

public static void generateNotification(Context context, String message, String time, String info) {
    try {/*from   w  w w.  j ava  2 s .c  o  m*/
        NotificationManager manager;
        int notificationID = 73;

        Bitmap largeIcon = BitmapFactory.decodeResource(context.getResources(),
                R.drawable.ic_stat_notification);

        Notification.Builder builder = new Notification.Builder(context);
        Intent resultIntent = new Intent(context, LogActivity.class);
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);

        stackBuilder.addParentStack(LogActivity.class);
        stackBuilder.addNextIntent(resultIntent);

        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        Spannable sb = new SpannableString(message + " " + time + "-" + info);
        sb.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, message.length(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        builder.setAutoCancel(true);
        builder.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND);
        builder.setContentTitle(context.getString(R.string.notification_title));
        builder.setContentText(sb);
        builder.setTicker(context.getString(R.string.notification_ticker));
        builder.setNumber(++msgCounter);
        builder.setSmallIcon(R.drawable.ic_stat_notification);
        builder.setLargeIcon(largeIcon);
        builder.setContentIntent(resultPendingIntent);

        if (msgCounter > 1) {
            Notification.InboxStyle inboxStyle = new Notification.InboxStyle();
            if (msgCounter > 6) {
                name[0] = new SpannableString("...");
                name[1] = name[2];
                name[2] = name[3];
                name[3] = name[4];
                name[4] = name[5];
                name[5] = sb;
            } else {
                name[msgCounter - 1] = sb;
            }

            inboxStyle.setBigContentTitle(context.getString(R.string.notification_title));
            inboxStyle.setSummaryText(
                    msgCounter + " " + context.getString(R.string.notification_title) + " Baru");

            for (int i = name.length; i > 0; i--) {
                inboxStyle.addLine(name[i - 1]);
            }

            builder.setStyle(inboxStyle);
            builder.setContentText(msgCounter + " " + context.getString(R.string.notification_title) + " Baru");
        } else {
            name[0] = sb;
        }
        manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(notificationID, builder.build());
    } catch (NullPointerException e) {
        e.printStackTrace();
    }

}

From source file:com.benext.thibault.appsample.notification.builder.NotificationBuilder.java

public static NotificationCompat.Builder buildNotificationExtenderPages(Context context) {

    ArrayList<Notification> pages = new ArrayList<>();

    // Create first page notification
    Bitmap tableImg = BitmapFactory.decodeResource(context.getResources(), R.drawable.resto);
    Notification page1 = new NotificationCompat.Builder(context).extend(
            new NotificationCompat.WearableExtender().setHintShowBackgroundOnly(true).setBackground(tableImg))
            .build();//from ww  w  .  j a v a2  s. c  o  m

    pages.add(page1);

    // Create second page notification
    Spannable meatTitle = new SpannableString(context.getString(R.string.menu_meats));
    meatTitle.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, meatTitle.length(),
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    NotificationCompat.InboxStyle secondPageStyle = new NotificationCompat.InboxStyle();
    secondPageStyle.setBigContentTitle("Page 2").setSummaryText("")
            .addLine(getSpannableString(context, R.string.menu_starters, Typeface.BOLD))
            .addLine(context.getString(R.string.menu_starters_content1))
            .addLine(context.getString(R.string.menu_starters_content2))
            .addLine(getSpannableString(context, R.string.menu_meats, Typeface.BOLD))
            .addLine(context.getString(R.string.menu_meats_content1))
            .addLine(context.getString(R.string.menu_meats_content2));

    Notification page2 = new NotificationCompat.Builder(context)
            .extend(new NotificationCompat.WearableExtender().setBackground(
                    BitmapFactory.decodeResource(context.getResources(), R.drawable.table_restaurant)))
            .setStyle(secondPageStyle).build();

    pages.add(page2);

    // Create builder for the main notification
    Bitmap restoImg = BitmapFactory.decodeResource(context.getResources(), R.drawable.resto);
    return (NotificationCompat.Builder) buildNotificationSimpleNBackground(context)
            .setStyle(new NotificationCompat.BigPictureStyle().bigPicture(restoImg))
            .extend(new NotificationCompat.WearableExtender().addPages(pages));
}

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

private static void setBold(Spannable s, int start, int end) {
    s.setSpan(new StyleSpan(Typeface.BOLD), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}

From source file:Main.java

public static Spanned toSpanned(final Context context, final String toastString,
        @ColorRes final int toastColor) {
    final String string = "" + context.getResources().getColor(toastColor);
    final Spannable spannable = (Spannable) Html
            .fromHtml(colorize_a(colorize_em(toastString, string, true), string));
    for (final URLSpan urlSpan : (URLSpan[]) spannable.getSpans(0, spannable.length(), (Class) URLSpan.class)) {
        spannable.setSpan(urlSpan, spannable.getSpanStart(urlSpan), spannable.getSpanEnd(urlSpan), 0);
    }//from w w  w .j  a  v a  2s .c o  m
    return spannable;
}

From source file:Main.java

/**
 * Apply only the spans from src to dst specific by spans.
 *
 * @see {@link android.text.TextUtils#copySpansFrom}
 *///  w ww.ja  v a 2 s  . c o  m
public static void copySpans(@NonNull Spanned src, @NonNull Spannable dst, @NonNull Collection<Object> spans) {
    for (Object span : spans) {
        int start = src.getSpanStart(span);
        int end = src.getSpanEnd(span);
        int flags = src.getSpanFlags(span);
        dst.setSpan(span, start, end, flags);
    }
}

From source file:Main.java

public static CharSequence highlightText(String search, String originalText) {
    if (search != null && !search.equalsIgnoreCase("")) {
        String normalizedText = Normalizer.normalize(originalText, Normalizer.Form.NFD)
                .replaceAll("\\p{InCombiningDiacriticalMarks}+", "").toLowerCase();
        int start = normalizedText.indexOf(search);
        if (start < 0) {
            return originalText;
        } else {//from w w  w .  j ava  2  s  . com
            Spannable highlighted = new SpannableString(originalText);
            while (start >= 0) {
                int spanStart = Math.min(start, originalText.length());
                int spanEnd = Math.min(start + search.length(), originalText.length());
                highlighted.setSpan(new ForegroundColorSpan(Color.BLUE), spanStart, spanEnd,
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                start = normalizedText.indexOf(search, spanEnd);
            }
            return highlighted;
        }
    }
    return originalText;
}

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

/**
 * Marks a portion of {@code spannable} as containing text selection.  If
 * {@code start} and {@code end} are equal, then then added span marks a
 * cursor.//  w  w  w .  j  av  a  2  s .  c o m
 */
public static void addSelection(Spannable spannable, int start, int end) {
    int flags;
    if (start == end) {
        flags = Spanned.SPAN_EXCLUSIVE_INCLUSIVE;
    } else {
        flags = Spanned.SPAN_EXCLUSIVE_EXCLUSIVE;
    }
    spannable.setSpan(new SelectionSpan(), start, end, flags);
}