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.nttec.everychan.ui.presentation.HtmlParser.java

private static void end(SpannableStringBuilder text, Class<?> kind, Object repl) {
    int len = text.length();
    Object obj = getLast(text, kind);
    int where = text.getSpanStart(obj);

    text.removeSpan(obj);// www.  jav a 2 s . com

    if (where != len) {
        text.setSpan(repl, where, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
}

From source file:com.anysoftkeyboard.ui.settings.AboutAnySoftKeyboardFragment.java

@Override
public void onViewStateRestored(Bundle savedInstanceState) {
    super.onViewStateRestored(savedInstanceState);
    TextView additionalSoftware = (TextView) getView().findViewById(R.id.about_legal_stuff_link);
    SpannableStringBuilder sb = new SpannableStringBuilder(additionalSoftware.getText());
    sb.clearSpans();//removing any previously (from instance-state) set click spans.
    sb.setSpan(new ClickableSpan() {
        @Override/*from ww  w .j a  v  a  2 s  . c  om*/
        public void onClick(View widget) {
            FragmentChauffeurActivity activity = (FragmentChauffeurActivity) getActivity();
            activity.addFragmentToUi(new AdditionalSoftwareLicensesFragment(),
                    FragmentChauffeurActivity.FragmentUiContext.DeeperExperience);
        }
    }, 0, additionalSoftware.getText().length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
    additionalSoftware.setMovementMethod(LinkMovementMethod.getInstance());
    additionalSoftware.setText(sb);
}

From source file:com.securecomcode.voice.ui.UpgradeCallDialogFragment.java

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    final AlertDialog.Builder builder;
    if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.HONEYCOMB) {
        builder = new AlertDialog.Builder(
                new ContextThemeWrapper(getActivity(), R.style.RedPhone_Light_Dialog));
    } else {//  w w  w.j a  v a2s  .com
        builder = new AlertDialog.Builder(getActivity(), R.style.RedPhone_Light_Dialog);
    }

    builder.setIcon(R.drawable.red_call);

    final String upgradeString = getActivity().getResources()
            .getString(R.string.RedPhoneChooser_upgrade_to_redphone);
    SpannableStringBuilder titleBuilder = new SpannableStringBuilder(upgradeString);
    titleBuilder.setSpan(new AbsoluteSizeSpan(20, true), 0, upgradeString.length(),
            Spanned.SPAN_INCLUSIVE_INCLUSIVE);
    builder.setTitle(titleBuilder);

    builder.setMessage(
            R.string.RedPhoneChooser_this_contact_also_uses_redphone_would_you_like_to_upgrade_to_a_secure_call);

    builder.setPositiveButton(R.string.RedPhoneChooser_secure_call, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            Intent intent = new Intent(getActivity(), RedPhoneService.class);
            intent.setAction(RedPhoneService.ACTION_OUTGOING_CALL);
            intent.putExtra(Constants.REMOTE_NUMBER, number);
            getActivity().startService(intent);

            Intent activityIntent = new Intent();
            activityIntent.setClass(getActivity(), RedPhone.class);
            activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(activityIntent);

            getActivity().finish();
        }
    });

    builder.setNegativeButton(R.string.RedPhoneChooser_insecure_call, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            CallChooserCache.getInstance().addInsecureChoice(number);

            Intent intent = new Intent("android.intent.action.CALL",
                    Uri.fromParts("tel", number + CallListener.IGNORE_SUFFIX, null));
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            getActivity().finish();
        }
    });

    AlertDialog alert = builder.create();

    alert.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            ((AlertDialog) dialog).setOnCancelListener(new DialogInterface.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialogInterface) {
                    getActivity().finish();
                }
            });

            ((AlertDialog) dialog).setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialogInterface) {
                    getActivity().finish();
                }
            });
            Button positiveButton = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE);

            Button negativeButton = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_NEGATIVE);
        }
    });

    return alert;
}

From source file:com.freshdigitable.udonroad.UserInfoView.java

private void bindURL(String displayUrl, String actualUrl) {
    if (TextUtils.isEmpty(displayUrl) || TextUtils.isEmpty(actualUrl)) {
        return;/*from ww w  .  ja  v a 2  s. c  o m*/
    }
    final SpannableStringBuilder ssb = new SpannableStringBuilder(displayUrl);
    ssb.setSpan(new UnderlineSpan(), 0, displayUrl.length(), 0);
    url.setText(ssb);
    url.setVisibility(VISIBLE);
    urlIcon.setVisibility(VISIBLE);
    final OnClickListener clickListener = create(actualUrl);
    url.setOnClickListener(clickListener);
    urlIcon.setOnClickListener(clickListener);
}

From source file:com.woodblockwithoutco.beretainedexample.MainActivity.java

protected CharSequence[] getItems() {
    //filling list with entries like "fieldName 0xfieldHash"
    String[] fieldNames = new String[] { "mIntArray", "mObject", "mMap", };

    String[] fieldHashcodes = new String[] { "0x" + Integer.toHexString(System.identityHashCode(mIntArray)),
            "0x" + Integer.toHexString(System.identityHashCode(mObject)),
            "0x" + Integer.toHexString(System.identityHashCode(mMap)) };

    if (fieldHashcodes.length != fieldNames.length) {
        throw new IllegalStateException("Did you forget to add something?");
    }/*from   ww w  .  j a  v a 2  s  . c  o  m*/

    int length = fieldHashcodes.length;
    CharSequence[] items = new CharSequence[length];
    for (int i = 0; i < length; i++) {
        SpannableStringBuilder description = new SpannableStringBuilder();
        description.append(fieldNames[i]);
        description.setSpan(new TypefaceSpan("bold"), 0, description.length(),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        description.append(" ").append(fieldHashcodes[i]);
        items[i] = description;
    }

    return items;
}

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

private static void startFont(SpannableStringBuilder text, Attributes attributes) {
    String color = attributes.getValue("", "color");
    String face = attributes.getValue("", "face");
    String style = attributes.getValue("", "style");

    int len = text.length();
    text.setSpan(new Font(color, face, style), len, len, Spannable.SPAN_MARK_MARK);
}

From source file:com.keylesspalace.tusky.util.LinkHelper.java

/**
 * Finds links, mentions, and hashtags in a piece of text and makes them clickable, associating
 * them with callbacks to notify when they're clicked.
 *
 * @param view the returned text will be put in
 * @param content containing text with mentions, links, or hashtags
 * @param mentions any '@' mentions which are known to be in the content
 * @param listener to notify about particular spans that are clicked
 *//*from   w  w w  .  jav  a  2  s. c o  m*/
public static void setClickableText(TextView view, Spanned content, @Nullable Status.Mention[] mentions,
        final LinkListener listener) {

    SpannableStringBuilder builder = new SpannableStringBuilder(content);
    URLSpan[] urlSpans = content.getSpans(0, content.length(), URLSpan.class);
    for (URLSpan span : urlSpans) {
        int start = builder.getSpanStart(span);
        int end = builder.getSpanEnd(span);
        int flags = builder.getSpanFlags(span);
        CharSequence text = builder.subSequence(start, end);
        if (text.charAt(0) == '#') {
            final String tag = text.subSequence(1, text.length()).toString();
            ClickableSpan newSpan = new ClickableSpan() {
                @Override
                public void onClick(View widget) {
                    listener.onViewTag(tag);
                }

                @Override
                public void updateDrawState(TextPaint ds) {
                    super.updateDrawState(ds);
                    ds.setUnderlineText(false);
                }
            };
            builder.removeSpan(span);
            builder.setSpan(newSpan, start, end, flags);
        } else if (text.charAt(0) == '@' && mentions != null && mentions.length > 0) {
            String accountUsername = text.subSequence(1, text.length()).toString();
            /* There may be multiple matches for users on different instances with the same
             * username. If a match has the same domain we know it's for sure the same, but if
             * that can't be found then just go with whichever one matched last. */
            String id = null;
            for (Status.Mention mention : mentions) {
                if (mention.localUsername.equalsIgnoreCase(accountUsername)) {
                    id = mention.id;
                    if (mention.url.contains(getDomain(span.getURL()))) {
                        break;
                    }
                }
            }
            if (id != null) {
                final String accountId = id;
                ClickableSpan newSpan = new ClickableSpan() {
                    @Override
                    public void onClick(View widget) {
                        listener.onViewAccount(accountId);
                    }

                    @Override
                    public void updateDrawState(TextPaint ds) {
                        super.updateDrawState(ds);
                        ds.setUnderlineText(false);
                    }
                };
                builder.removeSpan(span);
                builder.setSpan(newSpan, start, end, flags);
            }
        } else {
            ClickableSpan newSpan = new CustomURLSpan(span.getURL());
            builder.removeSpan(span);
            builder.setSpan(newSpan, start, end, flags);
        }
    }
    view.setText(builder);
    view.setLinksClickable(true);
    view.setMovementMethod(LinkMovementMethod.getInstance());
}

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

private static void endA(SpannableStringBuilder text) {
    int len = text.length();
    Object obj = getLast(text, Href.class);
    int where = text.getSpanStart(obj);

    text.removeSpan(obj);/*from  w w  w  .jav a 2  s.  co m*/

    if (where != len) {
        Href h = (Href) obj;

        if (h.mHref != null) {
            text.setSpan(new URLSpan(h.mHref), where, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
}

From source file:io.github.hidroh.materialistic.widget.StoryView.java

private Spannable decorateUpdated() {
    SpannableStringBuilder sb = new SpannableStringBuilder("*");
    sb.setSpan(new AsteriskSpan(getContext()), sb.length() - 1, sb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    return sb;//from  w w w.  j av a2  s .c  om
}

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

static CharSequence formatInboxMessage(final String sender, final CharSequence message, final Uri attachmentUri,
        final String attachmentType) {
    final Context context = Factory.get().getApplicationContext();
    final TextAppearanceSpan notificationSenderSpan = new TextAppearanceSpan(context,
            R.style.NotificationSenderText);

    final TextAppearanceSpan notificationTertiaryText = new TextAppearanceSpan(context,
            R.style.NotificationTertiaryText);

    final SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder();
    if (!TextUtils.isEmpty(sender)) {
        spannableStringBuilder.append(sender);
        spannableStringBuilder.setSpan(notificationSenderSpan, 0, sender.length(), 0);
    }//w w  w  .  j av  a2  s.  c  o m
    final String separator = context.getString(R.string.notification_separator);

    if (!TextUtils.isEmpty(message)) {
        if (spannableStringBuilder.length() > 0) {
            spannableStringBuilder.append(separator);
        }
        final int start = spannableStringBuilder.length();
        spannableStringBuilder.append(message);
        spannableStringBuilder.setSpan(notificationTertiaryText, start, start + message.length(), 0);
    }
    if (attachmentUri != null) {
        if (spannableStringBuilder.length() > 0) {
            spannableStringBuilder.append(separator);
        }
        spannableStringBuilder.append(formatAttachmentTag(null, attachmentType));
    }
    return spannableStringBuilder;
}