G.ONE
G.ONE

Reputation: 537

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

Upvotes: 25

Views: 26024

Answers (7)

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

Upvotes: 12

Ashok Songara
Ashok Songara

Reputation: 160


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)

Upvotes: 2

Umar Ata
Umar Ata

Reputation: 4258

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

Upvotes: 7

dindinii
dindinii

Reputation: 655

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

Upvotes: 26

Zachary McKenney
Zachary McKenney

Reputation: 79

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

Upvotes: 4

Henry
Henry

Reputation: 43738

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.

Upvotes: 11

theyouishere
theyouishere

Reputation: 177

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

Upvotes: -2

Related Questions