Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. package com.example.tutorial;
  2.  
  3. import android.annotation.TargetApi;
  4. import android.content.Context;
  5. import android.content.res.TypedArray;
  6. import android.graphics.Typeface;
  7. import android.os.Build;
  8. import android.util.AttributeSet;
  9. import android.util.Log;
  10. import android.widget.TextView;
  11.  
  12. import java.util.HashMap;
  13.  
  14. /**
  15.  * Created by Shiburagi on 28-Jan-15.
  16.  */
  17. public class CustomTextView extends TextView {
  18.     private static final String TAG = "TextView";
  19.     private final static HashMap<String, Typeface> fonts;
  20.  
  21.     static {
  22.         fonts = new HashMap<String, Typeface>();
  23.     }
  24.  
  25.     /**
  26.      * Default Constructor
  27.      * @param context
  28.      */
  29.     public CustomTextView(Context context) {
  30.         super(context);
  31.     }
  32.  
  33.     /**
  34.      * Overload Constructor
  35.      * @param context
  36.      * @param attrs
  37.      */
  38.     public CustomTextView(Context context, AttributeSet attrs) {
  39.         super(context, attrs);
  40.         setFont(context, attrs);
  41.     }
  42.  
  43.     /**
  44.      * Overload Constructor
  45.      * @param context
  46.      * @param attrs
  47.      * @param defStyleAttr
  48.      */
  49.     public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr) {
  50.         super(context, attrs, defStyleAttr);
  51.         setFont(context, attrs);
  52.     }
  53.  
  54.     /**
  55.      * Overload Constructor
  56.      * @param context
  57.      * @param attrs
  58.      * @param defStyleAttr
  59.      * @param defStyleRes
  60.      */
  61.     @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  62.     public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
  63.         super(context, attrs, defStyleAttr, defStyleRes);
  64.         setFont(context, attrs);
  65.     }
  66.  
  67.     /**
  68.      * set font according font attribute been defined in XML layout
  69.      * @param context
  70.      * @param attrs
  71.      */
  72.     private void setFont(Context context, AttributeSet attrs) {
  73.  
  74.         // get All attribute of CustomTextView in xml file
  75.         TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomTextView);
  76.  
  77.         // get font name from <something>:font attribute
  78.         String font = a.getString(R.styleable.CustomTextView_font);
  79.         setFont(context, font);
  80.  
  81.         a.recycle();
  82.     }
  83.  
  84.     /**
  85.      * set font for this TextView.
  86.      * @param context
  87.      * @param asset
  88.      * @return
  89.      */
  90.     public boolean setFont(Context context, String asset) {
  91.         Typeface typeface = null;
  92.         String directory="fonts/";
  93.         try {
  94.  
  95.             // if the font not exist yet, then create it and store in HashMap fonts
  96.             // otherwise, just call it back.
  97.             if(!fonts.containsKey(asset)){
  98.                 typeface = Typeface.createFromAsset(context.getAssets(),directory.concat(asset));
  99.                 fonts.put(asset,typeface);
  100.             }else{
  101.                 typeface = fonts.get(asset);
  102.             }
  103.  
  104.         } catch (Exception e) {
  105.             Log.e(TAG, "Could not get typeface: " + e.getMessage());
  106.             return false;
  107.         }
  108.  
  109.         // set custom font
  110.         setTypeface(typeface);
  111.         return true;
  112.     }
  113. }