environmenttypeface

How to set typeface from ExternalStorage and multi font


I've created a list of fonts in ExternalStorageDirectory and I'm saving it to :

ArrayList<String> list = new ArrayList<String>();

How do I set the typeface in textView from fonts to myFolder?

This code only sets typeface to one font in a directory

String root_sd = Environment.getExternalStorageDirectory().toString();
File name = new File(root_sd + "/myFolder/");
File[] files = name.listFiles();
for (int i = 0; i < files.length; i++){

    Typeface typeface = Typeface.createFromFile(files[i].getPath());
    textView.setTypeface(typeface);
    textView.setTextSize(20);

}

Solution

  • This is the sample for using 3 different typeface in 3 different textView based on your code. You can try this instead of removing the loop.

    String root_sd = Environment.getExternalStorageDirectory().toString();
    File name = new File(root_sd + "/myFolder/");
    File[] files = name.listFiles();
    
    Typeface typeface = Typeface.createFromFile(files[0].getPath());
    textView.setTypeface(typeface);
    textView.setTextSize(20);
    
    Typeface typeface2 = Typeface.createFromFile(files[1].getPath());
    textView2.setTypeface(typeface2);
    textView2.setTextSize(20);
    
    Typeface typeface3 = Typeface.createFromFile(files[2].getPath());
    textView3.setTypeface(typeface3);
    textView3.setTextSize(20);