List of usage examples for android.text SpannableStringBuilder getSpanEnd
public int getSpanEnd(Object what)
From source file:Main.java
/** * Make UI TextView a html link.//from w w w. ja v a2 s . co 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 *///from w ww . j ava 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
/** * ? ? SpoilerSpan ForegroundColorSpan ?? ? ?? ? *///from w w w . j av a2s . c om private static void fixSpoilerSpans(SpannableStringBuilder builder, ThemeColors themeColors) { SpoilerSpan[] spoilers = builder.getSpans(0, builder.length(), SpoilerSpan.class); for (SpoilerSpan span : spoilers) { int start = builder.getSpanStart(span); int end = builder.getSpanEnd(span); builder.removeSpan(span); builder.setSpan(span, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } ClickableURLSpan[] urls = builder.getSpans(0, builder.length(), ClickableURLSpan.class); for (ClickableURLSpan span : urls) { int start = builder.getSpanStart(span); int end = builder.getSpanEnd(span); builder.setSpan(new ForegroundColorSpan(themeColors.urlLinkForeground), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } }
From source file:com.android.tv.dvr.ui.DvrUiHelper.java
@NonNull public static CharSequence getStyledTitleWithEpisodeNumber(Context context, String title, String seasonNumber, String episodeNumber, int episodeNumberStyleResId) { if (TextUtils.isEmpty(title)) { return ""; }/*w ww . j a v a 2 s. c om*/ SpannableStringBuilder builder; if (TextUtils.isEmpty(seasonNumber) || seasonNumber.equals("0")) { builder = TextUtils.isEmpty(episodeNumber) ? new SpannableStringBuilder(title) : new SpannableStringBuilder(Html.fromHtml(context.getString( R.string.program_title_with_episode_number_no_season, title, episodeNumber))); } else { builder = new SpannableStringBuilder(Html.fromHtml(context .getString(R.string.program_title_with_episode_number, title, seasonNumber, episodeNumber))); } Object[] spans = builder.getSpans(0, builder.length(), Object.class); if (spans.length > 0) { if (episodeNumberStyleResId != 0) { builder.setSpan(new TextAppearanceSpan(context, episodeNumberStyleResId), builder.getSpanStart(spans[0]), builder.getSpanEnd(spans[0]), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } builder.removeSpan(spans[0]); } return new SpannableString(builder); }
From source file:org.pencilsofpromise.rss.RSSFragment.java
private Spanned removeContentSpanObjects(String sb) { SpannableStringBuilder spannedStr = (SpannableStringBuilder) Html.fromHtml(sb.toString().trim()); Object[] spannedObjects = spannedStr.getSpans(0, spannedStr.length(), Object.class); for (int i = 0; i < spannedObjects.length; i++) { if (spannedObjects[i] instanceof ImageSpan) spannedStr.replace(spannedStr.getSpanStart(spannedObjects[i]), spannedStr.getSpanEnd(spannedObjects[i]), ""); }/*from w w w . j av a 2 s .c om*/ return spannedStr; }
From source file:com.jecelyin.editor.v2.core.text.TextUtils.java
/** * Return a new CharSequence in which each of the source strings is * replaced by the corresponding element of the destinations. *//* ww w . ja v a 2 s . co m*/ public static CharSequence replace(CharSequence template, String[] sources, CharSequence[] destinations) { SpannableStringBuilder tb = new SpannableStringBuilder(template); for (int i = 0; i < sources.length; i++) { int where = indexOf(tb, sources[i]); if (where >= 0) tb.setSpan(sources[i], where, where + sources[i].length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } for (int i = 0; i < sources.length; i++) { int start = tb.getSpanStart(sources[i]); int end = tb.getSpanEnd(sources[i]); if (start >= 0) { tb.replace(start, end, destinations[i]); } } return tb; }
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 ww . j av a 2 s .co m 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); }