Android Set Custom Font to a Paint

android set custom font to a paint

If by "custom font" you mean a font that you are supplying as an asset, the following code should work:

Typeface plain = Typeface.createFromAsset(assetManager, pathToFont); 
Typeface bold = Typeface.create(plain, Typeface.DEFAULT_BOLD)
Paint paint = new Paint();
paint.setTypeface(bold);
canvas.drawText("Sample text in bold",0,0,paint);

android - how to set custom typeface in paint object?

try this:

    Typeface tf =Typeface.createFromAsset(getAssets(),"fonts/iran_sans_.ttf");
Paint paint = new Paint();
paint.setTypeface(tf);
canvas.drawText("Sample text",0,0,paint);

you can also use the Textpaint class instead of Paint

   TextPaint textPaint = new TextPaint();
textPaint.setTextSize(20);
textPaint.setTextAlign(Paint.Align.LEFT);
textPaint.setColor(Color.WHITE);
Typeface tf =Typeface.createFromAsset(getAssets(),"fonts/iran_sans_.ttf");
textPaint.setTypeface(tf);

see this: https://developer.android.com/reference/android/text/TextPaint.html

How android set custom font in canvas?

create "fonts" folder under "assets" folder. After that put your font file in "fonts" folder and write below code.

   Typeface tf =Typeface.createFromAsset(getAssets(),"fonts/YOURFONT.ttf");
Paint paint = new Paint();
paint.setTypeface(tf);
canvas.drawText("Sample text in bold RECOGNITION",0,0,paint);

Creating font and text styles in android with Paint object

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

Custom Font Android Studio With Draw?

Create the typeface elsewhere such that it is accessible in your drawText, as you do not wish to do this every time during rendering -

   Typeface type = Typeface.createFromAsset(getAssets(),"fonts/Kokila.ttf"); 

In your drawText, create a Paint object and set its typeface

   Paint paint = new Paint();
paint.setTypeface (type);

Then draw text using the paint object.

Android Set Custom font from web url

I wrote a sample project :

JAVA code :

import android.app.*;
import android.os.*;
import android.widget.*;
import android.graphics.*;
import java.io.*;
import android.view.*;
import java.net.*;
import android.util.*;

/*************
CODED BY : SIROS BAGHBAN
DATE : 13/7/2018
*/

public class MainActivity extends Activity
{
private TextView MyText;
private Button MyButt;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

MyText =(TextView)findViewById(R.id.fonta);
MyButt =(Button)findViewById(R.id.butt);

loadfont();

MyButt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loadfont();
}
});
}

// Load font if available on SD
private void loadfont () {
File file = new File("/mnt/sdcard/myfont.ttf");
if(file.exists()){

// Display font from SD
Typeface typeFace = Typeface.createFromFile(
new File(Environment.getExternalStorageDirectory(), "/myfont.ttf"));
MyText.setTypeface(typeFace);

}
else {

download();
}
}

// Download the custom font from the URL
private void download () {

new DownloadFileFromURL().execute("http://webpagepublicity.com/free-fonts/x/Xtrusion%20(BRK).ttf"); // Downlod LINK !

}

// File download process from URL
private class DownloadFileFromURL extends AsyncTask<String, String, String> {

@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... f_url) {
int count;
try {
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
conection.connect();
int lenghtOfFile = conection.getContentLength();
InputStream input = new BufferedInputStream(url.openStream(), 8192);
OutputStream output = new FileOutputStream("/sdcard/myfont.ttf");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress(""+(int)((total*100)/lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();

} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return null;
}
@Override
protected void onPostExecute(String file_url) {
// Display the custom font after the File was downloaded !
loadfont();
}
}
}

XML code :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Show me the custom font !"
android:id="@+id/butt"
android:layout_centerInParent="true"/>

<TextView
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="wrap_content"
android:text="Custom FONT !"
android:layout_above="@id/butt"
android:layout_centerHorizontal="true"
android:layout_marginBottom="30dp"
android:id="@+id/fonta"/>

</RelativeLayout>

Add these permissions in your manifest file :

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />


Note : Turn on the Internet to test the project :)

good luck.

How to change fontFamily of TextView in Android

From android 4.1 / 4.2 / 5.0, the following Roboto font families are available:

android:fontFamily="sans-serif"           // roboto regular
android:fontFamily="sans-serif-light" // roboto light
android:fontFamily="sans-serif-condensed" // roboto condensed
android:fontFamily="sans-serif-black" // roboto black
android:fontFamily="sans-serif-thin" // roboto thin (android 4.2)
android:fontFamily="sans-serif-medium" // roboto medium (android 5.0)

Sample Image

in combination with

android:textStyle="normal|bold|italic"

this 16 variants are possible:

  • Roboto regular
  • Roboto italic
  • Roboto bold
  • Roboto bold italic
  • Roboto-Light
  • Roboto-Light italic
  • Roboto-Thin
  • Roboto-Thin italic
  • Roboto-Condensed
  • Roboto-Condensed italic
  • Roboto-Condensed bold
  • Roboto-Condensed bold italic
  • Roboto-Black
  • Roboto-Black italic
  • Roboto-Medium
  • Roboto-Medium italic

fonts.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="font_family_light">sans-serif-light</string>
<string name="font_family_medium">sans-serif-medium</string>
<string name="font_family_regular">sans-serif</string>
<string name="font_family_condensed">sans-serif-condensed</string>
<string name="font_family_black">sans-serif-black</string>
<string name="font_family_thin">sans-serif-thin</string>
</resources>


Related Topics



Leave a reply



Submit