0

I have to make an application that can retrieve font list that is pre-installed with Android OS. I need to display system font and can select to set that font for my application.

I have number of feature in my application and i have to get random system font and apply this font style to my custom soft keyboard. how i can apply this font to my custom soft keyboard.

How i can access this font and apply to my application?

Vasudev Vyas
  • 726
  • 1
  • 10
  • 28
  • 3
    Possible duplicate of [How to retrieve a list of available/installed fonts in android?](https://stackoverflow.com/questions/3532397/how-to-retrieve-a-list-of-available-installed-fonts-in-android) – Nouman Ch Mar 07 '19 at 04:34
  • Okkey This is duplicated i will delete if i get answer from it. – Vasudev Vyas Mar 07 '19 at 04:37
  • Don't delete, but accept that this is duplicate by clicking the button above this question. – Gourav Mar 07 '19 at 05:14
  • @NoumanCh Do it for me . – Vasudev Vyas Mar 07 '19 at 05:42
  • @VasudevVyas I can't mark it as duplicate you have to do this by yourself. check this link https://meta.stackexchange.com/questions/79916/is-it-possible-to-mark-my-own-question-as-duplicate-of-another. – Nouman Ch Mar 07 '19 at 06:57

3 Answers3

1

File Manager Class to Load System Fonts

public class FontManager {
    // This function enumerates all fonts on Android system and returns the HashMap with the font
    // absolute file name as key, and the font literal name (embedded into the font) as value.
    static public HashMap< String, String > enumerateFonts()
    {
        String[] fontdirs = { "/system/fonts", "/system/font", "/data/fonts" };
        HashMap< String, String > fonts = new HashMap< String, String >();
        TTFAnalyzer analyzer = new TTFAnalyzer();

        for ( String fontdir : fontdirs )
        {
            File dir = new File( fontdir );

            if ( !dir.exists() )
                continue;

            File[] files = dir.listFiles();

            if ( files == null )
                continue;

            for ( File file : files )
            {
                String fontname = analyzer.getTtfFontName( file.getAbsolutePath() );


                if ( fontname != null ) {
//                    Log.d("fonts", fontname+" : "+file.getAbsolutePath());
                    fonts.put(file.getAbsolutePath(), fontname);
                }

            }




        }

        return fonts.isEmpty() ? null : fonts;
    }

Font Detail Model Class:

public class FontDetail
{
    String sFontNames;
    String sFontPaths;
    String sFontType;

    public FontDetail(String sFontNames, String sFontPaths, String sFontType)
    {
        this.sFontNames = sFontNames;
        this.sFontPaths = sFontPaths;
        this.sFontType = sFontType;
    }

    public String getsFontNames()
    {
        return sFontNames;
    }

    public void setsFontNames(String sFontNames)
    {
        this.sFontNames = sFontNames;
    }

    public String getsFontPaths()
    {
        return sFontPaths;
    }

    public void setsFontPaths(String sFontPaths)
    {
        this.sFontPaths = sFontPaths;
    }

    public String getsFontType()
    {
        return sFontType;
    }

    public void setsFontType(String sFontType) {
        this.sFontType = sFontType;
    }
}

Array List to Hold Fonts List:

   ArrayList<FontDetail> arrLstFontDetail = new ArrayList<FontDetail>();

Load System Fonts in arrLstFontDetails:

public void loadSystemFontsToListView() {
      try {
         Iterator<?> iter = FontManager.enumerateFonts().entrySet().iterator();

         while (iter.hasNext()) {
            @SuppressWarnings("rawtypes")
            Map.Entry mEntry = (Map.Entry) iter.next();
            LogMaintain.ShowLog(LogMaintain.LogType.Error, "System : " + (String) mEntry.getValue() + "Key: " + (String) mEntry.getKey());
            arrLstFontDetail.add(new FontDetail((String) mEntry.getValue(), (String) mEntry.getKey(), "System"));
         }
}
Muhammad Hassaan
  • 874
  • 6
  • 18
1

How to retrieve a list of available/installed fonts in android?

From above original question i got my answer but i am extending my answer with what i really wanted to do. how can we access those font in our application.

We can access system from path using this function (Typeface.createFromFile("your file")

      String path = "/system/fonts";
      File file = new File(path);
      File ff[] = file.listFiles();

       for(int i = 0 ; i < ff.length ; i++){
       fontName.setText(file.getName());
       fontName.setTypeface(Typeface.createFromFile(ff[i]));   
       }
Vasudev Vyas
  • 726
  • 1
  • 10
  • 28
0

Typeface is the class which would help you to get the list of all the fonts installed. Then you can map the fonts. This link could help out from it : How to get name of the font applied on the textview

bhumilvyas
  • 121
  • 5