Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating font and text styles in android with Paint object

Tags:

java

android

I am using the android Paint class to create text. I know how to set text size and color. I want to use Arial as font size and bold. How can I do it using the paint object. I have looked on the methods in the Paint class but couldn't find anything on how I can do it.

This is how I create my text style.

// Defining a paint object
paint = new Paint();
paint.setTextSize(30);
paint.setTextAlign(Paint.Align.LEFT);
paint.setAntiAlias(true);
paint.setColor(Color.WHITE);

Here is how I draw the text on a view.

g.drawString("My Text", 430, 774, paint);

How do I create the Arial font and bold text using the Paint class.

like image 678
EM10 Avatar asked Apr 16 '14 14:04

EM10


2 Answers

Use the TextPaint class instead of Paint. And can be implemented as below

TextPaint textPaint = new TextPaint();
textPaint.setTextSize(30);
textPaint.setTextAlign(Paint.Align.LEFT);
textPaint.setColor(Color.WHITE);
textPaint.setTypeface(Typeface.create("Arial", Typeface.BOLD));
like image 156
Senthilkumar Viswanathan Avatar answered Oct 27 '22 00:10

Senthilkumar Viswanathan


Januari 2020

Copy the fonts you want to use to res/font (e.g. opensans_regular.ttf, opensans_italic.ttf, opensans_bolditalic.ttf, etc.) Watch out no '-' of capitals in the name!

Create New Font resource file opensans.xml

<?xml version="1.0" encoding="utf-8"?>
<font-family
     xmlns:app="http://schemas.android.com/apk/res-auto">

    <font
        app:fontStyle="normal"
        app:fontWeight="400"
        app:font="@font/opensans_regular" />

    <font
        app:fontStyle="italic"
        app:fontWeight="400"
        app:font="@font/opensans_italic" />

    <font
        app:fontStyle="normal"
        app:fontWeight="700"
        app:font="@font/opensans_bold" />

    <font
        app:fontStyle="italic"
        app:fontWeight="700"
        app:font="@font/opensans_bolditalic" />

    <font
        app:fontStyle="normal"
        app:fontWeight="200"
        app:font="@font/opensans_light" />

    <font
        app:fontStyle="italic"
        app:fontWeight="200"
        app:font="@font/opensans_lightitalic" />

    <font
        app:fontStyle="normal"
        app:fontWeight="800"
        app:font="@font/opensans_extrabold" />

    <font
        app:fontStyle="italic"
        app:fontWeight="800"
        app:font="@font/opensans_extrabolditalic" />

</font-family>

In your MainActivity.java you can use the following code

    Paint paint = new Paint();
    Typeface typeface;

    if (Build.VERSION.SDK_INT >= 28) {
        // This does only works from SDK 28 and higher
        Typeface typefaceA = ResourcesCompat.getFont(this, R.font.opensans);
        typeface = Typeface.create(typefaceA, 700, false);
    } else {
        // This always works (Whole name without .ttf)
        typeface = ResourcesCompat.getFont(this, R.font.opensans_bolditalic);
    }
    paint.setTypeface(typeface);
like image 35
Caroline Avatar answered Oct 26 '22 23:10

Caroline