HUHO
HUHO

Reputation: 121

How to import google fonts in Android Studio?

I'm a complete beginner to Android Studio, I'd like to know how to import new fonts(more precisely: Roboto and Pacifico font families)

Upvotes: 9

Views: 22477

Answers (4)

Dankyi Anno Kwaku
Dankyi Anno Kwaku

Reputation: 1293

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

enter image description here

enter image description here

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" />

Upvotes: 5

flash76
flash76

Reputation: 382

It's easy.

  1. Place a TextView.
  2. In the properties pane, select the font-family dropdown. If it isn't there, find the caret thingy (the > and click on it to expand the text attributes menu) Expand the font-family drop down.
  3. In the little list, scroll all the way down till you see more fonts
  4. This will open up a dialog box where you can search from Google Fonts
  5. Search for the font you like with the search bar at the top
  6. Select your font.
  7. Select the style of the font you like (i.e. bold, normal, italic, etc)
  8. In the right pane, choose the radio button that says Add font to project
  9. Click okay. Now your TextView has the font you like!

BONUS: If you would like to style EVERYTHING with text in your application with chosen font, just add <item name="android:fontfamily">@font/fontnamehere</item> into your styles.xml file.

Upvotes: 13

meditat
meditat

Reputation: 1165

For setting the font of your step there are some simple steps.

1.select File>New>Folder>Assets Folder

enter image description here

2.click finish

3.right click on assets and create a folder called fonts

4.put your font file in assets > fonts

5.then code to change your font(or create a new theme using that font.)enter image description here

Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/anyFont.ttf");
//and then use the typeface for changing the font using `textView.setTypeface(tf)`

Upvotes: 5

demo_Ashif
demo_Ashif

Reputation: 314

If you want to use these font for your android studio ide then download and install the fonts and change android studio code text font.

If you want to use font for your created android application then you have to put all .ttf files inside asset folder of the app. Please google that how to create asset folder and how to use custom font in your created application

Upvotes: -3

Related Questions