Skip to content

Instantly share code, notes, and snippets.

@mancdevcarl
Created September 10, 2015 16:04
Show Gist options
  • Save mancdevcarl/fb2b97365326c57250ce to your computer and use it in GitHub Desktop.
Save mancdevcarl/fb2b97365326c57250ce to your computer and use it in GitHub Desktop.
Font Awesome Android
public class MyAppClass extends Application
{
public static Typeface FONT_AWESOME;
public static FontAwesomeHelper FONT_AWESOME_HELPER;
@Override
public void onCreate()
{
super.onCreate();
FONT_AWESOME = Typeface.createFromAsset(getAssets(), "fontawesome-webfont.ttf");
FONT_AWESOME_HELPER = new FontAwesomeHelper(loadJson());
}
public String loadJson()
{
String json = null;
try{
InputStream is = getAssets().open("font_awesome.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
}
catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}
}
public class FontAwesomeHelper
{
private static final String TAG = FontAwesomeHelper.class.getSimpleName();
private HashMap<String,String> mFontIcons = new HashMap<>();
public FontAwesomeHelper(String json)
{
parseIcons(json);
}
private void parseIcons(String json)
{
JSONObject jObject = null;
try{
jObject = new JSONObject(json);
Map<String, String> map = new HashMap<>();
Iterator iter = jObject.keys();
while(iter.hasNext()){
String key = (String) iter.next();
String value = jObject.getString(key);
map.put(key, value);
mFontIcons.put(key, value);
}
}
catch(JSONException e){
e.printStackTrace();
}
}
public String getUnicodeValue(String key)
{
//each icon class from my rest api was prefixed with 'fa fa-', so i remove it
String keyTrimmed = key.replace("fa fa-", "");
return mFontIcons.get(keyTrimmed);
}
}
...
TextView phoneIconTv = new TextView(this);
phoneIconTv.setTypeface(MyAppClass.FONT_AWESOME);
phoneIconTv.setText(MyAppClass.FONT_AWESOME_HELPER.getUnicodeValue("phone")
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment