[Android Studio] How to change TextView to any font [Java]

From left image to right image

Introduction

Prepare a free font. FONT FREE(https://fontfree.me/) Free font Kensaku (https://cute-freefont.flop.jp/) There are many others, but the above site is recommended. This time, I will use "Rondo B" here. Download the font you want to use. image.png

Prepare the project

This time I created a project with the name TestFont. We will proceed assuming that you have MainActivity.java and activity_main.xml.

Change the project name, save location, etc. according to your own project.

image.png image.png

Prepare the assets folder

The font file should be in the asset folder, but it doesn't exist at first, so create it. Right-click on the app in the upper left corner of the Android Studio screen and select ** New → Folder → Asset Folder **. image.png Make sure the Target Source Set is main and press Finish. image.png The assets folder has been created. image.png Right-click on the assets folder and select Show in Explorer. (Of course, you can deploy Explorer yourself) image.png Place the downloaded font file in the assets folder. image.png

If the font file is added when you open the AndroidStudio assets folder, you're ready to go. image.png

Tag .xml file

Two lines have been added from the default. We'll be using tags in our .java files, so tag text = "Hello World!". This is the second line. The letters are small and hard to see, so I made them larger. This is the 6th line.

activity_main.xml


    <TextView
        android:id="@+id/text01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:textSize="30sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

Introduced in .java file

Here is the default

MainActivity.java


package test.com.testfont;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

The 3rd, 4th, and 5th lines of the onCreate method are the added lines. Change variables (rondeB, text, etc.) as appropriate. I think import will be added automatically. (If it is not added automatically, it will be added by pressing Alt + Enter)

MainActivity.java


package test.com.testfont;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Typeface;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Typeface rondeB = Typeface.createFromAsset(getAssets(), "Ronde-B_square.otf");
        TextView text = findViewById(R.id.text01);
        text.setTypeface(rondeB);
    }
}

The finished product

## reference## [[Android Studio] How to use custom fonts (Java code)](https://codeforfun.jp/android-studio-how-to-use-custom-fonts/)

Recommended Posts

[Android Studio] How to change TextView to any font [Java]
[Android Studio] [Java] How to fix the screen vertically
Java to C and C to Java in Android Studio
How to use ExpandableListView in Android Studio
[Android] How to detect volume change (= volume button press)
How to use Truth (assertion library for Java / Android)
How to change from Oracle Java 8 to Adopt Open JDK 9
[Java] How to use Map
How to lower java version
[Java] How to use Map
How to uninstall Java 8 (Mac)
Java --How to make JTable
How to use java Optional
How to minimize Java images
How to write java comments
How to use java class
[Java] How to use Optional ②
[Java] How to use removeAll ()
[Java] How to display Wingdings
[Java] How to use string.format
How to use Java Map
How to set Java constants
How to use Java variables
How to convert Java radix
[Java] How to implement multithreading
[Java] How to use Optional ①
How to initialize Java array
How to take a screenshot with the Android Studio emulator
Refer to C ++ in the Android Studio module (Java / kotlin)
Use Java included with Android Studio to build React Native
[2020 version] How to send an email using Android Studio Javamail
How to make an app using Tensorflow with Android Studio
How to implement one-line display of TextView in Android development
How to study Java Silver SE 8
How to use Java HttpClient (Get)
[Rails] How to install Font Awesome
Studying Java # 6 (How to write blocks)
How to "hollow" View on Android
[Java] How to update Java on Windows
How to make a Java container
How to disassemble Java class files
How to enclose any character with "~"
How to use Java HttpClient (Post)
[Java] How to use join method
How to learn JAVA in 7 days
Android: How to deal with "Could not determine java version from '10 .0.1'"
How to change kube-proxy to ipvs mode.
[Processing × Java] How to use variables
[Ruby] How to use any? Method
[Java] How to create a folder
[Android Studio] [Java] Learning memos & links
How to decompile java class files
[Java] How to use LinkedHashMap class
Source to display character array with numberPicker in Android studio (Java)
[JavaFX] [Java8] How to use GridPane
How to write Java variable declaration
[Rails] How to install Font Awesome
How to use class methods [Java]
[Java] How to use List [ArrayList]
How to use classes in Java?
How to name variables in Java