how to specify the font file in the settings instead of code? - android

I'm trying to decorate my application fonts. I get to do it from code. but do not know how to do it in the settings. in the code I do so
public class Fonts {
public static Typeface getHeaderFont(Context context){
return Typeface.createFromAsset(context.getAssets(), "header_levelI.ttf");
}
public static Typeface getSubHeaderFont(Context context){
return Typeface.createFromAsset(context.getAssets(), "header_levelII.ttf");
}
}
and
Typeface type= Fonts.getHeaderFont(getActivity());
TextView header = (TextView) v.findViewById(R.id.cassaName);
header.setTypeface(type);
settings android studio only standard fonts. How do I specify fonts in the file not to write extra code?

Create Custom TextView with your custom font.
Note: It's important that you place required font files in the assets/fonts directory.
public class CustomTextView extends TextView {
public CustomTextView(Context context) {
super(context);
setFont();
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setFont();
}
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setFont();
}
private void setFont() {
Typeface font = Typeface.createFromAsset(getContext().getAssets(),"fonts/ss-symbolicons-line.ttf");
setTypeface(font, Typeface.NORMAL);
}
}

Related

Font awesome text is not solid in Android

I am trying to show hand pointer icon which is https://fontawesome.com/icons/hand-pointer?style=regular I want to show it solidly(white background) like this one https://fontawesome.com/icons/hand-pointer?style=solid .
But here I have a problem that both of them has the same unicode which is f25a.
//This is the FontAwesomeTextView I Created
FontAwesomeTextView fingerPointer = new FontAwesomeTextView(MyApplication.getStaticContext());
fingerPointer.setText(MyApplication.getStaticContext().getString(R.string.finger_pointer));
fingerPointer.setTextColor(ContextCompat.getColor(MyApplication.getStaticContext(), R.color.colorWhite));
fingerPointer.setShadowLayer(2,2,2,R.color.colorOrange);
fingerPointer.setTextSize(30f);
fingerPointer.setVisibility(View.GONE);
this.fingerPointer = fingerPointer;
//This is the hand pointer text that I am using
<string name="finger_pointer" translatable="false"></string>
As you can see icon i send in link has the same unicode in string.xml.
This is my FontAwesomeTextView which is pretty simple class that loads fontawesome typeface and set it.
public class FontAwesomeTextView extends AppCompatTextView {
public FontAwesomeTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public FontAwesomeTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public FontAwesomeTextView(Context context) {
super(context);
init();
}
private void init() {
Typeface typeface = FontCache.get(FontCache.FONT_AWESOME,getContext());
setTypeface(typeface);
}
}

retrieve custom char with charcode android

I'm using a custom font with characters like star, sun, etc.
I know their charcode into this font.
I set the TextView font with the following code:
public class MyTextView extends TextView {
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyTextView(Context context) {
super(context);
init();
}
public void init() {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "font/myFont.ttf");
setTypeface(tf ,1);
}}
is there a way, since I know their charcode, to use those specific characters directly in strings.xml and programmatically?
You can reference characters by their 4 digit hexadecimal code in java :
String someString = "\u2776"
Or in resources :
<string name="some_string_name">\u2776</string>
(2776 is the code for ❶ in the default font)

Change font of entire application [duplicate]

This question already has answers here:
How to set default font family for entire Android app
(16 answers)
Custom Fonts and Custom Textview on Android
(3 answers)
Closed 8 years ago.
I want to use custom font in my application. Which is the best way to give font to entire application. I know how to assign custom font to a single TextView or Button.
Is it possible to mention the custom font in one place, for eg. in styles.xml, and the font will be applied to whole application (every TextView, Button, EditText and soon).
use this type of textview in your app
public class MyTextView extends TextView {
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyTextView(Context context) {
super(context);
init();
}
public void init() {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "font/chiller.ttf");
setTypeface(tf ,1);
}
}
also edit text
public class CEditText extends EditText {
private Context context;
private AttributeSet attrs;
private int defStyle;
public CEditText(Context context) {
super(context);
this.context=context;
init();
}
public CEditText(Context context, AttributeSet attrs) {
super(context, attrs);
this.context=context;
this.attrs=attrs;
init();
}
public CEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.context=context;
this.attrs=attrs;
this.defStyle=defStyle;
init();
}
private void init() {
Typeface font=Typeface.createFromAsset(getContext().getAssets(), "fonts/myfont.ttf");
this.setTypeface(font);
}
#Override
public void setTypeface(Typeface tf, int style) {
tf=Typeface.createFromAsset(getContext().getAssets(), "fonts/myfont.ttf");
super.setTypeface(tf, style);
}
#Override
public void setTypeface(Typeface tf) {
tf=Typeface.createFromAsset(getContext().getAssets(), "fonts/myfont.ttf");
super.setTypeface(tf);
}
Unfortunatly, android is not providing any method to apply custom font for the complete app in one place.
However, you can create your CustomTextView extends TextView and CustomButton extends Button. In that you can set the font by creating a FontInstance.

RuntimeException: native typeface cannot be made or memory leak for custom TextView loading font

There's a HUGE problem in my code wherein I am loading a font in my assets\fonts\ folder from a custom TextView class. The first problem is that it crashes on 4.0 devices with the exception Caused by: java.lang.RuntimeException: native typeface cannot be made. I was using the same process here with the method:
public class MyTextView extends TextView {
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyTextView(Context context) {
super(context);
}
public void setTypeface(Typeface tf, int style) {
if (style == Typeface.BOLD) {
super.setTypeface(Typeface.createFromAsset(
getContext().getAssets(), "fonts/hirakakupronbold.ttf"));
} else if (style == Typeface.ITALIC) {
super.setTypeface(Typeface.createFromAsset(
getContext().getAssets(), "fonts/hirakakupronitalic.ttf"));
} else {
super.setTypeface(Typeface.createFromAsset(
getContext().getAssets(), "fonts/hirakakupron.ttf"));
}
}
}
Notice that I'm using the extension .ttf, and I found that this is causing the RunTimeException. So I converted the respective fonts with a .otf extensions, and now it runs already in 4.0 devices but has memory leaks basing here. There are workarounds here but I don't know how to use/call it. Any help would do, thank you.
Okay, so I finally figured that instantiating a TypeFace object inside a TextView class would cause so much load each time that same TextView is instantiated. This caused my app to lag and resulted to OutOfMemoryException eventually. So what I did was to create a different custom TypeFace class that would call my fonts from the assets so that it instantiates from the TypeFace class and not from the TextView class.
Here's my TypeFaces class:
public class TypeFaces {
private static final Hashtable<String, Typeface> cache = new Hashtable<String, Typeface>();
public static Typeface getTypeFace(Context context, String assetPath) {
synchronized (cache) {
if (!cache.containsKey(assetPath)) {
try {
Typeface typeFace = Typeface.createFromAsset(
context.getAssets(), assetPath);
cache.put(assetPath, typeFace);
} catch (Exception e) {
Log.e("TypeFaces", "Typeface not loaded.");
return null;
}
}
return cache.get(assetPath);
}
}
}
And the custom TextView class:
public class TextViewHirakaku extends TextView {
public TextViewHirakaku(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public TextViewHirakaku(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TextViewHirakaku(Context context) {
super(context);
}
public void setTypeface(Typeface tf, int style) {
if (style == Typeface.BOLD) {
super.setTypeface(TypeFaces.getTypeFace(getContext(),
"fonts/hirakakupronbold.ttf"));
} else if (style == Typeface.ITALIC) {
super.setTypeface(TypeFaces.getTypeFace(getContext(),
"fonts/hirakakupronitalic.ttf"));
} else {
super.setTypeface(TypeFaces.getTypeFace(getContext(),
"fonts/hirakakupron.ttf"));
}
}
}
Notice that I'm now calling getTypeFace method from TypeFaces class here.
If you're having this issue on Android Studio then put your assets under main directory instead of putting it under res directory.
Also in font naming use lowercase letters and underscores only, e.g. my_font.ttf
That worked like charm for me
If you are extending this view from xml then try using it this way::
public class MyTextView extends TextView {
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyTextView(Context context) {
super(context);
init();
}
public void init() {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/hirakakupronbold.ttf");
setTypeface(tf);
}
}
Its working fine for me. Make separate class extending TextView for each typeface style.to To apply it, replace your "TextView" with "com.yourpackage.MyTextView"
Regards,
In my case, the XML namespace prefix that I was using for my custom view (costum) wasn't set right:
xmlns:costum="http://schemas.android.com/apk/tools"
All I had to do is to change it to
xmlns:costum="http://schemas.android.com/apk/res-auto"
& it worked.

Where to Set Custom Typeface in Custom View Class (extends Button)?

I have a custom View class that extends Button. I'd like to set a custom Typeface to all instances of this class, but since some methods get called multiple times in a view, I was wondering what is the best overwriting method to place the following code in?
final Typeface face = Typeface.createFromAsset(getContext().getAssets(),
"myfont.ttf");
this.setTypeface(face);
Currently, I have it in onMeasure(), but I noticed it gets called multiple times, which I assume won't be good for performance.
Most correct is to add your code to constructor.
public class ButtonPlus extends Button {
public ButtonPlus(Context context) {
super(context);
}
public ButtonPlus(Context context, AttributeSet attrs) {
super(context, attrs);
setCustomFont(context, attrs);
}
public ButtonPlus(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setCustomFont(context, attrs);
}
private void setCustomFont(Context ctx, AttributeSet attrs) {
TypedArray a = ctx.obtainStyledAttributes(attrs,
R.styleable.TextViewPlus);
String customFont = a.getString(R.styleable.TextViewPlus_customFont);
setCustomFont(ctx, customFont);
a.recycle();
}
public boolean setCustomFont(Context ctx, String asset) {
Typeface tf = null;
try {
tf = Typeface.createFromAsset(ctx.getAssets(), asset);
} catch (Exception e) {
return false;
}
setTypeface(tf);
return true;
}
}
here customFont - is my custom attribute. i specify font from layout using this attr. I just created following custom attr in values/attrs.xml

Categories

Resources