Monday 9 March 2015

Android applying custom fonts

Hello Friends,
      Today, I am sharing my small blog which helps you in applying custom fonts
on android text view from xml or styles file. Its very easy and reduces the number
of java line from your code and also make the performance of your app more better.
                                     


Steps:
1.Placed all your custom fonts file inside assets folder i.e: assets/fonts/digitalism.ttf)
2.Add the path of above custom font file your string.xml file
<string name="FONT_d3_digitalism">fonts/d3-digitalism.ttf</string>
<string name="FONT_digitalism">fonts/digitalism.ttf</string>
<string name="FONT_Mohave_Bold">fonts/Mohave-Bold.ttf</string>
<string name="FONT_Roboto_Black">fonts/Roboto-Black.ttf</string>
<string name="FONT_Roboto_Bold">fonts/Roboto-Bold.ttf</string>
<string name="FONT_Roboto_Medium">fonts/Roboto-Medium.ttf</string>
<string name="FONT_RobotoCondensed_Bold">fonts/RobotoCondensed-Bold.ttf</string>

3. Declartion of style inside values/styles.xml file for eg:
        <style name="CustomDigital">
          <item name="font">@string/FONT_digitalism</item>
        </style>

4. Inside attrs.xml add this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
      <declare-styleable name="Fontify">
<attr name="font" format="string" />
      </declare-styleable>

   <!-- Allows attribute use in an AppTheme definition -->
   <declare-styleable name="FontifyTheme">
<attr name="fontifyStyle" format="reference"/>
   </declare-styleable>
</resources>
5. finally, In our layout file we have to use this :
      <com.android.developer.solutions.font.TextClock
         android:id="@+id/textClock1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignLeft="@+id/btn"
         android:layout_alignRight="@+id/btn"
         android:layout_below="@+id/btn"
         android:layout_marginTop="20dp"
         android:gravity="center"
         android:textSize="18sp"
         style="@style/CustomDigital"
         android:text="TextClock" />


For more check below link:
https://github.com/danh32/Fontify


Download Demo Code : CustomTextFont

Hope this will help some one.
Enjoy Coding :)

Mukesh Kumar

Hi Guys I am from Delhi working as Web/Mobile Application Developer(Android Developer), also have knowledge of Roboelctric and Mockito ,android test driven development... Blogging has been my passion and I think blogging is one of the powerful medium to share knowledge and ideas....

0 comments:

Post a Comment

 

Copyright @ 2013 Android Developers Blog.