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
Related
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.
How do I set up a constructor in a custom TextView to be able to pass text from a fragment?
In other words, I'm confused how to send text from my fragment (Fragment1) to the custom view (View1):
public class View1 extends TextView {
//constructors:
public View1(Context context, AttributeSet ats, int ds) {
super(context, ats, ds);
init();
}
public View1(Context context) {
super(context);
init();
}
public View1(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
...
canvas.drawText(myString, margin1, margin2, paint); //myString is from Fragment1
....
}
I asked a similar question here, but didn't really get much help. Example code would go a long way towards clearing up my confusion. Thanks in advance!
You are extending a TextView anyway. As A--C mentioned, you can use getText(), as well as setText() to get and set the text.
In your context, I am not sure if it is a good idea to use TextView to implement your custom view/widget. View might be a better starting point, as TextView carries all kind of stuff around for formatting, icon/drawable display, click/button logic etc.
You need to define the standard constructors if you want to be able to have the system instantiate/inflate your components from an XML layout. Then you can use standard getters/setters for your data, same way as all other controls do it.
If you instantiate your widget/view yourself (in your code), you are free to define whatever constructors you want to (I believe).
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
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"
/>
I have developed a very huge application and now i have a requirement of having custom font for all controls in the application. so I want to know the better way to change the font in one shot. The application has more than a hundred XML layout. and i cant change all controls to a custom component with custom font. Please provide a solution to Change the font without altering all the controls in XML.
Do something like this
pacage com.prac;
class MyFontedTextView extends TextView {
public FontedTextView(Context context) {
super(context);
init();
}
public FontedTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public FontedTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
String otfName = "MyCustomOtfFileWhichIPutInAssetsFolder.otf";
Typeface font = Typeface.createFromAsset(context.getAssets(), otfName);
this.setTypeface(font);
}
}
Now replace this all over in xml file from your TextViews
<com.prac.MyFontedTextView .... instead of <TextView
This change you have to do all over for it to apply
also for the case of button text . Button is also subclass of TextView
So the same can work for button's too
Hope this help or can lead you to the solution you are looking