I want to know how do I use an external font in Android Studio as there is no Assets folder . I have look for a helpful turtorial on inte.net but they all pretend to use Assets folder.
I created an asset folder myself in src/main
but Android Studio doesnt recognie getAssets()
.
Go on your Project: app-->src-->main
Create a assets folder like this:
|assets
|-----------------fonts
|-------------------font.ttf
|java
|res
AndroidManifest.xml
and then use
Typeface face=Typeface.createFromAsset(getAssets(),"fonts/digital.ttf");
txtV.setTypeface(face);
If you have custom font then use following code:
TextView tv=(TextView)findViewById(R.id.custom);
Typeface face=Typeface.createFromAsset(getAssets(),"fonts/Verdana.ttf");
tv.setTypeface(face);
Also place your font file in assets/fonts folder and follow the instructions from here.
NOTE: You have to make asset folder by yourself
Put your font files (eg customfont.tff and customfont_bold.tff) under the folder app>src>res>font> (Note: If it is not present, create font folder)
Additionally, create a file named fonts.xml inside the same folder with the following content:
<font-family xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<font
app:fontStyle="normal"
app:fontWeight="700"
app:font="@font/customfont"/>
<font
app:fontStyle="normal"
app:fontWeight="700"
app:font="@font/customfont_bold"/>
</font-family>
Then, edit the file app>src>res>values>styles.xml to apply a default font for the entire app.
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:fontFamily">@font/customfont</item>
</style>
If you want to change the font of an individual UI element:
<TextView
android:fontFamily="@font/customfont"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="@color/black"
android:textSize="18sp"
android:text="Some text"
/>
NOTE: This method is valid for API Level 16+ (For more info: https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml )
if you are having an error with "getAssets()" method then you can use following way. in assets folder puts font family
Typeface getFace=Typeface.create("OpenSans",3);
textView = (TexView) findViewById(R.id.textView);
textView.setTypeface(getFace);
If you'e tried both the res/asset/font and main/asset font folders and you've tried different fonts, and it failed to work -- it's likely to be an Android Studio bug.
I had the same problem but I solved it by importing my font to an online font editor (search for pentacom font editor) and exporting the font and saving it to a new ttf file. The resulting font will be lower in resolution but it worked for me.
There might be other online font editors/exporters you can try.
According to android developers : Android 8.0 (API level 26) introduces a new feature, Fonts in XML, which lets you use fonts as resources. You can add the font file in the res/font/ folder to bundle fonts as resources. These fonts are compiled in your R file and are automatically available in Android Studio. You can access the font resources with the help of a new resource type, font. For example, to access a font resource, use @font/myfont, or R.font.myfont.
for more details : https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml.html
To use external fonts, first download the font in.tff format Google font- Roboto
Add the font asset folder as shown in the image below
After the font asset folder is created, copy and paste the downloaded.tff font into the "font" folder. (make sure the name is formatted well. )
Reference the font in your theme.xml or any layout using the android:fontFamily="@font/splashfont" property.
This is how you do it in a theme.xml file
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.FishPott" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/color_black_level_1</item>
<item name="colorPrimaryVariant">@color/color_black_level_2</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/color_black_level_1</item>
<item name="colorSecondaryVariant">@color/color_black_level_2</item>
<item name="colorOnSecondary">@color/color_white_level_1</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
<item name="android:fontFamily">@font/robotoregular</item>
</style>
This is how you do it in a textview
<com.google.android.material.textview.MaterialTextView
android:id="@+id/activity_start_fp_MaterialTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="32dp"
android:fontFamily="@font/splashfont"
android:gravity="center"
android:text="MyText"
android:textColor="@color/color_black_level_1"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/activity_start_logo_ShapeableImageView" />
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.