// Font path
String fontPath = "fonts/jcc.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);
In Android Studio it comes up with an error, "Cannot resolve symbol 'setTypeface'" and "Unknown class: 'tf'". I do not know why, I defined 'tf' and I have looked at many tutorial's that use setTypeface.
Please Help!
Edit, here is a screenshot, I am using this exact code and my fonts are under 'assets/font/jcc.tf'.
http://i.imgur.com/fcDdVRz.png
Sorry do not have enough reputation to post images :(
After much trial and error I found the solution!
My code was after the onCreate method, once I placed it inside all the errors went away!
The code with errors:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
// Font path
String fontPath = "fonts/SECRCODE.ttf";
// text view label
TextView txtGhost = (TextView) findViewById(R.id.java1);
// Loading Font Face
Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);
// Applying font
txtGhost.setTypeface(tf);
The code without errors (notice the code is now inside onCreate method):
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Font path
String fontPath = "fonts/SECRCODE.ttf";
// text view label
TextView txtGhost = (TextView) findViewById(R.id.java1);
// Loading Font Face
Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);
// Applying font
txtGhost.setTypeface(tf);
}
If anyone could explain why this works it would help greatly!
Thanks!
The reason you need to set the font (typeface) in the onCreate method is because your Java code in the onCreate method is automatically executed when an activity is loaded. Without putting it inside the onCreate() method there is no reference/call to execute your code.
Related
This question already has answers here:
Android - Using Custom Font
(21 answers)
Closed 5 years ago.
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);
}
}
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.
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
This tutorial link helps to understand on configuring the custom font in android.
You can use android:fontFamily attribute in xml file.
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);
}
}
I am making a text editor. I have a image button and what I want is that when I click that image button the editor font would change to a custom font given in assets folder.
Also when I run the app it unfortunately stops. Below is the java code.
String fontPath = "fonts/PORNSA_.TTF";
final Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);
et = (EditText) findViewById(R.id.editText);
fontButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
et.setTypeface(tf);
}
});
You want to change your font file name without Caps and "_" symbol?
I have recently come across a situation while developing an app where i have to display different languages in text view . currently I am displaying few using fonts/typeface like this :
Typeface tf = Typeface.createFromAsset(this.getAssets(),
"DroidHindi.ttf");
TextView textView1 = (TextView) findViewById(R.id.textView2);
textView1.setTypeface(tf);
textView1.setText("कचजड, कचजड");
Typeface tf1 = Typeface.createFromAsset(this.getAssets(),
"asunaskh.ttf");
TextView textView = (TextView) findViewById(R.id.textView1);
textView.setTypeface(tf1);
textView.setText("یہ انگریزی نہیں");
Typeface tf2 = Typeface.createFromAsset(this.getAssets(),
"Banglafont.ttf");
TextView textView2 = (TextView) findViewById(R.id.textView3);
textView2.setTypeface(tf2);// এই ইংরেজি নয়
textView2.setText("এই ইংরেজি নয়");
its fine my question is that i have to support for some 20 different languages then things will become very tedious when i apply this in different activities . Any alternative way to achieve.
You do is to create a class and a function with access public that will return you TextView Object with the Font that you want to suppose.
TextView public SetLanguage(TextView tv,string type)
{
TextView newtv = tv;
Typeface tf;
switch(type)
{
case "urdu":
tf = Typeface.createFromAsset(this.getAssets(),"urdu.ttf");
break;
case "hindi":
tf = Typeface.createFromAsset(this.getAssets(),"hindi.ttf");
break;
// up so on
}
newtv.setTypeface(tf);
return newtv;
}
// and call it any where..
TextView textView1 = (TextView) findViewById(R.id.textView2);
textView1 = classobj.SetLanguage(textView1,"urdu");
//assign string of text to it
Initialize your typefaces when the app starts up, and make a method which takes any view and sets the typeface based on the language.
Just like we do with other resource files, we can also define a font directory for any locale or country code we want to. The downside to this approach is that both the fonts have to be named the same although they are different but otherwise a clean solution.
I want to apply Font type without using XML. By code, I want to change Font type and it should be able to apply for a whole application, when I click Arial else Times New Roman etc.
try this code to change the font of app
TypeFace typeFace = Typeface.createFromAsset(getAssets(), "font.ttf");
TextView view= (TextView) findViewById(R.id.view);
view.setTypeface(typeFace)
I would reccomend having a list preference, in a preference screen. You can then set the fonts from that. i.e:
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
String font = sp.getString("font","-1")
if(font.equals("timesroman")) {
TypeFace typeFace = Typeface.createFromAsset(getAssets(), "timesnewroman.ttf");
}
else if(font.equals("arial")) {
TypeFace typeFace = Typeface.createFromAsset(getAssets(), "arial.ttf");
}
TextView view= (TextView) findViewById(R.id.view);
view.setTypeface(typeFace)
I want to draw a black stroke on my text in Android.
I have seen this example:
How do you draw text with a border on a MapView in Android?
Where the solution overrides onDraw() to create the stroke.
The problem is, I'm still relatively starting out in Android, and I have no idea how to transition to using that solution.
In my onCreate I set the text typeface (it's custom):
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeatures();
// Set content view and component listeners
setContentView(R.layout.meme_maker);
setListeners();
context = this;
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/Impact.ttf");
TextView mmt = (TextView) findViewById(R.id.meme_maker_title);
TextView ttc = (TextView) findViewById(R.id.top_text_canvas);
TextView tbc = (TextView) findViewById(R.id.bottom_text_canvas);
ttc.setTypeface(tf);
tbc.setTypeface(tf);
mmt.setTypeface(tf);
}
And I have an onClickListener where I change the text content of the TextView, based on the user writing the text he/she wants in a TextEntry and clicking a button afterwards.
ImageView ii = (ImageView) findViewById(R.id.insert_image);
ii.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText tt = (EditText) findViewById(R.id.top_text_text);
EditText bt = (EditText) findViewById(R.id.bottom_text_text);
TextView ttc = (TextView) findViewById(R.id.top_text_canvas);
TextView btc = (TextView) findViewById(R.id.bottom_text_canvas);
ttc.setText(tt.getText().toString().toUpperCase());
btc.setText(bt.getText().toString().toUpperCase());
}
});
It's pretty straightforward so far. My question is: how to insert the stroke of the text? Where? Do I need to create a Canvas and Paint objects?
The simplest way to get a shadow for text rendered in a TextView is to set up a style as described in this answer. That requires very little work and sounds like it will work fine in your situation.
Using the technique you link to involves extending an existing View class, overriding onDraw(), and using Canvas.drawText() on the canvas passed to onDraw() to render the text yourself. That can be exactly what you need in some situations, but sounds like overkill for your current situation. If you want to look into it further, the Android dev guide on the subject is a good read.