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.
Related
I have my own font that I want to use for my app in all layouts, I want to change the default font for my app.
In styles.xml I can use
<item name="android:fontFamily"></item>
to change the change the font, but how do I use my custom font here?
In App -> src -> main, i made a new directory called Assets, then another directory in that one called Fonts, and this is where i placed my custom font.
Is this possible? If so, how?
EDIT
As mayosk said, I added
compile 'uk.co.chrisjenx:calligraphy:2.2.0'
And made a Java Class that extends Application as shown below
public class CustomResources extends Application {
#Override
public void onCreate() {
super.onCreate();
CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
.setDefaultFontPath("fonts/din_light.ttf")
.setFontAttrId(R.attr.fontPath)
.build()
);
}
}
But the font is still the same, did I miss something
Use caligraphy :
https://github.com/chrisjenx/Calligraphy
Add dependency to your app build.gradle:
compile 'uk.co.chrisjenx:calligraphy:2.2.0'
and extend your application class where you need to add this code to onCreate method
CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
.setDefaultFontPath("Fonts/customFont.ttf")
.setFontAttrId(R.attr.fontPath)
.build()
);
and also you need to override attachBaseContext method() in your activity:
#Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}
First of all you need to create a base class which extends Textview, here is the baseclass
public class MyTextView extends TextView {
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setTypeFace(context);
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setTypeFace(context);
}
public MyTextView(Context context) {
super(context);
setTypeFace(context);
}
private void setTypeFace(Context context) {
setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/Lato-Bold.ttf"));
}}
later in xml
<com.example.MyTextView
android:id="#+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="hi" />
same for edittext also. By doing this all the textviews and edittexts have same font entire the app.
I have a special way to change the default font.I set a whole paint to create custom view and draw font on onDraw method,instead of xml way。
like this:
`private void initPaint(Context context)
{
mTextPaint = new TextPaint();
mTextPaint.setTextSize(66.46F);
mTextPaint.setColor(Color.BLACK);
Typeface typeface = Typeface.createFromAsset(context.getAssets(), "fonts/MFKeKe_Noncommercial-Regular.ttf");
mTextPaint.setTypeface(typeface);
mTextPaint.setTextSkewX(-0.5F);
}
#Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
mStaticLayout = new StaticLayout(TEXT1,mTextPaint,canvas.getWidth(), Layout.Alignment.ALIGN_NORMAL,1.0F,0.0F,false);
mStaticLayout.draw(canvas);
canvas.restore();
}`
But in usual,
For anyone still have question, the real, good answer is here:
https://stackoverflow.com/a/16883281/1154026
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.
I have a Scrollview and I have the attribute android:clickable="true" and android:autoLink="all".
I have a string for the ScrollView, and the emails, tel numbers etc, appear and are correctly clickable.
However, The string contains other numbers, such as Years, which also appear clickable and I don't want this; how can I stop this from happening?
Don't use autoLink="all", use the ones you need.
android:autoLink="web|email|phone" will probably cover your use cases.
The clickable="true" on the ScrollView isn't needed for this; rather you should set the autoLink attribute on the TextViews themselves; perhaps extracting a style if you have other common properties.
Add the new Linkify class to your project. From a place that you have access to the TextView (e.g. the Activity):
TextView myTextView = // get a reference to your textview
int mask = Linkify.ALL;
Linkify.addLinks(myTextView, mask);
The addLinks(TextView, int) method is static, so you can use it without creating an instance of Linkify. The return value (boolean) indicates whether something was linkified, but you probably don't need this information, so we don't bother with it.
You'll need to ensure that you don't put the autoLink attribute on the TextViews, otherwise the setText(...) implementations will still linkify years (unless you completely override the setText(...) implementations without calling super.setText(...))
For extra brownie points, you can create a subclass of TextView which will do the linkify for you when you set text on it:
public class AutoLinkifyTextView extends TextView {
public AutoLinkifyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public AutoLinkifyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
public void setText(String text) {
super.setText(text);
parseLinks();
}
#Override
public void setText(int stringRes) {
super.setText(stringRes);
parseLinks();
}
private void parseLinks() {
Linkify.addLinks(this, Linkify.ALL);
}
}
For top marks of course, you'd read the attributes from the attrs and use the correct mask from the XML attributes, but I'd prefer to get rid of that option and do it here.
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`m implementing a custom view which contains text, and the text will be drawn in onDraw.
My problem is, I wish my text will looks like those in the TextView objects, but I`m not quit familiar with setting up the style and typeface.
The text finally drawn with my fresh new Paint object, looks different from TextView: The lines looks thinner and seems bitten by some insects... - -b. I wish my text will be drawn just like those TextView objects.
Plz help!
====================
The problem in other words:
In a custom view extending View, I call mPaint.drawText in the onDraw method, with a new Paint object mPaint. But the text drawn looks different to the default TextView, the lines are thinner and not smooth(like were bitten by some insects). I just want it to be the same look as TextView.
public class CustomText extends TextView {
public CustomText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
Typeface typeface = Typeface.createFromAsset(context.getAssets(),
"fonts/your_font.ttf");
setTypeface(typeface);
}
public CustomText(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
Typeface typeface = Typeface.createFromAsset(context.getAssets(),
"fonts/fonts/your_font.ttf");
setTypeface(typeface);
}
public CustomText(Context context) {
super(context);
// TODO Auto-generated constructor stub
Typeface typeface = Typeface.createFromAsset(context.getAssets(),
"fonts/fonts/your_font.ttf");
setTypeface(typeface);
}
}
Now use the CustomText in your class/xml.
Hope this will help you.
You can create a custom view by extending TextView as a parent view. by doing this you will get all the default features that textView has and plus you can add other custom features as per your requirements.