Using Library
I found an example very similar to what you want to do using a library, it's in this page .
This page explains how to use a library to use the FontIcon using the files < strong> .ttf and .css of icon packs that can be downloaded from the page for example.
Resources
What it does next is to open the .css in which the icons are defined in the following way:
.ic-quote:before {
content: "\e000";
}
.ic-users:before {
content: "\e001";
}
.ic-info:before {
content: "\e002";
}
.ic-edit:before {
content: "\e003";
}
.ic-delete:before {
content: "\e004";
}
These are styles with unicode characters of our icons. We will rewrite them as string resources. For this, in the res/values/
folder, an .xml file will be created that is called, for example, icons.xml
The styles of the .css will be rewritten as string resources remaining as follows:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="ic_quote"></string>
<string name="ic_users"></string>
<string name="ic_info"></string>
<string name="ic_edit"></string>
<string name="ic_delete"></string>
</resources>
Note: You now have an automated parser available: FonIcon prepare for Android
The file with the icons will be placed inside the asserts folder:
assets/icons.ttf
Gradle
Add in build.gradle
:
repositories {
mavenCentral()
}
dependencies {
compile 'com.shamanland:fonticon:0.1.8'
}
Code
There are two important classes: FontIconDrawable and FontIconTypefaceHolder . The first extends the Drawable abstract class, and the second is only for the static storage of the Typeface instance loaded from the .ttf file.
The Typeface file will be initialized in the Application.onCreate () method:
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
FontIconTypefaceHolder.init(getAssets(), "icons.ttf");
}
}
The use of FontIconDrawable
is very simple, for example in Fragment.onCreateView()
:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle state) {
Drawable[] icons = new Drawable[]{
FontIconDrawable.inflate(getResources(), R.xml.ic_quote),
FontIconDrawable.inflate(getResources(), R.xml.ic_users),
FontIconDrawable.inflate(getResources(), R.xml.ic_info),
};
// use drawables for any purpose
}
You have a complete example in GitHub
Unused Library
On the other hand I think that this could be done without the use of libraries
Using Font Awesome how it says this StackOverflow response in English
Copy fontawesome-webfont.ttf
into the asserts folder
Find the character entities for the icons you wanted, using this page
Create an entry in strings.xml
for each icon. For example, for the heart:
<string name="icon_heart"></string>
Reference said entry in the view of your xml design
<Button
android:id="@+id/like"
style="?android:attr/buttonStyleSmall"
...
android:text="@string/icon_heart" />
Load the source in the onCreate
method and set it to the appropriate Views:
Typeface font = Typeface.createFromAsset( getAssets(), "fontawesome-webfont.ttf" );
...
Button button = (Button)findViewById( R.id.like );
button.setTypeface(font);
On the other hand I think it could be done using .svg
files. But I have not found anything yet, I will continue investigating the subject.
Some steps to transform the .png to .svg
The first thing you should do is transform the .png with all the icons to .svg. There are many online tools and services to do this, you can use this for example. Remember to specify the Monochrome option when converting the .png.