Refine search
SpannableStringBuilder longDescription = new SpannableStringBuilder(); longDescription.append("First Part Not Bold "); int start = longDescription.length(); longDescription.append("BOLD"); longDescription.setSpan(new ForegroundColorSpan(0xFFCC5500), start, longDescription.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); longDescription.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), start, longDescription.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); longDescription.append(" rest not bold");
final Context context = ... // whereever your context is CharSequence sequence = Html.fromSource(context.getString(R.string.clickable_string)); SpannableStringBuilder strBuilder = new SpannableStringBuilder(sequence); UnderlineSpan[] underlines = strBuilder.getSpans(UnderlineSpan.class); for(UnderlineSpan span : underlines) { int start = strBuilder.getSpanStart(span); int end = strBuilder.getSpanEnd(span); int flags = strBuilder.getSpanFlags(span); ClickableSpan myActivityLauncher = new ClickableSpan() { public void onClick(View view) { context.startActivity(getIntentForActivityToStart()); } }; strBuilder.setSpan(myActivityLauncher, start, end, flags); } TextView textView = ... textView.setText(strBuilder); textView.setMovementMethod(LinkMovementMethod.getInstance());
public static void replaceUnderlineSpans(SpannableStringBuilder builder, Resources resources) { UnderlineSpan[] underlineSpans = builder.getSpans(0, builder.length(), UnderlineSpan.class); for (UnderlineSpan span : underlineSpans) { int start = builder.getSpanStart(span); int end = builder.getSpanEnd(span); builder.removeSpan(span); builder.setSpan(new SquigglySpan(resources), start, end, 0); } }
final Pattern p = Pattern.compile("Java"); final Matcher matcher = p.matcher(text); final SpannableStringBuilder spannable = new SpannableStringBuilder(text); final ForegroundColorSpan span = new ForegroundColorSpan(Color.GREEN); while (matcher.find()) { spannable.setSpan( span, matcher.start(), matcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE ); } myTextView.setText(spannable);
private void formatLinks(SpannableStringBuilder titleSpan) { Matcher matcher = urlPattern.matcher(titleSpan); while(matcher.find()) { titleSpan.delete(matcher.start(), matcher.end()); titleSpan.insert(matcher.start(), matcher.group(1)); titleSpan.setSpan(new ForegroundColorSpan(Style.blue), matcher.start(), matcher.start() + matcher.group(1).length(), 0); matcher = urlPattern.matcher(titleSpan); } }
if (element.getParent() != null && element.getParent().getType() == Type.LIST_ITEM) { builder.append("\n"); builder.append("\n"); break; case LIST_ITEM: builder.append(" "); if (mOrderedListNumber.containsKey(element.getParent())) { int number = mOrderedListNumber.get(element.getParent()); mOrderedListNumber.put(element.getParent(), number + 1); } else { builder.append(mOptions.mUnorderedListItem); builder.append(" "); break; case AUTOLINK: builder.append(element.getAttribute("link")); break; case HRULE: builder.append("-"); break; case IMAGE: if (loadImageCallback != null && !TextUtils.isEmpty(element.getAttribute("link"))) { builder.append("\n");
SpannableStringBuilder builder = new SpannableStringBuilder(); String red = "this is red"; SpannableString redSpannable= new SpannableString(red); redSpannable.setSpan(new ForegroundColorSpan(Color.RED), 0, red.length(), 0); builder.append(redSpannable); String white = "this is white"; SpannableString whiteSpannable= new SpannableString(white); whiteSpannable.setSpan(new ForegroundColorSpan(Color.WHITE), 0, white.length(), 0); builder.append(whiteSpannable); String blue = "this is blue"; SpannableString blueSpannable = new SpannableString(blue); blueSpannable.setSpan(new ForegroundColorSpan(Color.BLUE), 0, blue.length(), 0); builder.append(blueSpannable); mTextView.setText(builder, BufferType.SPANNABLE);
@Override public boolean ehValido(String valor) { if (valor == null) { throw new IllegalArgumentException("Campos não podem ser nulos"); } String valorSemFormatacao = Formatador.Padroes.PADRAO_SOMENTE_NUMEROS.matcher(valor).replaceAll(""); return ehValido(new SpannableStringBuilder(valorSemFormatacao), new ResultadoParcial()).isValido(); }
HeaderViewHolder headerViewHolder = (HeaderViewHolder) holder; setTypeIcon(headerViewHolder); headerViewHolder.mTypeTv.setText(title); headerViewHolder.mTypeRankBtn.setOnClickListener(v -> mContext.startActivity( new Intent(mContext, OriginalRankActivity.class))); switch (type) { case TYPE_RECOMMENDED: headerViewHolder.mTypeMore.setVisibility(View.GONE); headerViewHolder.mTypeRankBtn.setVisibility(View.VISIBLE); headerViewHolder.mAllLiveNum.setVisibility(View.GONE); headerViewHolder.mTypeMore.setVisibility(View.VISIBLE); headerViewHolder.mAllLiveNum.setVisibility(View.VISIBLE); SpannableStringBuilder stringBuilder = new SpannableStringBuilder("当前" + liveCount + "个直播"); ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan( mContext.getResources().getColor(R.color.pink_text_color)); stringBuilder.setSpan(foregroundColorSpan, 2, stringBuilder.length() - 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); headerViewHolder.mAllLiveNum.setText(stringBuilder); break;
TextView txt = (TextView) findViewById(R.id.custom_fonts); txt.setTextSize(30); Typeface font = Typeface.createFromAsset(getAssets(), "Akshar.ttf"); Typeface font2 = Typeface.createFromAsset(getAssets(), "bangla.ttf"); SpannableStringBuilder SS = new SpannableStringBuilder("আমারநல்வரவு"); SS.setSpan (new CustomTypefaceSpan("", font2), 0, 4,Spanned.SPAN_EXCLUSIVE_INCLUSIVE); SS.setSpan (new CustomTypefaceSpan("", font), 4, 11,Spanned.SPAN_EXCLUSIVE_INCLUSIVE); txt.setText(SS);
final SpannableStringBuilder str = new SpannableStringBuilder("Your awesome text"); str.setSpan(new android.text.style.StyleSpan(android.graphics.Typeface.BOLD), INT_START, INT_END, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); TextView tv=new TextView(context); tv.setText(str);
private static SpannableStringBuilder addClickablePartTextViewResizable(final Spanned strSpanned, final TextView tv, final int maxLine, final String spanableText, final boolean viewMore) { String str = strSpanned.toString(); SpannableStringBuilder ssb = new SpannableStringBuilder(strSpanned); if (str.contains(spanableText)) { ssb.setSpan(new MySpannable(false){ @Override public void onClick(View widget) { if (viewMore) { tv.setLayoutParams(tv.getLayoutParams()); tv.setText(tv.getTag().toString(), BufferType.SPANNABLE); tv.invalidate(); makeTextViewResizable(tv, -1, "View Less", false); } else { tv.setLayoutParams(tv.getLayoutParams()); tv.setText(tv.getTag().toString(), BufferType.SPANNABLE); tv.invalidate(); makeTextViewResizable(tv, 3, "View More", true); } } }, str.indexOf(spanableText), str.indexOf(spanableText) + spanableText.length(), 0); } return ssb; }
protected 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) { // Do something with span.getURL() to handle the link click... } }; strBuilder.setSpan(clickable, start, end, flags); strBuilder.removeSpan(span); } protected void setTextViewHTML(TextView text, String html) { CharSequence sequence = Html.fromHtml(html); SpannableStringBuilder strBuilder = new SpannableStringBuilder(sequence); URLSpan[] urls = strBuilder.getSpans(0, sequence.length(), URLSpan.class); for(URLSpan span : urls) { makeLinkClickable(strBuilder, span); } text.setText(strBuilder); text.setMovementMethod(LinkMovementMethod.getInstance()); }
TextView tagsTextView = (TextView) mView.findViewById(R.id.tagsTextView); SpannableStringBuilder stringBuilder = new SpannableStringBuilder(); SpannableString tag1 = new SpannableString("9.5"); stringBuilder.append(tag1); stringBuilder.setSpan(new TagSpan(getResources().getColor(R.color.blue), getResources().getColor(R.color.white)), stringBuilder.length() - tag1.length(), stringBuilder.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); SpannableString tag2 = new SpannableString("excellent!"); stringBuilder.append(tag2); stringBuilder.setSpan(new TagSpan(getResources().getColor(R.color.blueLight), getResources().getColor(R.color.blue)), stringBuilder.length() - tag2.length(), stringBuilder.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); tagsTextView.setText(stringBuilder, TextView.BufferType.SPANNABLE);
@Override public void setText(CharSequence text, BufferType type) { SpannableStringBuilder builder = new SpannableStringBuilder(text); EmojiconHandler.addEmojis(getContext(), builder, mEmojiconSize, mTextStart, mTextLength); super.setText(builder, type); }
private static void startImg(SpannableStringBuilder text, Attributes attributes, Html.ImageGetter img) { String src = attributes.getValue("", "src"); Drawable d = null; if (img != null) { d = img.getDrawable(src); } if (d == null) { d = Resources.getSystem().getDrawable(android.R.drawable.ic_menu_report_image); d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight()); } int len = text.length(); text.append("\uFFFC"); text.setSpan(new ImageSpan(d, src), len, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); }
Drawable myDrawable; //Drawable you want to display @Override public CharSequence getPageTitle(int position) { SpannableStringBuilder sb = new SpannableStringBuilder(" Page #"+ position); // space added before text for convenience myDrawable.setBounds(0, 0, myDrawable.getIntrinsicWidth(), myDrawable.getIntrinsicHeight()); ImageSpan span = new ImageSpan(myDrawable, ImageSpan.ALIGN_BASELINE); sb.setSpan(span, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); return sb; }
Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.ic_document); final Drawable d = new BitmapDrawable(getResources(), b); d.setBounds(0, 0, 200, 200); final ImageSpan is = new ImageSpan(d); SpannableStringBuilder ss = new SpannableStringBuilder(".\n"); ss.setSpan(is, 0, (".\n").length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); ss.setSpan(new MyClickAbleSpan("abc"), 0, (".\n").length(), 0); editor.append(ss, 0, (".\n").length()); editor.setMovementMethod(LinkMovementMethod.getInstance());