Make some text bold inside StaticLayout in android

661 views Asked by At

Is there any way to make some text bold of a static layout? I am trying to print one government document so that I have used StaticLayout using following code to set text on Canvas.

TextPaint mTextPaint=new TextPaint();
mTextPaint.setTypeface(Typeface.createFromAsset(context.getAssets(),"fonts/times.ttf"));

    StaticLayout mTextLayout;
    canvas.save();

mTextLayout = new StaticLayout(getText(), mTextPaint, pageInfo.getPageWidth()/2-otherPadding*3, Layout.Alignment.ALIGN_NORMAL, 1.0f, 1.0f, true);
translate(textX, textY);
draw(canvas);
....

Where getText() method returns the String which I want to print as given below.

String getText()
{
     return "Name and complete address of Genetic Clinic/Ultrasound Clinic/Imaging centre : "+hospital.getName();
}

So Here I want to make hospital name bold only.

2

There are 2 answers

1
szholdiyarov On

You can make text bold in either ways:

When you call setTypeface() you can also pass second parameter: mTextPaint.setTypeface(Typeface.createFromAsset(context.getAssets(),"fonts/times.ttf"), Typeface.BOLD);

OR in your xml

android:textStyle="bold"
1
Shubham On

This should help:

<string name="hospital message">Name and complete address of Genetic Clinic/Ultrasound Clinic/Imaging centre : <b> %1$s </b></string>

and then:

Resources res = getResources();
String text = String.format(res.getString(R.string.hospital_message), hospital.getName());