Created
September 10, 2015 16:04
-
-
Save mancdevcarl/fb2b97365326c57250ce to your computer and use it in GitHub Desktop.
Font Awesome Android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
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