How to substring a spannable? - android

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.

Related

Android format TextView with multiple styles

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

How can I change the size of the decimal part of a number?

I am trying to print a decimal number inside a textview with a special format: the part on the left of the comma in one particular size and the part on the right in a different size.
Any idea about how can I make this work?
you can make use of SpannableString
TextView tv = findViewById(R.id.textview);
String value = "10.3";
final SpannableString text = new SpannableString(value);
text.setSpan(new RelativeSizeSpan(1.5f), value.indexOf(".")+1, value.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
text.setSpan(new ForegroundColorSpan(Color.RED), value.indexOf(".")+1, value.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(text);
for more refer these
http://alexandroid.net/spannablestring-example/
https://developer.android.com/reference/android/text/SpannableString.html
https://takeoffandroid.com/android-spannable-text-view-to-change-color-size-style-and-adding-click-event-for-particular-word-8acd8a05ec61
Use SpannableString..
Example
String yourText = "20.00";
SpannableString ss = new SpannableString(yourText);
ss.setSpan(new RelativeSizeSpan(1.2f), youString.indexOf(".") + 1, youString.length(), 0);
yourTextview.setText(ss);
Here is an example doing this
double number = 422.343;
String completeNumber = String.valueOf(number);
int decimalPointIndex = completeNumber.indexOf('.');
SpannableString ss= new SpannableString(completeNumber);
ss.setSpan(new RelativeSizeSpan(2f), 0,decimalPointIndex, 0);
textView.setText(ss);
Convert the decimal number to String
Get index value of decimal point
Then use it and display the first part before decimal as bigger

Spannable style isn't applying

So, i'm having a message which contains text and SpannableStringBuilder which is initialized like this :
final SpannableStringBuilder builder = new SpannableStringBuilder(message.getMessage());
And by default, i'm setting font to BOLD :
builder.setSpan(new StyleSpan(Typeface.BOLD), 0, messageText.length(), 0);
But my message text will be changed, example :
Your question : {question}
Your answer : {answer}
{question} and {answer} are objects that will be replaced with text, replacing is happening like this:
final TextObject question = objects.getQuestion();
spannable = new SpannableString(question.getText());
spannable.setSpan(new StyleSpan(Typeface.NORMAL), 0, spannable.length(), 0);
builder.replace(start, end, spannable);
Text is replacing just fine, but NORMAL font type isn't applying to it, so i have all text BOLD, but i need this parts to be normal
Any help is highly appreciated
You have to add that Spanned.SPAN_INCLUSIVE_INCLUSIVE as a last parameter
spannable.setSpan(new StyleSpan(Typeface.NORMAL), 0, spannable.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
and if you want some part in bold and some not you have to be specific which part. The second and the third parameter of setSpan method is responsible for that

Android,setSpan twice?

Say I have a SpannableString that a URLSpan is set for span in it like this:
SpannableString ss = new SpannableString(text);
ss.setSpan(new URLSpan("com://my.app"), 3, 6,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
Then I set another URLSpan for that span.But second URLSpan does not work and first one works.I can not use ss.removeSpan(what); because I do not want to remove all URLSpans.How I can solve this problem?
I use getSpans() in specific span(That I want to reset for it) to get it's span and then I use removeSpan to remove it:
SpannableString ss = new SpannableString(text);
ss.setSpan(new URLSpan("com://my.app"), 3, 6,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
URLSpan[] toRemoveSpans = ss.getSpans(3, 6, URLSpan.class);
ss.removeSpan(toRemoveSpans[0]);
ss.setSpan(new MyURLSpan("com://my.app"), 3, 6,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

How to make multi spannable text?

This is example.
String source = "This is example text";
Spannable out = new SpannedString(source);
StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);
out.setSpan(boldSpan, 1, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
out.setSpan(boldSpan, 9, 12, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//someTextView.setText(out);
Expected result: This is example text ("hi" & "xam" are bold)
Actual result:      This is example text (works only last setSpan method and bold is only "xam")
How to make multi spannable text? It's possible?
Maybe problem is in Spannable.SPAN_EXCLUSIVE_EXCLUSIVE flag? Thanks.
I think you need a new StyleSpan for each.
String source = "This is example text";
Spannable out = new SpannedString(source);
StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);
StyleSpan boldSpan2 = new StyleSpan(Typeface.BOLD);
out.setSpan(boldSpan, 1, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
out.setSpan(boldSpan2, 9, 12, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Haven't tried it out though.
The reason is that each bold spannable text must be represented by a StyleSpan. The reason for yours is just the same as reassignment of a value.

Categories

Resources