use of android.text.style.StyleSpan in project Android-Developers-Samples by johnjohndoe.
the class MainActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sample_main);
// BEGIN_INCLUDE(text_auto_linkify)
/*
* text_auto_linkify shows the android:autoLink property, which
* automatically linkifies things like URLs and phone numbers
* found in the text. No java code is needed to make this
* work.
* This can also be enabled programmatically by calling
* .setAutoLinkMask(Linkify.ALL) before the text is set on the TextView.
*
* See android.text.util.Linkify for other options, for example only
* auto-linking email addresses or phone numbers
*/
// END_INCLUDE(text_auto_linkify)
// BEGIN_INCLUDE(text_html_resource)
/*
* text_html_resource has links specified by putting anchor tags (<a>) in the string
* resource. By default these links will appear but not
* respond to user input. To make them active, you need to
* call setMovementMethod() on the TextView object.
*/
TextView textViewResource = (TextView) findViewById(R.id.text_html_resource);
textViewResource.setText(Html.fromHtml(getResources().getString(R.string.link_text_manual)));
textViewResource.setMovementMethod(LinkMovementMethod.getInstance());
// END_INCLUDE(text_html_resource)
// BEGIN_INCLUDE(text_html_program)
/*
* text_html_program shows creating text with links from HTML in the Java
* code, rather than from a string resource. Note that for a
* fixed string, using a (localizable) resource as shown above
* is usually a better way to go; this example is intended to
* illustrate how you might display text that came from a
* dynamic source (eg, the network).
*/
TextView textViewHtml = (TextView) findViewById(R.id.text_html_program);
textViewHtml.setText(Html.fromHtml("<b>text_html_program: Constructed from HTML programmatically.</b>" + " Text with a <a href=\"http://www.google.com\">link</a> " + "created in the Java source code using HTML."));
textViewHtml.setMovementMethod(LinkMovementMethod.getInstance());
// END_INCLUDE(text_html_program)
// BEGIN_INCLUDE(text_spannable)
/*
* text_spannable illustrates constructing a styled string containing a
* link without using HTML at all. Again, for a fixed string
* you should probably be using a string resource, not a
* hardcoded value.
*/
SpannableString ss = new SpannableString("text_spannable: Manually created spans. Click here to dial the phone.");
/*
* Make the first 38 characters bold by applying a StyleSpan with bold typeface.
*
* Characters 45 to 49 (the word "here") is made clickable by applying a URLSpan
* pointing to a telephone number. Clicking it opens the "tel:" URL that starts the dialer.
*
* The SPAN_EXCLUSIVE_EXCLUSIVE flag defines this span as exclusive, which means
* that it will not expand to include text inserted on either side of this span.
*/
ss.setSpan(new StyleSpan(Typeface.BOLD), 0, 39, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
ss.setSpan(new URLSpan("tel:4155551212"), 40 + 6, 40 + 10, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
TextView textViewSpan = (TextView) findViewById(R.id.text_spannable);
textViewSpan.setText(ss);
/*
* Set the movement method to move between links in this TextView.
* This means that the user traverses through links in this TextView, automatically
* handling appropriate scrolling and key commands.
*/
textViewSpan.setMovementMethod(LinkMovementMethod.getInstance());
// END_INCLUDE(text_spannable)
}
use of android.text.style.StyleSpan in project FlexibleAdapter by davideas.
the class Utils method highlightText.
/**
* Sets a spannable text with the accent color (if available) into the provided TextView.
* <p>Internally calls {@link #fetchAccentColor(Context, int)}.</p>
*
* @param context context
* @param textView the TextView to transform
* @param originalText the original text which the transformation is applied to
* @param constraint the text to highlight
* @param defColor the default color in case accentColor is not found
* @see #fetchAccentColor(Context, int)
* @deprecated Use
* {@link #highlightText(TextView, String, String, int)} OR
* {@link #highlightText(TextView, String, String)}
*/
@Deprecated
public static void highlightText(@NonNull Context context, @NonNull TextView textView, String originalText, String constraint, @ColorInt int defColor) {
if (originalText == null)
originalText = "";
if (constraint == null)
constraint = "";
int i = originalText.toLowerCase(Locale.getDefault()).indexOf(constraint.toLowerCase(Locale.getDefault()));
if (i != -1) {
Spannable spanText = Spannable.Factory.getInstance().newSpannable(originalText);
spanText.setSpan(new ForegroundColorSpan(fetchAccentColor(context, defColor)), i, i + constraint.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spanText.setSpan(new StyleSpan(Typeface.BOLD), i, i + constraint.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spanText, TextView.BufferType.SPANNABLE);
} else {
textView.setText(originalText, TextView.BufferType.NORMAL);
}
}
use of android.text.style.StyleSpan in project plaid by nickbutcher.
the class HomeActivity method setNoFiltersEmptyTextVisibility.
private void setNoFiltersEmptyTextVisibility(int visibility) {
if (visibility == View.VISIBLE) {
if (noFiltersEmptyText == null) {
// create the no filters empty text
ViewStub stub = (ViewStub) findViewById(R.id.stub_no_filters);
noFiltersEmptyText = (TextView) stub.inflate();
String emptyText = getString(R.string.no_filters_selected);
int filterPlaceholderStart = emptyText.indexOf('ࢴ');
int altMethodStart = filterPlaceholderStart + 3;
SpannableStringBuilder ssb = new SpannableStringBuilder(emptyText);
// show an image of the filter icon
ssb.setSpan(new ImageSpan(this, R.drawable.ic_filter_small, ImageSpan.ALIGN_BASELINE), filterPlaceholderStart, filterPlaceholderStart + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// make the alt method (swipe from right) less prominent and italic
ssb.setSpan(new ForegroundColorSpan(ContextCompat.getColor(this, R.color.text_secondary_light)), altMethodStart, emptyText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ssb.setSpan(new StyleSpan(Typeface.ITALIC), altMethodStart, emptyText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
noFiltersEmptyText.setText(ssb);
noFiltersEmptyText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
drawer.openDrawer(GravityCompat.END);
}
});
}
noFiltersEmptyText.setVisibility(visibility);
} else if (noFiltersEmptyText != null) {
noFiltersEmptyText.setVisibility(visibility);
}
}
use of android.text.style.StyleSpan in project plaid by nickbutcher.
the class SearchActivity method setNoResultsVisibility.
void setNoResultsVisibility(int visibility) {
if (visibility == View.VISIBLE) {
if (noResults == null) {
noResults = (TextView) ((ViewStub) findViewById(R.id.stub_no_search_results)).inflate();
noResults.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
searchView.setQuery("", false);
searchView.requestFocus();
ImeUtils.showIme(searchView);
}
});
}
String message = String.format(getString(R.string.no_search_results), searchView.getQuery().toString());
SpannableStringBuilder ssb = new SpannableStringBuilder(message);
ssb.setSpan(new StyleSpan(Typeface.ITALIC), message.indexOf('“') + 1, message.length() - 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
noResults.setText(ssb);
}
if (noResults != null) {
noResults.setVisibility(visibility);
}
}
use of android.text.style.StyleSpan in project android_frameworks_base by ResurrectionRemix.
the class HtmlToSpannedConverter method withinParagraph.
private static void withinParagraph(StringBuilder out, Spanned text, int start, int end) {
int next;
for (int i = start; i < end; i = next) {
next = text.nextSpanTransition(i, end, CharacterStyle.class);
CharacterStyle[] style = text.getSpans(i, next, CharacterStyle.class);
for (int j = 0; j < style.length; j++) {
if (style[j] instanceof StyleSpan) {
int s = ((StyleSpan) style[j]).getStyle();
if ((s & Typeface.BOLD) != 0) {
out.append("<b>");
}
if ((s & Typeface.ITALIC) != 0) {
out.append("<i>");
}
}
if (style[j] instanceof TypefaceSpan) {
String s = ((TypefaceSpan) style[j]).getFamily();
if ("monospace".equals(s)) {
out.append("<tt>");
}
}
if (style[j] instanceof SuperscriptSpan) {
out.append("<sup>");
}
if (style[j] instanceof SubscriptSpan) {
out.append("<sub>");
}
if (style[j] instanceof UnderlineSpan) {
out.append("<u>");
}
if (style[j] instanceof StrikethroughSpan) {
out.append("<span style=\"text-decoration:line-through;\">");
}
if (style[j] instanceof URLSpan) {
out.append("<a href=\"");
out.append(((URLSpan) style[j]).getURL());
out.append("\">");
}
if (style[j] instanceof ImageSpan) {
out.append("<img src=\"");
out.append(((ImageSpan) style[j]).getSource());
out.append("\">");
// Don't output the dummy character underlying the image.
i = next;
}
if (style[j] instanceof AbsoluteSizeSpan) {
AbsoluteSizeSpan s = ((AbsoluteSizeSpan) style[j]);
float sizeDip = s.getSize();
if (!s.getDip()) {
Application application = ActivityThread.currentApplication();
sizeDip /= application.getResources().getDisplayMetrics().density;
}
// px in CSS is the equivalance of dip in Android
out.append(String.format("<span style=\"font-size:%.0fpx\";>", sizeDip));
}
if (style[j] instanceof RelativeSizeSpan) {
float sizeEm = ((RelativeSizeSpan) style[j]).getSizeChange();
out.append(String.format("<span style=\"font-size:%.2fem;\">", sizeEm));
}
if (style[j] instanceof ForegroundColorSpan) {
int color = ((ForegroundColorSpan) style[j]).getForegroundColor();
out.append(String.format("<span style=\"color:#%06X;\">", 0xFFFFFF & color));
}
if (style[j] instanceof BackgroundColorSpan) {
int color = ((BackgroundColorSpan) style[j]).getBackgroundColor();
out.append(String.format("<span style=\"background-color:#%06X;\">", 0xFFFFFF & color));
}
}
withinStyle(out, text, i, next);
for (int j = style.length - 1; j >= 0; j--) {
if (style[j] instanceof BackgroundColorSpan) {
out.append("</span>");
}
if (style[j] instanceof ForegroundColorSpan) {
out.append("</span>");
}
if (style[j] instanceof RelativeSizeSpan) {
out.append("</span>");
}
if (style[j] instanceof AbsoluteSizeSpan) {
out.append("</span>");
}
if (style[j] instanceof URLSpan) {
out.append("</a>");
}
if (style[j] instanceof StrikethroughSpan) {
out.append("</span>");
}
if (style[j] instanceof UnderlineSpan) {
out.append("</u>");
}
if (style[j] instanceof SubscriptSpan) {
out.append("</sub>");
}
if (style[j] instanceof SuperscriptSpan) {
out.append("</sup>");
}
if (style[j] instanceof TypefaceSpan) {
String s = ((TypefaceSpan) style[j]).getFamily();
if (s.equals("monospace")) {
out.append("</tt>");
}
}
if (style[j] instanceof StyleSpan) {
int s = ((StyleSpan) style[j]).getStyle();
if ((s & Typeface.BOLD) != 0) {
out.append("</b>");
}
if ((s & Typeface.ITALIC) != 0) {
out.append("</i>");
}
}
}
}
}
Aggregations