Custom Font not working on Android - android

I am doing the below. All I get is the basic font, not my custom symbol font.
Any ideas?
Paint pnt = new Paint();
// SymbolNo is 38. Returns string "&" which is correct in normal font.
String symbolText = Character.toString((char)SymbolNo);
// Should adopt a symbol font and draw symbol to screen instead. But I just see "&"
Typeface tf = Typeface.createFromAsset(m_context.getAssets(), "fonts/myFont.TTF" );
pnt.setTypeface(tf);
m_canvas.drawText(symbolText,x, y, pnt);
my font is in assets/fonts/myFont.TTF

Not every font works with Android. It just silently fails.
One course of action is to find an app that definitely handles a custom font -- such as this sample app of mine -- as a basis for experimentation. You can run that app to confirm that its fonts appear, then replace one of those with your font. If that works, then there is something messed up in the way you are loading in the font (though I have no idea what or how). If the font fails to work in my sample app, where the font that ships with that app does work, the problem lies in the font.
Unfortunately, I have no idea what makes a font work or not work. You could try opening the font in a font editor, making a minor change (e.g., deleting some glyph you know that you won't need), saving it back out, and seeing if the revised font works. If it does, that means that however the font was saved originally has something in it that Android does not like, but that your font editor can generate Android-friendly fonts.

Hello I have a solution regarding this can you try using fonts in this way ... I Have implemented this in my Project ...
Steps:
Make a Package (com.fontUtils.fonts)
Make the Font Files Like For TextView , EditText or Button Text
For Example :
public class ButtonHelveticaBold extends Button {
Context ButtonFontContext;
public ButtonHelveticaBold(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
ButtonFontContext = context;
}
public ButtonHelveticaBold(Context context, AttributeSet attrs) {
super(context, attrs);
ButtonFontContext = context;
}
public ButtonHelveticaBold(Context context) {
super(context);
ButtonFontContext = context;
}
#Override
public void setTypeface(Typeface tf, int style) {
Typeface typeFaceHelvetica;
if (style == Typeface.BOLD) {
typeFaceHelvetica = Typeface.createFromAsset(getContext().getAssets(), "fonts/helvetica_bold_neue.ttf");
} else {
typeFaceHelvetica = Typeface.createFromAsset(getContext().getAssets(), "fonts/helvetica_neue_regular.ttf");
}
super.setTypeface(typeFaceHelvetica);
}
}
3 : Use this in XML Like this way:
<com.fontUtils.fonts.ButtonHelveticaBold
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>

Related

Android - How to add text style in my entire application

i have Helvetica Neue.ttf in asset Folder , How to set the Helvetica Neue textStyle on My Entire Applcation.
There is currently no way to do this with the Views that come with the Android SDK. You can set your View to use any of the Roboto fonts as per this answer, but you cannot set a custom font.
The way I typically tackle this problem is to create my own TextView that uses my font, like so:
public class MyFontTextView extends TextView {
public static final String FONT_PATH = "fonts/MyFont.ttf";
public MyFontTextView(Context context) {
super(context);
initFont();
}
public MyFontTextView(Context context, AttributeSet attrs) {
super(context, attrs);
initFont();
}
public MyFontTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initFont();
}
/**
* Set up the font.
*/
private void initFont() {
if (!isInEditMode()) {
Typeface font = Typeface.createFromAsset(getContext().getAssets(), FONT_PATH);
setTypeface(font);
}
}
}
You replace all of your TextViews with this TextView, and then you will have your font. Note that other UI elements (e.g. Buttons) will still use Roboto unless you also customize those.
If you have a View that you only use once in your application, you could call setTypeFace() on that View instead of creating a custom View. The custom View method works well for Views that you use a lot in an application such as TextViews.

Using Custom Fonts Properly in Android

So I've extended TextView to use a custom font (as described here), i.e.,
public class CustomTextView extends TextView {
public static final int CUSTOM_TEXT_NORMAL = 1;
public static final int CUSTOM_TEXT_BOLD = 2;
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
initCustomTextView(context, attrs);
}
private void initCustomTextView(Context context, AttributeSet attrs) {
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CustomTextView, 0, 0);
int typeface = array.getInt(R.styleable.CustomTextView_typeface, CUSTOM_TEXT_NORMAL);
array.recycle();
setCustomTypeface(typeface);
}
public setCustomTypeface(int typeface) {
switch(typeface) {
case CUSTOM_TEXT_NORMAL:
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "customTextNormal.ttf");
setTypeface(tf);
break;
case CUSTOM_TEXT_BOLD:
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "customTextBold.ttf");
setTypeface(tf);
break;
}
}
}
I then use CustomTextView in a fragment added to activity's main layout. All works fine, but there appears to be some memory issues, i.e., every time I rotate the screen (causing the activity to go through its life cycle), the font assets are loaded into the native heap in addition to the previous load. For example; below is a screen dump from adb shell dumpsys meminfo my.package.com after the initial load and no screen rotations (using Roboto-Light font):
and the same screen dump after a few rotaions
What is clear is the increase in the Asset Allocations and the Native Heap that occurs on each screen rotation (GC doesn't clear this up either). Surely then we shouldn't be using custom fonts in the manner described above and, if not, how should we be using custom fonts?
You should find your answer here.
Basically you need to build your own system to cache those typefaces after you create them (and therefore you'll only be creating each typeface once).

Custom font not showing properly some characters

I live in Hungary, and we got some special characters like: ő, ű... etc
In my android app i made a custom TextView. This custom TextView sets a custom typeface in its constructor and it works properly, except a little bug.
The special characters like: "ő" , does not converts the new typeface, its remains the same default font.
,
(Maybe some character coding thingee or i dont know really...)
(The font i use is Helvetica Neue Light, and if i open from Windows/Fonts folder in my computer i can see the special characters, so it means this font does have the "ő" character, but some reason android cannot handle it properly.)
Please help if you can! Thanks!
E D I T:
My custom textView class:
public class FlexiTextView extends TextView {
public FlexiTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initView(context);
}
public FlexiTextView(Context context) {
super(context);
initView(context);
}
public FlexiTextView(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context);
}
private void initView(Context context) {
try {
Typeface typeface = Typeface.createFromAsset(context.getAssets(), "fonts/helveticaneue.ttf");
this.setTypeface(typeface);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
that seems simple, the font you are using does not support some characters. Characters like ö, œ, or â are more than just a basic letter with an accent, they are designed (in the font) as a new and different caracter. That's why you have to choose a font that supports your language's special characters. Otherwise, the default system font will probably be used.
The problem is with your device where you are trying to set it. Just try to find other .ttf with the same font. And do not set typeface this way! Typeface.createFromAsset() is too expensive. Use caching for this. link which saves your performance

Android Custom Typeface

Hi I am using a custom Typeface in my app with the following code:
public Typeface font;
//Activity on create:
font = Typeface.createFromAsset(getAssets(),"DINOT-Medium.otf");
TextView tv = (TextView)vi.findViewById(R.id.textView1);
tv.setTypeface(font);
The problem is after a while it gets buggy: the text can no longer be read and only squares are visible. Do you know why this happens? How can it be fixed?
Thanks
Create a Custom TextView like this :
public class DINOTMediumTextView extends TextView {
public DINOTMediumTextView(Context context) {
super(context);
setCustomFont(context);
}
public DINOTMediumTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setCustomFont(context);
}
public DINOTMediumTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setCustomFont(context);
}
private void setCustomFont(Context context) {
Typeface tf = Typeface.createFromAsset(context.getAssets(), "fonts/DINOT-Medium.otf");
setTypeface(tf);
}
}
put the font's file in assets/fonts/ ( create a folder inside the assets folder)
and then in your layout xml :
<com.yourapp.views.DINOTMediumTextView
android:id="blabla"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
com.yourapp.views is the name of the package that contains your DINOTMediumTextView class.
On Mobiletuts+ there is very good tutorial on Text formatting for Android. Quick Tip: Customize Android Fonts
EDIT: Tested it myself now. Here is the solution. You can use a subfolder called fonts but it must go in the assets folder not the res folder. So
assets/fonts
Also make sure that the font ending I mean the ending of the font file itself is all lower case. In other words it should not be myFont.TTF but myFont.ttf

custom textview using custom font

I'm trying to implement a custom textview that uses my own custom font.
is there a way to set the typeface before doing a Super.onDraw()?
So as to replace the usual font to the custom font that I want to use.
Something like:
protected void onDraw(Canvas canvas)
{
Typeface font1 = Typeface.createFromAsset(context.getAssets(), "fonts/myfonts.ttf");
this.setTypeface(font1);
this.setTextSize(18);
super.onDraw(canvas);
}
I know the above code won't work.
Or do I have no choice but to use drawText() to do so?
It's a very bad practice to create new Typeface object on every time when your onDraw method is called. Such things as font set up should be done in the class constructor but not on every time your view is being drawn.
Oh my bad, it actually does change the font.
Just that it didn't show up on the preview on Eclipse but it does show on the emulator.
Problem solved.
public class CustomTextView extends TextView {
public CustomTextView(Context context, AttributeSet attributes) {
super(context, attributes);
applyCustomFont(context);
}
private void applyCustomFont(Context context) {
TypeFace customTypeFace = Typeface.createFromAsset(context.getAssets(), "custom_font_name");
setTypeface(customTypeFace);
}
#Override
public void setTextAppearance(Context context, int resid) {
super.setTextAppearance(context, resid);
applyCustomFont(context);
}
}
The code snippet creates a custom TextView and during the creation of the textview it set the custom font.
When you try to programmatically set the text appearance, the custom font is reset. Hence you can override the setTextAppearance method and set the custom font again.

Categories

Resources