Example usage for android.text SpannableStringBuilder getSpanFlags

List of usage examples for android.text SpannableStringBuilder getSpanFlags

Introduction

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

Prototype

public int getSpanFlags(Object what) 

Source Link

Document

Return the flags of the end of the specified markup object, or 0 if it is not attached to this buffer.

Usage

From source file:Main.java

/**
 * Make UI TextView a html link./*from w ww  .  j av  a 2s. c  o m*/
 * 
 * @param context the context
 * @param textView the text view
 * @param html the html containing link info
 */
public static void makeTextViewAHTMLLink(final Context context, TextView textView, String html) {
    textView.setLinksClickable(true);
    textView.setMovementMethod(LinkMovementMethod.getInstance());
    CharSequence sequence = Html.fromHtml(html);
    SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(sequence);
    URLSpan[] urls = spannableStringBuilder.getSpans(0, sequence.length(), URLSpan.class);
    for (final URLSpan urlSpan : urls) {
        int start = spannableStringBuilder.getSpanStart(urlSpan);
        int end = spannableStringBuilder.getSpanEnd(urlSpan);
        int flags = spannableStringBuilder.getSpanFlags(urlSpan);
        ClickableSpan clickable = new ClickableSpan() {
            public void onClick(View view) {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(urlSpan.getURL()));
                context.startActivity(intent);
            }

            @Override
            public void updateDrawState(TextPaint textPaint) {
                super.updateDrawState(textPaint);
                textPaint.setUnderlineText(false);
            }
        };
        spannableStringBuilder.removeSpan(urlSpan);
        spannableStringBuilder.setSpan(clickable, start, end, flags);
    }
    textView.setText(spannableStringBuilder);
}

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
 *//*  w w w .  j  a v  a 2s.com*/
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:org.tvbrowser.tvbrowser.TvBrowser.java

private void makeLinkClickable(SpannableStringBuilder strBuilder, final URLSpan span) {
    int start = strBuilder.getSpanStart(span);
    int end = strBuilder.getSpanEnd(span);
    int flags = strBuilder.getSpanFlags(span);
    ClickableSpan clickable = new ClickableSpan() {
        public void onClick(View view) {
            if (!mLoadingPlugin) {
                mLoadingPlugin = true;//from  w  w  w.java2 s. c om
                String url = span.getURL();

                if (url.startsWith("http://play.google.com/store/apps/details?id=")) {
                    try {
                        startActivity(new Intent(Intent.ACTION_VIEW,
                                Uri.parse(url.replace("http://play.google.com/store/apps", "market:/"))));
                    } catch (android.content.ActivityNotFoundException anfe) {
                        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
                    }

                    mLoadingPlugin = false;
                } else if (url.startsWith("plugin://") || url.startsWith("plugins://")) {
                    final File path = IOUtils.getDownloadDirectory(getApplicationContext());

                    if (!path.isDirectory()) {
                        path.mkdirs();
                    }

                    if (url.startsWith("plugin://")) {
                        url = url.replace("plugin://", "http://");
                    } else if (url.startsWith("plugins://")) {
                        url = url.replace("plugins://", "https://");
                    }

                    String name = url.substring(url.lastIndexOf("/") + 1);

                    mCurrentDownloadPlugin = new File(path, name);

                    if (mCurrentDownloadPlugin.isFile()) {
                        mCurrentDownloadPlugin.delete();
                    }

                    final String downloadUrl = url;

                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            AsyncTask<String, Void, Boolean> async = new AsyncTask<String, Void, Boolean>() {
                                private ProgressDialog mProgress;
                                private File mPluginFile;

                                protected void onPreExecute() {
                                    mProgress = new ProgressDialog(TvBrowser.this);
                                    mProgress.setMessage(getString(R.string.plugin_info_donwload).replace("{0}",
                                            mCurrentDownloadPlugin.getName()));
                                    mProgress.show();
                                };

                                @Override
                                protected Boolean doInBackground(String... params) {
                                    mPluginFile = new File(params[0]);
                                    return IOUtils.saveUrl(params[0], params[1], 15000);
                                }

                                protected void onPostExecute(Boolean result) {
                                    mProgress.dismiss();

                                    if (result) {
                                        Intent intent = new Intent(Intent.ACTION_VIEW);
                                        intent.setDataAndType(Uri.fromFile(mPluginFile),
                                                "application/vnd.android.package-archive");
                                        TvBrowser.this.startActivityForResult(intent, INSTALL_PLUGIN);
                                    }

                                    mLoadingPlugin = false;
                                };
                            };

                            async.execute(mCurrentDownloadPlugin.toString(), downloadUrl);
                        }
                    });
                } else {
                    mLoadingPlugin = false;
                }
            }
        }
    };
    strBuilder.setSpan(clickable, start, end, flags);
    strBuilder.removeSpan(span);
}