I need text with icon in the middle of the text for two locale like image below for example.
Spannables can be customised to use images, aka ImageSpan
https://medium.com/androiddevelopers/spantastic-text-styling-with-spans-17b0c16b4568
https://developer.android.com/reference/android/text/style/ImageSpan
SpannableString string = new SpannableString("Bottom: span.\nBaseline: span.");
string.setSpan(new ImageSpan(this, R.mipmap.ic_launcher), 7, 8, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
string.setSpan(new ImageSpan(this, R.mipmap.ic_launcher, DynamicDrawableSpan.ALIGN_BASELINE), 22, 23, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Related
I have used one textview and value set for it is like ex:11,234.45 and after decimal values like 45 should be shown with reduced front like 12sp and different colour like grey and 11,234 with black colour with front 16sp. how to achieve this using single textview in android?
You can achieve that if you follow this steps
First you need to split you string into 2 strings with this code lines
String s ="11,234.45";
String[] split = s.split(".");
String firstSubString = split[0];
String secondSubString = split[1];
Then you change the font size and color of secondSubString like this
SpannableString ss= new SpannableString(secondSubString);
ss.setSpan(new RelativeSizeSpan(2f), 0, 5, 0); // set size
ss.setSpan(new ForegroundColorSpan(Color.RED), 0, 5, 0);// set color
You can change firstSubString size and color like this
SpannableString ss1= new SpannableString(firstSubString);
ss1.setSpan(new RelativeSizeSpan(2f), 0, 5, 0); // set size
ss1.setSpan(new ForegroundColorSpan(Color.BLACK), 0, 5, 0);// set color
Finally you set your string to the TextView
TextView tv= (TextView) findViewById(R.id.textview);
tv.setText(ss1 + "." + ss);
I have what I think is a simple spanned string question. I have a string like this:
SpannableString styledString = new SpannableString("10:50 PM");//PM is 6-8
styledString.setSpan(new RelativeSizeSpan(.66f), 6, 8, 0);
styledString.setSpan(new UnderlineSpan(), 0, 8, 0);
((ProgressTextButton)findViewById(R.id.time_button_12)).setText(styledString);
It looks like this:
Is there any way to make it so that the underline is uniform?
I used the below code to add image span.
Drawable happySmiley = ctx.getResources().getDrawable(
R.drawable.img_smile);
happySmiley.setBounds(0, 0, happySmiley.getIntrinsicWidth(),
happySmiley.getIntrinsicHeight());
Drawable sadSmiley = ctx.getResources()
.getDrawable(R.drawable.ic_launcher);
sadSmiley
.setBounds(0, 0, sadSmiley.getIntrinsicWidth(),
sadSmiley.getIntrinsicHeight());
SpannableStringBuilder builder = new SpannableStringBuilder();
builder.append("Some text ");
builder.setSpan(new ImageSpan(happySmiley), builder.length()-1, builder.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
builder.append(". More text [sad_smiley_anchor]");
builder.setSpan(new ImageSpan(sadSmiley), builder.length()
- "[sad_smiley_anchor]".length(), builder.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tvSS.setText(builder);
o/p Some text (smiley). More text (smiley).
But I want to display the smiley above the "Some" in "Some text". Like
(Smiley) (Smiley)
Some Text. More text.
If smiley can be set as background to "Some" in "Some text" like setForegroundSpan() etc. then also problem can be short out. Please comment on this.
Thanks
Is it possible to change the color of StrikethroughSpan?
I've already tried TextPaint, but it seems that it doesn't change anything.
defining the foreground TextView or defining Paint Flag
Through PaintFlag
This is the simplest method you just have to set strikethrough flag on your TextView as,
yourTextView.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG);
You have to add two CharacterStyles to the region, e.g.
textView.setText("blah blah", TextView.BufferType.SPANNABLE);
Spannable spannable = (Spannable) textView;
spannable.setSpan(new StrikethroughSpan(), 5, 8, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
spannable.setSpan(new ForegroundColorSpan(Color.parseColor("#ff0000")), 5, 8, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
Is it possible to have a textview to have different color for every word? Or even every letter? I tried extending textview and creating it but however I thought of the problem is, how would I draw all the the text out at the same time with different colors?
Use android.text.Spannable
final SpannableStringBuilder str = new SpannableStringBuilder(text);
str.setSpan(
new ForegroundColorSpan(Color.BLUE),
wordStart,
wordEnd,
SpannableStringBuilder.SPAN_EXCLUSIVE_EXCLUSIVE
);
myTextView.setText(str);
EDIT: To make all "Java" green
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);
The SpannableString class allows you to easily format certain pieces (spans) of a string one way and other pieces another by applying extensions of CharacterStyle (i.e. ForegroundColorSpan) via the setSpan method.
You can try This:
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
richTextView = (TextView)findViewById(R.id.rich_text);
// this is the text we'll be operating on
SpannableString text = new SpannableString("Lorem ipsum dolor sit amet");
// make "Lorem" (characters 0 to 5) red
text.setSpan(new ForegroundColorSpan(Color.RED), 0, 5, 0);
// make "ipsum" (characters 6 to 11) one and a half time bigger than the textbox
text.setSpan(new RelativeSizeSpan(1.5f), 6, 11, 0);
// make "dolor" (characters 12 to 17) display a toast message when touched
final Context context = this;
ClickableSpan clickableSpan = new ClickableSpan() {
#Override
public void onClick(View view) {
Toast.makeText(context, "dolor", Toast.LENGTH_LONG).show();
}
};
text.setSpan(clickableSpan, 12, 17, 0);
// make "sit" (characters 18 to 21) struck through
text.setSpan(new StrikethroughSpan(), 18, 21, 0);
// make "amet" (characters 22 to 26) twice as big, green and a link to this site.
// it's important to set the color after the URLSpan or the standard
// link color will override it.
text.setSpan(new RelativeSizeSpan(2f), 22, 26, 0);
text.setSpan(new URLSpan("http://www.chrisumbel.com"), 22, 26, 0);
text.setSpan(new ForegroundColorSpan(Color.GREEN), 22, 26, 0);
// make our ClickableSpans and URLSpans work
richTextView.setMovementMethod(LinkMovementMethod.getInstance());
// shove our styled text into the TextView
richTextView.setText(text, BufferType.SPANNABLE);
}
The result will look like this:
For more detail see Chris Umbel blog.
Yes, you can do this with Spannable and SpannableStringBuilder. See Is there any example about Spanned and Spannable text for one example.
For the various ways to format text (background color, foreground color, clickable, etc.), see CharacterStyle.