is there a way to change the fonts of all textviews in a layout?
currently im using this to change fonts manually.
TextView txtAppName = (TextView) f
Users can change the default font in the settings, and it sticks with all applications, so if you will find a way to set this settings for the user you will change his font in all of his applications.
My advice would be to create an extrication for TextView and set your font only once in that Class.
Perhaps a little late for you, but for other Android users this may be very useful.
Unfortunately, Android doesn't provide the quick, easy and clean way you're looking for to change the font for your entire app. But recently I've looked into this matter and created some tools that allow you to change the font without any coding (you can do it all through xml, styles and even text appearances). You can read all about it on this blog, and see the github project here.
Here's an example of how to apply these tools. Put all your font files in assets/fonts/
. Then, declare those fonts in an xml file and load this file early in your app with TypefaceManager.initialize(this, R.xml.fonts);
(e.g., in the onCreate of your Application class). The xml file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<familyset>
<!-- Some Font. Can be referenced with 'someFont' or 'aspergit' -->
<family>
<nameset>
<name>aspergit</name>
<name>someFont</name>
</nameset>
<fileset>
<file>Aspergit.ttf</file>
<file>Aspergit Bold.ttf</file>
<file>Aspergit Italic.ttf</file>
<file>Aspergit Bold Italic.ttf</file>
</fileset>
</family>
<!-- Another Font. Can be referenced with 'anotherFont' or 'bodoni' -->
<family>
<nameset>
<name>bodoni</name>
<name>anotherFont</name>
</nameset>
<fileset>
<file>BodoniFLF-Roman.ttf</file>
<file>BodoniFLF-Bold.ttf</file>
</fileset>
</family>
</familyset>
Now you can use them in your style or xml with the flFont attribute. Here's how to apply the font to all texts in your entire app:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools">
<!-- Application theme -->
<!-- Use a different parent if you don't want Holo Light -->
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<item name="android:textViewStyle">@style/MyTextViewStyle</item>
</style>
<!-- Style to use for ALL text views (including FontTextView) -->
<!-- Use a different parent if you don't want Holo Light -->
<style name="MyTextViewStyle" parent="@android:style/Widget.Holo.Light.TextView">
<item name="android:textAppearance">@style/MyTextAppearance</item>
</style>
<!-- Text appearance to use for ALL text views (including FontTextView) -->
<!-- Use a different parent if you don't want Holo Light -->
<style name="MyTextAppearance" parent="@android:style/TextAppearance.Holo">
<!-- Alternatively, reference this font with the name "aspergit" -->
<!-- Note that only our own TextView's will use the font attribute -->
<item name="flFont">someFont</item>
<item name="android:textStyle">bold|italic</item>
</style>
</resources>
Now you can make an layout.xml looks like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<com.innovattic.font.FontTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This uses my font in bold italic style" />
<com.innovattic.font.FontTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:flFont="anotherFont"
android:textStyle="normal"
android:text="This uses another font in normal style" />
</LinearLayout>
Don't forget to apply the theme in your Android manifest.
All system default fonts you can get are in:
android/res/values/styles.xml
You can change them only through system applications such as Settings. Maybe you can call some system function that does it. But it seems inprobable, as that would cause problems to all other aplications.
If you have your own custom font, you should add it to assests->fonts folder, which can be created as follows:
Then create fonts folder & paste your "ttf" file there.
Then it's needed to iterate over all TextViews & set font (my is "fonts/Qlassik_TB.ttf") programatically like that:
@Override
protected void onCreate(Bundle savedInstanceState) {
// Your stuff here...
Typeface tf = Typeface.createFromAsset(this.getAssets(), "fonts/Qlassik_TB.ttf");
ViewGroup myMostParentLayout = (ViewGroup) findViewById(R.id.frame_layout_most_parent);
setFontToAllChilds(myMostParentLayout, tf);
}
private void setFontToAllChilds(ViewGroup myMostParentLayout, Typeface tf) {
int childCount = myMostParentLayout.getChildCount();
for (int i = 0; i < childCount; ++i) {
View child = myMostParentLayout.getChildAt(i);
if (child instanceof ViewGroup)
setFontToAllChilds((ViewGroup) child, tf);
else if (child instanceof TextView)
((TextView) child).setTypeface(tf);
}
And finally add an id to your most parent WhateverLayout:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/frame_layout_most_parent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
which is used here:
ViewGroup myMostParentLayout = (ViewGroup) findViewById(R.id.frame_layout_most_parent);
You can make your own View that will set you font and use it
public class MyTextView extends TextView {
public MyTextView(Context context) {
super(context, attrs, defStyle);
setFont(context);
}
private void setFont(Context context) {
Typeface font = Typeface.createFromAsset(context.getAssets(), YOUR_FONT);
setTypeface(font);
}
}