23

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

G.ONE
  • 507
  • 1
  • 5
  • 14

7 Answers7

26
 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);
Ilario
  • 5,979
  • 2
  • 32
  • 46
dindinii
  • 645
  • 4
  • 7
10

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.

Henry
  • 42,982
  • 7
  • 68
  • 84
  • if i don't concatenate then how can i put a variable and mutiple text in a single text view – G.ONE Jun 07 '16 at 03:49
  • I tried textview1.setText(Html.fromHtml("Health:")+strhealth+"\n\n"); but output is still not having Health: as bold – G.ONE Jun 07 '16 at 04:12
  • Same problem as in your original try. Use this: `textview1.setText(Html.fromHtml("Health:"+strhealth+"\n\n"));` – Henry Jun 07 '16 at 04:27
  • thanks its working but now i have to use html to format everything in text view.Now the properties of text view e.g. center is not working – G.ONE Jun 07 '16 at 15:05
10

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
Somesh Kumar
  • 8,088
  • 4
  • 33
  • 49
7

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);
}
Umar Ata
  • 4,170
  • 3
  • 23
  • 35
4

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);
2

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)
Md.Sukel Ali
  • 2,987
  • 5
  • 22
  • 34
Ashok Songara
  • 162
  • 1
  • 10
-2

I think you should use 2 different textView, a label and one for the data. it's common and good practice

theyouishere
  • 177
  • 2
  • 11
  • 1
    Sometimes you have to process one string so it would require more work to split the strings to the appropriate textViews. So why not hit 2 birds with one stone when you can. – Ivan Leong Jul 05 '18 at 15:25