Tuesday, November 25, 2014

Android Roboto in simple steps [without extending TextView class]

Using Roboto font in simple steps:

1. Download all roboto fonts in    /assets/fonts folder
      like /assets/fonts/Roboto-Black.ttf

2. Create fonts.xml file in  /res/xml folder with content

<?xml version="1.0" encoding="utf-8"?>  
<familyset>

    <!-- Font with all four styles defined -->
    <family>
        <nameset>
            <name>roboto</name>
        </nameset>
        <fileset>
            <file>Roboto-Black.ttf</file>
            <file>Roboto-BlackItalic.ttf</file>
            <file>Roboto-Bold.ttf</file>
            <file>Roboto-BoldItalic.ttf</file>
            <file>Roboto-Italic.ttf</file>
            <file>Roboto-Light.ttf</file>
            <file>Roboto-LightItalic.ttf</file>
            <file>Roboto-Medium.ttf</file>
            <file>Roboto-MediumItalic.ttf</file>
            <file>Roboto-Regular.ttf</file>
            <file>Roboto-Thin.ttf</file>
            <file>Roboto-ThinItalic.ttf</file>
        </fileset>
    </family>
</familyset> 

3. Create attrs.xml file in /res/values with content
<?xml version="1.0" encoding="utf-8"?>  
<resources>

    <declare-styleable name="Fonts">
        <attr name="font" format="string" />
        <attr name="android:textStyle" />
    </declare-styleable>

</resources>

4. Create theme for app in /res/values/styles.xml 

   <style name="AppBaseTheme" parent="android:Theme.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
        <item name="font">roboto</item>        
    </style>
      <style name="robotoTextAppearance" parent="@android:style/TextAppearance.Holo">
        <item name="font">aspergit</item>
      </style>

5. Use 
   //For whole app
    <application
        android:name=".KApp"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppBaseTheme" >

//For individual control
<TextView ... android:textAppearance="@style/robotoTextAppearance"/>
<
Enjoy!!!