An implementation of Font Awesome for Android Projects.
Learn more about Font Awesome here
-
In your project level
build.gradle
add the repositoryallprojects { repositories { ... maven { url 'https://jitpack.io' } } }
-
In your module level
build.gradle
add the library (find the version numbers here)dependencies { ... implementation 'com.github.nathan-fiscaletti:fa-android:LATEST_VERSION' }
After installing the library, in your target (not in the fontawesome target), you will need to create a directory for the font assets.
- Create a directory in
your_app/src/main
calledassets
. - Within the
assets
directory create a directory calledfonts
. - Download your web fonts from font awesome and move either your free or pro font awesome font files into the assets directory, this project supports both. You should specifically move the
.ttf
font files. For Font Awesome 5.12.0 they are stored within thewebfonts
directory of the zip file you download.
The final directory structure should look like this:
<tk.nathanf.fontawesome.FATextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:fa_type="solid"
app:fa_icon="@string/fa_cloud_drizzle"
android:textColor="@android:color/black"
android:textSize="150sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
FATextView faTextView = new FATextView(context);
OR
FATextView faTextView = new FATextView(context, R.string.fa_check_circle, FATypeface.FAType.Solid, true);
The parameters are in the following order: The Context, the Icon, The Icon Type, true for autosize. If you set autosize to false when initializing it, you can later opt to autosize it using
faTextView.autoSizeIcon();
.
To change the icon set fa_icon
to @string/fa_...
where ...
is the name of the icon you would like to use. You can also control the type of icon used using the fa_type
property with one of the following: solid
, light
, regular
, or brands
.
Alternately, if you are working in code, you can use the following functions:
faTextView.setIcon(R.string.fa_check_circle);
faTextView.setTypeface(FATypeface.FAType.Solid);
You can control the icon color using
android:textColor
.
There are two ways to control the size of the icon.
- Use
wrap_content
for both the width and height of your view, and control the icon size using `android:textSize="XXsp". - Do not use
android:textSize
at all, and instead explicitly set width and height.
When using wrap_content
, the icon will follow the android:textSize
property for sizing.
When using explicate sizes, the icon will automatically scale to the size of the view.
You can opt to turn off auto-sizing using
app:fa_autosize="false"
.