How do I draw bold text on a Bitmap?

AndroidBitmapAndroid CanvasPaint

Android Problem Overview


I want a Bitmap icon with bold text to draw it on map. I have a snippet to write text on image:

Bitmap icon = BitmapFactory.decodeResource(PropertyMapList.this.getResources(),
        R.drawable.location_mark);
TextPaint paint = new TextPaint();
paint.setColor(Color.BLACK);
paint.setTextSize(14);
paint.setFakeBoldText(true);
//paint.setTextAlign(Align.CENTER);
Bitmap copy = icon.copy(Bitmap.Config.ARGB_8888, true); 
Canvas canvas = new Canvas(copy);
//canvas.drawText(jsonObj.getString("district_name"), 5, canvas.getHeight()/2, paint);
String districtName = jsonObj.getString("district_name");
StaticLayout layout = new StaticLayout((districtName.length()>25 ? districtName.substring(0, 24)+"..":districtName)+"\n"+jsonObj.getString("total_properties"), paint, canvas.getWidth()-10,Layout.Alignment.ALIGN_CENTER, 1.3f, 0, false);
canvas.translate(5, canvas.getHeight()/2); //position the text
layout.draw(canvas);

setFakeBoldText(true) doesn't work for me. I would like the text drawn on the Bitmap to be bolded.

Android Solutions


Solution 1 - Android

Use the setTypeface method on your Paint object to set the font to something with the bold style turned on.

paint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD));

Solution 2 - Android

For Custom Fonts:

Typeface typeface = Typeface.create(Typeface.createFromAsset(mContext.getAssets(), "fonts/bangla/bensen_handwriting.ttf"), Typeface.BOLD);

For Normal Fonts:

Typeface typeface = Typeface.create(Typeface.DEFAULT, Typeface.BOLD);
// or
Typeface typeface = Typeface.DEFAULT_BOLD;

and then

paint.setTypeface(typeface);

Solution 3 - Android

Kotlin syntax to setup Paint for bold text

paint = Paint(Paint.ANTI_ALIAS_FLAG).also { it.typeface = Typeface.DEFAULT_BOLD }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSampath KumarView Question on Stackoverflow
Solution 1 - AndroidGabe SechanView Answer on Stackoverflow
Solution 2 - AndroidAhamadullah SaikatView Answer on Stackoverflow
Solution 3 - AndroidCodeversedView Answer on Stackoverflow