0

I'm new to programming and I'm looking to add a custom font I download to Android Studio. I was able to follow instructions how to add the font but when I run the app I can only get one TextView of my two two TextViews to use this font. This is my code, can someone please tell me what I'm doing wrong here. Thank you!

public class MainActivity extends AppCompatActivity {
TextView text, text2;
    Typeface tfc1, tfc2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text = (TextView) findViewById(R.id.top_text);
        text2 = (TextView) findViewById(R.id.bottom_text);

        tfc1 = Typeface.createFromAsset(getAssets(),"fonts/StarWars.ttf");
        text.setTypeface(tfc1);

        tfc1 = Typeface.createFromAsset(getAssets(),"fonts/StarWars.ttf");
        text2.setTypeface(tfc2);



    }
}
Zach
  • 33
  • 7

5 Answers5

1

I assume there's a typo. You wrote tfc1 but you set tfc2. From your code:

tfc1 = Typeface.createFromAsset(getAssets(),"fonts/StarWars.ttf");
            text2.setTypeface(tfc2);  

As both of the tfc1 and tfc2 fonts are same. You can use one typeface to both of the textviews as @Sagar Patel showed.

Farwa
  • 6,156
  • 8
  • 31
  • 46
0

STEP 1/

Start by creating a folder named assests then inside that folder create another one named folder and import your *.ttf files to that folder

STEP 2/

Now import this before start writing the code given below:

import android.graphics.Typeface;

Now implement the following code to your class:

// Font path
String fontPath = "fonts/Face Your Fears.ttf";

// text view label
TextView txtGhost = (TextView) findViewById(R.id.ghost);

// Loading Font Face
Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);

// Applying font
txtGhost.setTypeface(tf);

I suggest you follow this tutorial right here it will guide step-by-step through using external fonts in Android Studio

Thorvald
  • 3,424
  • 6
  • 40
  • 66
  • I am having the same issue with these steps. I can only get my TextView id= top_text to use the font. – Zach Jan 18 '18 at 03:48
0

This tutorial link helps to understand on configuring the custom font in android.

enter image description here

Custom Font for android textview

Takermania
  • 1,345
  • 2
  • 12
  • 20
0

You can use android:fontFamily attribute in xml file.

Saurabh Vadhva
  • 511
  • 5
  • 12
0
public class MainActivity extends AppCompatActivity {
TextView text, text2;
    Typeface tfc1, tfc2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text = (TextView) findViewById(R.id.top_text);
        text2 = (TextView) findViewById(R.id.bottom_text);

        tfc1 = Typeface.createFromAsset(getAssets(),"fonts/StarWars.ttf");
        text.setTypeface(tfc1);

      //  tfc1 = Typeface.createFromAsset(getAssets(),"fonts/StarWars.ttf");
     //you get error in this line   text2.setTypeface(tfc2);

       text2.setTypeface(tfc1);
    }
}
Sagar Patel
  • 224
  • 1
  • 9