2

I want to set custom font from url like HiFont, HiFont application below screenshots!

here screenshot

Someone please help me. i want to typeface font not from assets/, from url eg: http://example.com/fonts/test.ttf.

Thanks.

3
  • Possible duplicate of Android - Using Custom Font Commented Jul 13, 2018 at 11:59
  • this is a different, i want to use font from url. not from assets folder Commented Jul 13, 2018 at 12:09
  • It's simple :) well, to do this, you must first download the font from the URL and use it ! Commented Jul 13, 2018 at 12:45

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

So you are basically downloading them at temporary path?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.