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?
Related
I have a dictionary database for an offline dictionary and I need to format the definitions.
The dictionary is in Romanian so you won't understand too much from the definition but I will try to explain the format briefly.
definition example: #ABAGERÍE^2# #s. f.# Meseria de abagiu. - #Abager# + #suf.# $-ie.$
From the online version, I could discover some things
# = italic
^ = upperscript
"#" = bold
$ = underline
Is there any way how can I make this possible in android?
You can use the spannable style classes:
final SpannableStringBuilder sb = new SpannableStringBuilder("Hello World 123456");
sb.setSpan(new StyleSpan(Typeface.BOLD), 0, 7, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
sb.setSpan(new StyleSpan(Typeface.ITALIC), 7, 14, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
sb.setSpan(new ForegroundColorSpan(), 0, 4, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
sb.setSpan(new UnderlineSpan(), 0, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
sb.setSpan(new SuperscriptSpan(), 1, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(sb);
Follow the link to get the full list of ParcelableSpan subclasses:
https://developer.android.com/reference/android/text/ParcelableSpan
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);
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 know how to take substring a normal string.
But how can I substring a spannable.
Consider following example.
This is my sample sentence.
now when I substring this i want to get the string "mple sente" just like this and with all this formatings.
In another words I want to substring a spannable not a string.
How can I do this?
Spannable is a CharSequence and you should be able to use CharSequence.subSequence(int start, int end) without losing any information about the spannable
Using below code you can get substring from spannable:
SpannableString ss1 = new SpannableString(s);
ss1.setSpan(new RelativeSizeSpan(1.1f), 0, 6, 0); // set size
ss1.setSpan(new ForegroundColorSpan(Color.RED), 0, 6, 0);
SpannableString ss = new SpannableString("Click here to learn more");
ss.setSpan(clickableSpan, 0, 10, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textview.setText(ss);
Hope it helps you.
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.