Why doesn't my text show up with style when using SpannableStringBuilder? - android

I have a problem with a SpannableString object.
Below's a short example:
SpannableString spanstr = new SpannableString("Bold please!");
spanstr.setSpan(new StyleSpan(Typeface.BOLD), 0, spanstr.length(), 0);
SpannableStringBuilder sb = new SpannableStringBuilder();
sb.append(spanstr);
sb.append("\n"); // A newline
sb.append("The first line is bold. This one isn't.");
CharSequence cq = sb.subSequence(0, sb.length());
// ^There's no such method to return a SpannableString,
// so I try to return a CharSequence instead.
// And then, at last:
TextView contentView = (TextView) findViewById(R.id.some_id);
contentView.setText(cq);
What the example's trying to do is showing this:
Bold please!
The first line is bold. This one isn't.
But the problem is: the first line of the text won't show up in bold.
Why doesn't it do it expected?

Use the spannable string builder for setting as text in textview :
contentView.setText(sb);
or else you can do like this :
SpannableStringBuilder spanstr = new SpannableStringBuilder("Bold please!");
spanstr.setSpan(new StyleSpan(Typeface.BOLD), 0, spanstr.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spanstr.append("\n");
spanstr.append("The first line is bold. This one isn't.");
contentView.setText(spanstr);

use SpannableStringBuilder instance itself.
contentView.setText(sb);
output with your code:

Try the below. You need to set the spannable string to the textview. So set the spannable string to your text as below
String s= "The first line is bold. This one isn't";
String title="Bold Please!";
TextView tv = (TextView) findViewById(R.id.some_id);
tv.setText("");
SpannableString ss1= new SpannableString(title);
ss1.setSpan(new StyleSpan(Typeface.BOLD), 0, ss1.length(), 0);
tv.append(ss1);
tv.append("\n");
tv.append(s);
I tried the above and you can see the resulting snapshot below.

If you use custom font in your device. There is such a silly bug i think. Please change your custom font to default in your device and try again.

late for the answer but for future reader who facing this problem,
make sure you don't call toString() when setting builder into textview
val builder = SpannableStringBuilder()
builder.setspan(xxxxxxx)
do this :
textview.text = builder
and dont do this :
textview.text = builder.toString() //will remove the styling
alas, the styling from SpanableStringBuilder wont work

Related

Bold Text in TextView does not work with combination of Spanned and String

I have this code
TextView text1 = (TextView) view.findViewById(R.layout.myLayout);
Spanned myBold = (Html.fromHtml("<b>Test<b>", Html.FROM_HTML_MODE_LEGACY));
If I do
text1.setText(myBold);
Then myBold is in bold,which is ok. But when I want to add a string more, like
text1.setText(myBold+"bla");
Then the whole TextView is not bold anymore. Why does the new String "bla" affect this?
Thanks.
Why does the new String "bla" affect this?
Because what you are really doing is:
text1.setText(myBold.toString() + "bla");
A String has no style information. A Spanned object does.
Use TextUtils.concat() instead:
text1.setText(TextUtils.concat(myBold, "bla"));
A better choice would be to use a Bold StyleSpan. In the next sample only the "hello" world will be set to bold by using such technique:
Java:
final SpannableString caption = new SpannableString("hello world");
// Set to bold from index 0 to the length of 'hello'
caption.setSpan(new StyleSpan(Typeface.BOLD), 0, "hello".length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
yourTextView.setText(caption);
Kotlin:
yourTextView.text = SpannableString("hello world").apply {
// Set to bold from index 0 to the length of 'hello'
setSpan(StyleSpan(Typeface.BOLD), 0, "hello".length, Spannable.SPAN_INCLUSIVE_EXCLUSIVE))
}
This would be a more optimal solution rather than using the Html.fromHtml technicque, as it doesn't have to go through the overhead of parsing/interpreting the HTML tags.
In addition, it allows you to combine more styles, sizes, etc, in the same SpannableString.

Can i change span Text in android

Here is my code,i want to put two type of fonts in my Textview. I can successfully implement a code to do that.My problem is i want to edit the variable text without changing the arrangement.
String text = " my font1 , font 2 ";
Typeface font,font1;
font = Typeface.createFromAsset(getAssets(), "font.TTF");
font1 = Typeface.createFromAsset(getAssets(), "font1.TTF");
Spannable s = new SpannableString(text);
s.setSpan(new CustomTypefaceSpan("", font), curpos, endpos, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
s.setSpan(new CustomTypefaceSpan("", font1), curpos, endpos, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
.
.
.
.
//want to chage variable text here !
I reffered the following but i don't get the solution.
Set color of TextView span in Android
Android: Coloring part of a string using TextView.setText()?
TextView with different textSize
Try using the SpannableStringBuilder class. "This is the class for text whose content and markup can both be changed." It sounds like you need a mutable class.
It's unclear precisely how you want to change the text, but I use SpannableStringBuilder like this to insert additional text at the beginning of some text that is html. Once you have your string builder object there are sufficient methods available to modify things.
Spanned spHtml = Html.fromHtml(myHtmlString, Html.FROM_HTML_MODE_LEGACY);
SpannableStringBuilder sb = new SpannableStringBuilder(spHtml);
sb.insert(0, "text to be inserted");
Can you try something like this :
public void updateTextView(String toThis) {
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(toThis);
// Put here your span, ...
}
And call this function when you want update your textView

Multicolored edittext hint

Is there a way to set multiple colors to the edittext's hint when wrapped by android.support.design.widget.TextInputLayout without compromising the behaviour of floating edittexts?
like,
Headline*
Headline and * with different colored hint
Try this! If you want to use it for setText just add BufferType.SPANNABLE (see below)
String redPart = "Hello";
String bluePart = "World";
SpannableStringBuilder builder = new SpannableStringBuilder();
SpannableString redColoredString= new SpannableString(redPart);
redColoredString.setSpan(new ForegroundColorSpan(Color.RED), 0, redPart.length(), 0);
builder.append(redColoredString);
SpannableString blueColoredString= new SpannableString(bluePart);
blueColoredString.setSpan(new ForegroundColorSpan(Color.BLUE), 0, bluePart.length(), 0);
builder.append(blueColoredString);
myEditText.setHint(builder)
//do following for setText
myEditText.setText(builder,BufferType.SPANNABLE)
Use SpannableString class which allows you to use different styles on parts of your string ... If I remember correctly, TextAppearanceSpan class is used for coloring a text..
See below code this is work for me.
EditText editText = (EditText) findViewById(textView);
Spannable wordtoSpan = new SpannableString("Hello world");
wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 2, 6, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
editText.setHint(wordtoSpan);
You can acheive this by programtically
SpannableString spString = new SpannableString("HEADERLINE*");
spString.setSpan(new ForegroundColorSpan(Color.MAGENTA), 11, spString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
metUserName.setHint(spString);

how to have bold and normal text in same textview in android?

I have searched internet and tried the following code but its not working
SpannableString ss1 = new SpannableString("Health: ");
ss1.setSpan(new android.text.style.StyleSpan(android.graphics.Typeface.BOLD), 0, ss1.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textview1.setText("\n"+ss1+strhealth+"\n\n");
i want output to be like this
Health: good
where strhealth = good
But it is coming out Health: good
What is the mistake ?
I am using android studio 2.1.1
String txt1="Health: ";
SpannableString txtSpannable= new SpannableString(txt1);
StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);
txtSpannable.setSpan(boldSpan, 0, 8, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
builder.append(txtSpannable);
String txt2="good";
builder.append(txt2);
textview1.lblStatus.setText(builder, TextView.BufferType.SPANNABLE);
The easiest way is
textview1.setText(Html.fromHtml("<b>Health:</b> good"));
The mistake in your code is to use string concatenation here: "\n"+ss1+strhealth+"\n\n" which strips out all formatting because the components are taken as normal strings.
In kotlin, you can do this. I used this to bold characters/word within a string. For example:
item = "Philippines"
query = "Phil"
result = Philippines
val spannable = SpannableString(item)
val indexStart = item.indexOf(query)
val indexEnd = indexStart + query.length
spannable.setSpan(StyleSpan(Typeface.BOLD), indexStart, indexEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
and use it like this
textView.text = spannable
I am bit late to answer, but I created a method for easy use by using answer already provided here.
private void setSpanString(String string1, String string2, TextView textView) {
SpannableStringBuilder builder=new SpannableStringBuilder();
SpannableString txtSpannable= new SpannableString(string1);
StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);
txtSpannable.setSpan(boldSpan, 0, string1.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
builder.append(txtSpannable);
builder.append(string2);
textView.setText(builder, TextView.BufferType.SPANNABLE);
}
I would use a string resource such as this:
<string name="health_status"><b>Health:</b> %1$s</string>
When you want to set the health status just use this code:
String ss1 = getString(R.string.health_status, strhealth);
Below I have mentioned the code which one through you can create spannableString in Kotlin
val spannableStringBuilder = SpannableStringBuilder()
val boldSpan: StyleSpan = StyleSpan(Typeface.BOLD)
sp_text.setSpan(boldSpan, firstIndex, lastIndex,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
sp_text.setSpan(clickableSpan, firstIndex, lastIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
sp_text.setSpan(ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.colorPrimary)), firstIndex, lastIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
I think you should use 2 different textView, a label and one for the data. it's common and good practice

SpannableStringBuilder to create String with multiple fonts/text sizes etc Example?

I need to create a String placed in a TextView that will display a string like this:
First Part Not Bold BOLD rest not bold
So I want to know how I could use SpannableStringBuilder to do this?
I could use three TextEdit to accomplish this but I would like to use best solution.
First Part Not Bold BOLD rest not bold
You can do this either as #Rajesh suggested or by this.
String normalBefore= "First Part Not Bold ";
String normalBOLD= "BOLD ";
String normalAfter= "rest not bold";
String finalString= normalBefore+normalBOLD+normalAfter;
Spannable sb = new SpannableString( finalString );
sb.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), finalString.indexOf(normalBOLD)+ normalBOLD.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //bold
sb.setSpan(new AbsoluteSizeSpan(intSize), finalString.indexOf(normalBOLD)+ normalBOLD.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);//resize size
to show this in TextView
textview.setText(sb, TextView.BufferType.SPANNABLE);
The accepted answer is fine (and I upvoted it), but it fails to use the SpannableStringBuilder as the submitter requested. As I had a case where the Builder made the most sense, here is the code for that (with a bonus use of also changing the color of the text if that is helpful to others). Note that you could also provide the initial string to the SpannableStringBuilder constructor, but I set it here to use "append" to be clear that you can append a lot before your desired "bold" text and then just record the start as shown. I would suspect that this is also faster code than the accepted answer.
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");
If you are using Kotlin you can do the following using the android-ktx library
val s = SpannableStringBuilder()
.append("First Part Not Bold ")
.bold { append("BOLD") }
.append("Rest not bold")
The bold is an extension function on SpannableStringBuilder. You can see the documentation here for a list of operations you can use.
Another example:
val s = SpannableStringBuilder()
.color(green, { append("Green text ") })
.append("Normal text ")
.scale(0.5, { append("Text at half size " })
.backgroundColor(green, { append("Background green") })
Where green is a resolved RGB color.
It is even possible to nest spans so you end up with an embedded DSL:
bold { underline { italic { append("Bold and underlined") } } }
You will need the following in your app module level build.gradle for it to work:
repositories {
google()
}
dependencies {
implementation "androidx.core:core-ktx:1.2.0"
}
From API 21 SpannableStringBuilder includes a simple method to do this. Here is a solution example:
SpannableStringBuilder builder= new SpannableStringBuilder();
StyleSpan boldSpan = new StyleSpan(android.graphics.Typeface.BOLD);
builder.append("First Part Not Bold ")
.append("BOLD ", boldSpan, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
.append("rest not bold");
Kotlin version:
val builder = SpannableStringBuilder()
val boldSpan = StyleSpan(Typeface.BOLD)
builder.append("First Part Not Bold ")
.append("BOLD ", boldSpan, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
.append("rest not bold")
Use HTML code in TextView using the Html class:
Spanned styledText = Html.fromHtml("First Part Not Bold <b>BOLD</b> rest not bold");
textView.setText(styledText);
This code should set to bold everything that comes inside the html bold tag. And it also deletes the tag so only the content inside is displayed.
SpannableStringBuilder sb = new SpannableStringBuilder("this is <b>bold</b> and this is <b>bold too</b> and this is <b>bold too, again</b>.");
Pattern p = Pattern.compile("<b>.*?</b>", Pattern.CASE_INSENSITIVE);
boolean stop = false;
while (!stop)
{
Matcher m = p.matcher(sb.toString());
if (m.find()) {
sb.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), m.start(), m.end(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
sb.delete(m.end()-4, m.end());
sb.delete(m.start(), m.start() + 3);
}
else
stop = true;
}
This code can also be adapted for other html style tags, such as Superscript (sup tag), etc.
SpannableStringBuilder sb = new SpannableStringBuilder("text has <sup>superscript</sup> tag");
Pattern p = Pattern.compile("<sup>.*?</sup>", Pattern.CASE_INSENSITIVE);
boolean stop = false;
while (!stop)
{
Matcher m = p.matcher(sb.toString());
if (m.find()) {
sb.setSpan(new SuperscriptSpan(), m.start(), m.end(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
sb.delete(m.end()-6, m.end());
sb.delete(m.start(), m.start() + 5);
}
else
stop = true;
}
To set the color, just use the ForegroundColorSpan with setSpan.
sb.setSpan(new ForegroundColorSpan(Color.rgb(255, 0, 0)), m.start(), m.end(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
Hope it helps.
We can also use SpannableStringBuilder with TextAppearanceSpan to accomplish that. Follow the below steps to implement like that.
Create a style in styles.xml.
<style name="BoldStyle">
<!-- Can add other styling attributes -->
<item name="android:textStyle">bold</item>
......
</style>
Use the below code.
SpannableStringBuilder builder = new SpannableStringBuilder("First Part Not Bold BOLD rest not bold");
builder.setSpan(new TextAppearanceSpan(this, R.style.BoldStyle), 20, 24, 0);
((TextView)findViewById(R.id.tv7)).setText(builder);
That's it. Hope it'll help someone.
you can bold and resize a part of your string in kotlin
val s = SpannableStringBuilder()
.append("First Part Not Bold And No Resize ")
.bold { scale(1.5f, { append("Second Part By Bold And Resize " )}) }
.append("Third Part Not Bold And No Resize")
yourTextview.text = s
So I know this has been solved, and even as requested with SpannableStringBuilder but in the event you wanted to build a string more dynamically I figured I would put this up.
// Stuff needed
TextView DataTextView = (TextView)rootView.findViewById(R.id.DataView);
String Fields[] = {...database column names as strings... "x","y"};
String DataString = new String();
int start,stop; // Start and Stop of formatting
// Final Result
SpannableStringBuilder coloredString = new SpannableStringBuilder();
SpannableString temp; // Small segment of colored string
for (int i =0; i < Fields.length; i++)
{
if (database_result.containsKey(Fields[i])) // Be sure a field exists in the ContentValues
{
DataString = Fields[i]+": ";
start = DataString.length();
DataString = DataString+ +database_result.getAsInteger(Fields[i])+" ";
stop= DataString.length();
temp = new SpannableString(DataString);
temp.setSpan(new ForegroundColorSpan(Color.WHITE),start, stop, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
coloredString.append(temp);
}
}
DataTextView.setText(coloredString);
database_result is a ContentValues type that I constructed from the returned Cursor type of the SQL query. The only problem I had with this was at first it was only ColorSpaning the first segment. It seams that you need to declare a new ForegroundColorSpan every time you want to use one (or any other kind of span) in a loop.
Why would you use SpannableStringBuilder when you can use SpannableBuilder?? (https://gist.github.com/qtyq/90f9b4894069a8b3676c)
SpannableString ss = SpannableBuilder.init("First Part Not Bold BOLD rest not bold")
.makeBold("BOLD")
.create()
For Xamarin.Android:
SpannableStringBuilder TextoFormateado = new SpannableStringBuilder();
TextoFormateado.Append("Not Bold");
int start = TextoFormateado.Length();
TextoFormateado.Append("Bold and Red");
TextoFormateado.SetSpan(new ForegroundColorSpan(new Color(255, 0, 0, 149)),
start, TextoFormateado.Length(), SpanTypes.ExclusiveExclusive);
TextoFormateado.SetSpan(new StyleSpan(TypefaceStyle.Bold),
start, TextoFormateado.Length(), SpanTypes.ExclusiveExclusive);
TextoFormateado.Append("Not bold");
TxtFinalText.TextFormatted = TextoFormateado;

Categories

Resources