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.
Related
Everyone
I want to apply my custom font to whole application not just a textview or not for an activity. if it is possible make just change in manifest file so it is easy for me.
so help me out.
You can create custom textview class and use that class whenever you need to use custom fonts.
Check this link: How to make a custom TextView?
There's also a library that does this. Very simple to use and set up.
https://github.com/chrisjenx/Calligraphy
Try this method for all activity. Or you can make this method in you utility class and use in you activity class see bellow.
Method in your utility.java file.
public static void setFont(ViewGroup group, Typeface font) {
int count = group.getChildCount();
View v;
for (int i = 0; i < count; i++) {
v = group.getChildAt(i);
if (v instanceof TextView || v instanceof EditText
|| v instanceof Button) {
((TextView) v).setTypeface(font);
} else if (v instanceof ViewGroup)
setFont((ViewGroup) v, font);
}
}
And then in your Activity.java file.
ll_Homescreen = (LinearLayout) findViewById(R.id.ll_Homescreen);
FontStyle = Typeface.createFromAsset(getAssets(), path of you font);
utility.setFont(ll_Homescreen, FontStyle);
I hope this will help you Happy Coding.
Is there an easy way to make all textviews (and any other text elements in my app) to use a custom font of my own (and not the built-in choices), without having to manually set them via textview.setTypeface()?
I guess extending Textview would do the trick but then building interfaces with the visual editor is kind of a pain. I was thinking of something like styling but can't find how to set a custom font there.
If you need to set one font for all TextViews in android application you can use this solution. It will override ALL TextView's typefaces, includes action bar and other standard components, but EditText's password font won't be overriden.
MyApp.java
public class MyApp extends Application {
#Override
public void onCreate()
{
TypefaceUtil.overrideFont(getApplicationContext(), "SERIF", "fonts/Roboto-Regular.ttf");
}
}
TypefaceUtil.java
public class TypefaceUtil {
/**
* Using reflection to override default typeface
* NOTICE: DO NOT FORGET TO SET TYPEFACE FOR APP THEME AS DEFAULT TYPEFACE WHICH WILL BE OVERRIDDEN
* #param context to work with assets
* #param defaultFontNameToOverride for example "monospace"
* #param customFontFileNameInAssets file name of the font from assets
*/
public static void overrideFont(Context context, String defaultFontNameToOverride, String customFontFileNameInAssets) {
try {
final Typeface customFontTypeface = Typeface.createFromAsset(context.getAssets(), customFontFileNameInAssets);
final Field defaultFontTypefaceField = Typeface.class.getDeclaredField(defaultFontNameToOverride);
defaultFontTypefaceField.setAccessible(true);
defaultFontTypefaceField.set(null, customFontTypeface);
} catch (Exception e) {
Log.e("TypefaceUtil","Can not set custom font " + customFontFileNameInAssets + " instead of " + defaultFontNameToOverride);
}
}
}
themes.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyAppTheme" parent="#android:style/Theme.Holo.Light">
<!-- you should set typeface which you want to override with TypefaceUtil -->
<item name="android:typeface">serif</item>
</style>
</resources>
Update for Android 5.0 or greater
As i have investigated and tested on device running api 5.0 or greater this solution is working fine because i am using the single style.xml file and not making other style.xml in values-21 folder
Although
Some users asking me that this solution not working with android 5.0 device (they may be using values-21 style.xml i guess)
So that is because Under API 21, most of the text styles include fontFamily setting, like
<item name="fontFamily">#string/font_family_body_1_material</item>
Which applys the default Roboto Regular font:
<string name="font_family_body_1_material">sans-serif</string>
So the best solution is
If Any one having problem not working for 5.0+. Don't override typeface in your values-v21 styles.
Just override font in values/style.xml and you will good to go :)
You can create a method to set typeface for a textview in a common class and you can set the call that method and send the textview as its attribute.
Yes. Just make a style and set it to a certain font and then set the entire app in that style.
You can do it by creating subclass of TextView and call setTypeFace method within it. For example in constructor.
Make a method in Constants class so that it can be accessed from all fragments and activities:
public static void overrideFonts(final Context context, final View v) {
try {
if (v instanceof ViewGroup) {
ViewGroup vg = (ViewGroup) v;
for (int i = 0; i < vg.getChildCount(); i++) {
View child = vg.getChildAt(i);
overrideFonts(context, child);
}
} else if (v instanceof TextView) {
((TextView) v).setTypeface(Typeface.createFromAsset(context.getAssets(), "font/Lato-Regular.ttf"));
} else if (v instanceof EditText) {
((EditText) v).setTypeface(Typeface.createFromAsset(context.getAssets(), "font/Lato-Regular.ttf"));
} else if (v instanceof Button) {
((Button) v).setTypeface(Typeface.createFromAsset(context.getAssets(), "font/Lato-Regular.ttf"));
}
} catch (Exception e) {
}
}
Call Method in activity as:
Constants.overrideFonts(MainActivity.this, getWindow().getDecorView().getRootView());
Call method in Fragment as:
Constants.overrideFonts(getActivity(), view);
I'm trying to apply a custom font style for my app..I know:
Typeface tf = Typeface.createFromAsset(getAssets(),"font.ttf");
text.setTypeface(tf);
But I want to apply custom font style for the entire app including the action bar tabs and all the text view.
is there a "supper" way to apply a custom font for the whole app??
You cannot set a custom font for the whole application in a single point yet, but you have to set the typeface for every view by yourself.
See this answer: Android: Want to set custom fonts for whole application not runtime
Custom ActionBar Styling
ActionBar Tabs
ActionBar Title
There is no way to set an entire applications typeface, but if you are looking for a more general programatic solution, I created a static class that can be used to set the Typeface of an entire view (Activity UI). Note that I am working with Mono (C#) but you can implement it easily using Java.
You can pass this class a layout or a specific view that you want to customize. If you want to be super efficient you could implement it using the Singleton pattern.
public static class AndroidTypefaceUtility
{
static AndroidTypefaceUtility()
{
}
//Refer to the code block beneath this one, to see how to create a typeface.
public static void SetTypefaceOfView(View view, Typeface customTypeface)
{
if (customTypeface != null && view != null)
{
try
{
if (view is TextView)
(view as TextView).Typeface = customTypeface;
else if (view is Button)
(view as Button).Typeface = customTypeface;
else if (view is EditText)
(view as EditText).Typeface = customTypeface;
else if (view is ViewGroup)
SetTypefaceOfViewGroup((view as ViewGroup), customTypeface);
else
Console.Error.WriteLine("AndroidTypefaceUtility: {0} is type of {1} and does not have a typeface property", view.Id, typeof(View));
}
catch (Exception ex)
{
Console.Error.WriteLine("AndroidTypefaceUtility threw:\n{0}\n{1}", ex.GetType(), ex.StackTrace);
throw ex;
}
}
else
{
Console.Error.WriteLine("AndroidTypefaceUtility: customTypeface / view parameter should not be null");
}
}
public static void SetTypefaceOfViewGroup(ViewGroup layout, Typeface customTypeface)
{
if (customTypeface != null && layout != null)
{
for (int i = 0; i < layout.ChildCount; i++)
{
SetTypefaceOfView(layout.GetChildAt(i), customTypeface);
}
}
else
{
Console.Error.WriteLine("AndroidTypefaceUtility: customTypeface / layout parameter should not be null");
}
}
}
In your activity you will need to create a Typeface object. I create mine in the OnCreate() using a .ttf file placed in my Resources/Assets/ directory. Make sure that the file is marked as an Android Asset in its' properties.
protected override void OnCreate(Bundle bundle)
{
...
LinearLayout rootLayout = (LinearLayout)FindViewById<LinearLayout>(Resource.Id.signInView_LinearLayout);
Typeface allerTypeface = Typeface.CreateFromAsset(base.Assets,"Aller_Rg.ttf");
AndroidTypefaceUtility.SetTypefaceOfViewGroup(rootLayout, allerTypeface);
}
In my project I am using the font android: fontFamily = "sans-serif-light", and working properly.
I am also using the library viewpagerindicator. I want to use the font android: fontFamily = "sans-serif-light" also in the viewpagerindicator, but can not find how to do it
I've tried using android:fontFamily = "sans-serif-light" in <com.viewpagerindicator.TitlePageIndicator ... and in style, but without success.
I have also tried:
PageIndicator mIndicator = (TitlePageIndicator) findViewById (R.id.indicator);
Typeface myTypeface = Typeface.createFromAsset (getAssets (), "fonts / Roboto-Light.ttf");
mIndicator.setTypeface (myTypeface);
but this does not work ..
I appreciate any help.
Thanks and regards
If I don't get you wrong, you want to change titles font in view pager indicator,
I changed the library to achieve that,
for TabPageIndicator custome typeface I added this for TabPageIndicator.java
private Typeface mTypeface;
public void setTypeFace(Typeface tf) {
this.mTypeface = tf;
}
and then change addTab function to this:
private void addTab(int index, CharSequence text, int iconResId) {
final TabView tabView = new TabView(getContext());
tabView.mIndex = index;
tabView.setFocusable(true);
tabView.setOnClickListener(mTabClickListener);
tabView.setText(text);
**if (mTypeface != null) {
tabView.setTypeface(mTypeface);
}**
if (iconResId != 0) {
tabView.setCompoundDrawablesWithIntrinsicBounds(iconResId, 0, 0, 0);
}
mTabLayout.addView(tabView, new LinearLayout.LayoutParams(0, MATCH_PARENT, 1));
}
now you should just setTypeFace on your tabpagerindicator, like this:
mTabPageIndicator = (TabPageIndicator) findViewById(R.id.tab_page_indicator);
mTabPageIndicator.setTypeFace(Typeface.createFromAsset(getApplicationContext().getAssets(), "fonts/custome_font.ttf");
I'm using the ViewPageIndicator library, here's how I did it (index is the tab index in your view page indicator):
private void changeTabText(int index, boolean on){
if(tabLayout.getChildCount()-1 >= index){
TextView tv = (TextView)tabLayout.getChildAt(index);
tv.setTextColor(getResources().getColor(on? android.R.color.black : R.color.light_grey));
CustomFont.setTypeface(tv, CustomFont.NORMAL);
}
}
Here's how I got the tablayout:
tabLayout = (ViewGroup)indicator.getChildAt(0); //indicator is a horizontal scroll view, there will be only one root layout
Here's what my custom font does:
public static void setTypeface(TextView view, String font) {
Typeface typeface = Typeface.createFromAsset(view.getContext().getAssets(), BASE_PATH + font);
view.setTypeface(typeface);
}
A more OOD implementation is to modify the Library by creating a new interface:
public interface FontPagerAdapter {
/**
* Get the fonts to set.
*/
Typeface getCustomFont();}
And in class TabPageIndicator.java add a new property:
private Typeface customTypeFace;
which will be set in the notifyDataSetChanged() method by declaring:
if (adapter instanceof FontPagerAdapter) {
FontPagerAdapter fontAdapter = (FontPagerAdapter)adapter;
customTypeFace = fontAdapter.getCustomFont();
}
Later you would change the Font by setting it programatically in the addTab method, just by adding:
if (customTypeFace != null) {
tabView.setTypeface(customTypeFace);
}
Finally in the adapter that will use the library, you need to implement this interface, then override the method:
#Override
public Typeface getCustomFont() {
Typeface font = Typeface.createFromAsset(context.getAssets(),"fonts/PoetsenOne-Regular.ttf");
return font;
}
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
}
}
}