Vasudev Vyas
Vasudev Vyas

Reputation: 742

How i can get Android System fonts list that is already installed and apply to custom keyboard

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?

Upvotes: 0

Views: 1909

Answers (3)

Vasudev Vyas
Vasudev Vyas

Reputation: 742

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

Upvotes: 1

Muhammad Hassaan
Muhammad Hassaan

Reputation: 1017

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

Upvotes: 1

bhumilvyas
bhumilvyas

Reputation: 121

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

Upvotes: 0

Related Questions