For implementing custom font i seen few examples here issue is different,I am taking custom font in one abstract class which is used in all over the application, Here is my code
public abstract class X extends Activity implements OnClickListener {
private Vibrator vibrator;
private TextView TV_score;
private TextView TV_hints;
private ImageButton BTN_back;
// Font path
private String fontPath = "fonts/CarterOne.ttf";
Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);
public static Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Logger.log("onCreate " + this.getClass().getName());
vibrator = (Vibrator) this.getSystemService(VIBRATOR_SERVICE);
context = getBaseContext();
SoundHandler.getInstance().initSounds(context);
}
tried by debugging Here i am getting null pointer exception
Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);
how to resolve this problem give some suggestion .
create Typeface object in onCreate method of activity
public abstract class X extends Activity implements OnClickListener {
private Vibrator vibrator;
private TextView TV_score;
private TextView TV_hints;
private ImageButton BTN_back;
// Font path
private String fontPath = "fonts/CarterOne.ttf";
Typeface tf ;
public static Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Logger.log("onCreate " + this.getClass().getName());
vibrator = (Vibrator) this.getSystemService(VIBRATOR_SERVICE);
context = getBaseContext();
SoundHandler.getInstance().initSounds(context);
tf = Typeface.createFromAsset(getAssets(), fontPath);
}
Assign value of tf in onCreate() then it resolve your problem.
You can use a library; Calligraphy
In android , you can define your own custom fonts for the strings in your application. You just need to download the required font from the internet, and then place it in assets/fonts folder.
After putting fonts in the assets folder under fonts folder, you can access it in your java code through Typeface class. First , get the reference of the text view in the code. Its syntax is given below:
TextView tx = (TextView)findViewById(R.id.textview1);
The next thing you need to do is to call static method of Typeface class createFromAsset() to get your custom font from assets. Its syntax is given below:
Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/font name.ttf");
The last thing you need to do is to set this custom font object to your TextView Typeface property. You need to call setTypeface() method to do that. Its syntax is given below:
tx.setTypeface(custom_font);
Related
// 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.
public class XYZ extends LinearLayout{
TextView text = (TextView) findViewById(R.id.kid_name);
Typeface font = Typeface.createFromAsset(getAssets(), "eng111.ttf");
text.setTypeface(font);
}
I just try to use the other format for the text and the problem is occurred "create the getAssets() method "
Where do I mistake ? please sort out this problem
You need a Context, since you are not in an Activity you need to call:
getContext().getAssets();
I am trying to assign a different font to my project.
I want the new font is valid for the entire project, but all I find is to change the font to a textview
Typeface font1 = Typeface.createFromAsset(getAssets(), "fonts/RockFont.ttf");
TextView customText1 = (TextView)findViewById(R.id.text1);
customText1.setTypeface(font1);
customText1.setTextSize(40.f);
customText1.setText("Hello! This is a custom font...");
There any way to default to the entire project a custom font?
Best regard
Not exactly what you asked for, but building on the comment above there are ways to make using a custom font with default controls easier.
This shows how to extend TextView and use a custom attribute so the TextView supports a custom font.
Custom fonts and XML layouts (Android)
What I do is create a support class and instantiate it from my activity and pass through all the views I wish to style.
public class textfactory{
private TextView tv;
private Button b;
private RadioButton rb;
private TypeFace font;
/**
* fetch font resource
*/
public textfactory(Context context){
this.font = Typeface.createFromAsset(context.getAssets(), 'customfont.ttf');
}
/**
* pass in all the views you wish to apply font to
*/
public void style(View... views){
for(View v : views){
if(v instance of TextView)
{
tv = (TextView)v;
tv.setTypeface(this.font);
}
else if(v instance of Button)
{
b = (Button)v;
b.setTypeface(this.font);
}
else if(v instance of RadioButton)
{
rb = (RadioButton)v;
rb.setTypeface(this.font);
}
//add as many view conditionals as required
}
}
}
I have a ttf file, and its theoretically possible, but I'm not looking to touch over 500 different lines of code to programmatically change this. What is the easiest way?
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/Typefaces.html
To extend previous answer with static TYPEFACE:
Add new function:
public static void applyCustomFont(ViewGroup list, Typeface customTypeface) {
for (int i = 0; i < list.getChildCount(); i++) {
View view = list.getChildAt(i);
if (view instanceof ViewGroup) {
applyCustomFont((ViewGroup) view, customTypeface);
} else if (view instanceof TextView) {
((TextView) view).setTypeface(customTypeface);
}
}
}
Get root view of our Layout:
View rootView = findViewById(android.R.id.content)
Than apply custom font for whole activity form with all sub-elements:
applyCustomFont((ViewGroup)rootView, C.TYPEFACE.ArialRounded(this));
I've dealt with this problem myself; although I couldn't set a custom font globally, I was able to just make it little easier to deal with.
So in my Constants class (C.java) I have an Inner class:
public static final class TYPEFACE {
public static final Typeface Helvetica(Context ctx){
Typeface typeface = Typeface.createFromAsset(ctx.getAssets(), "helvetica.otf");
return typeface;
}
public static final Typeface ArialRounded(Context ctx){
Typeface typeface = Typeface.createFromAsset(ctx.getAssets(), "arial_rounded.ttf");
return typeface;
}
}
And in my code, after declaring and intializing the TextView I just set it's Typeface:
TextView title = (TextView)findViewById(R.id.title);
title.setTypeface(C.TYPEFACE.Helvetica(this));
I know this doesn't solve your problem but I hope it helps...
-serkan
Another way to go would be a custom view extending TextView. Not pretty, but you wouldn't need to copy the font code all over the place.
In all, its a pretty annoying problem for sure.
I have already read some articles and searched on Google, but I failed to do it.
My problem is regarding the font-face.
In Android, there are only 4 attributes in "android:typeface": Normal, Sans, Serif, Monospace.
So what do I have to do to use "Verdana" in my application?
Please suggest me a correct way to use this font in my Android application.
This is a simple example... create a folder in the root of your project called assets/fonts/ then paste the TTF font file (in this case Verdana.ttf). Then, if you want to apply that font to, say a TextView, do the following:
import android.graphics.Typeface;
public class FontSampler extends Activity {
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
TextView tv=(TextView)findViewById(R.id.custom);
Typeface face=Typeface.createFromAsset(getAssets(),
"fonts/Verdana.ttf");
tv.setTypeface(face);
}
}
This example was taken from the ComonsWare book (written by Mark Murphy). You can download the full example from GitHub.
You can use PixlUI at https://github.com/neopixl/PixlUI
import their .jar and use it in XML
<com.neopixl.pixlui.components.textview.TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world"
pixlui:typeface="GearedSlab.ttf" />
Well!!
This question is pretty old but still if someone is looking for the answer(in 2015) on how to apply custom font to all the Textviews through xml code directly see below:
First:
we need to add custom font inside assets folder inside your app directory:
.ttf or .otf both work in case of Android
Second:
Create Class CustomTextView which extends TextView like below:
public class CustomTextView extends TextView {
public CustomTextView(Context context) {
super(context);
}
public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public void setTypeface(Typeface tf) {
super.setTypeface(FontCache.getFont(getContext(),"fonts/<font_name>"));
}
}
Third:
FontCache class being used inside CustomTextView's setTypeface() method.Purpose is to do basic Font Caching using HashMap:
public class FontCache {
private static Map<String,Typeface> fontMap = new HashMap<String,Typeface>();
public static Typeface getFont(Context context,String fontname){
if(fontMap.containsKey(fontname)){
return fontMap.get(fontname);
}
else{
Typeface tf = Typeface.createFromAsset(context.getAssets(),fontname);
fontMap.put(fontname,tf);
return tf;
}
}
}
Fourth:[Final step]
All we do now is use the CustomTextView directly inside our xml file wherever custom font textview is required:
<<package_name>.CustomTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Custom Font Text"
android:textSize ="18sp"
android:textAppearance="?android:textAppearanceSmall"
android:id="#+id/custom_txt"
/>
Sorry, if this has already been posted somewhere on SO. Just thought to share if it helps someone!!
You can use simple EasyFonts third party library to set variety of custom font to your TextView. By using this library you should not have to worry about downloading and adding fonts into the assets/fonts folder. Also about Typeface object creation.
This library does not provides Verdana Font face.
But provide following font faces. Which might you would like to use.
Roboto
Droid Serif
Droid Robot
Freedom
Fun Raiser
Android Nation
Green Avocado
Recognition
Simply:
TextView myTextView = (TextView)findViewById(R.id.myTextView);
myTextView.setTypeface(EasyFonts.robotoThin(this));
I am author of this library.
To change the (custom) font of your app globally, have a look at Calligraphy
Simply add Calligraphy to your gradle.build and add the following snippet to your Application.onCreate():
CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
.setDefaultFontPath("fonts/MyCustomFont.ttf")
.setFontAttrId(R.attr.fontPath)
.build()
);
and in every Activity add the following:
#Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}
That is all you need to do to change the font globally in your App. Have a look at the docs for more details.
// My example show you how to change fonts into a normal textView or list view
create a fonts folder into your assets dir of android and copy your custom font in that ..
assets/fonts/monaco.ttf
// Font path
String fontPath = "fonts/monaco.ttf";
// Loading Font Face
Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);
// CASE 1 : Inside your list view
holder.name = (TextView) convertView
.findViewById(R.id.textView_cityName);
// set name of text in each row
holder.name.setText(CitiesNames.get(position));
// set the type of font you want to set
holder.name.setTypeface(tf);
// CASE 2 : Inside your text view
TextView tx = (TextView)findViewById(R.id.textview1);
tx.setTypeface(tf);
//vKj
TextView textView = (Textview) findViewById(R.id.mytext);
Typeface face=Typeface.createFromAsset(getAssets(),
"fonts/Verdana.ttf");
textView.setTypeFace(face);