Thursday, July 13, 2017

android - Custom font resource in java code

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ecf5f5"
    android:padding="16dp"
    android:orientation="vertical"
    >
    <TextView
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello Android APP Development!"
        android:textSize="40sp"
        android:textColor="#e92121"
        />
    <TextView
        android:id="@+id/text_view2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This text display a custom
         font using font resource in Java code backward compatible."
        android:textSize="30sp"
        android:textColor="#28601e"
        />
</LinearLayout>
MainActivity.java


package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.support.v4.content.res.ResourcesCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {
    private Context mContext;
    private Activity mActivity;

    private TextView mTextView;
    private TextView mTextView2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the application context
        mContext = getApplicationContext();
        mActivity = MainActivity.this;

        // Get the widget reference from xml layout
        mTextView = (TextView) findViewById(R.id.text_view);
        mTextView2 = (TextView) findViewById(R.id.text_view2);

        /*
            ResourcesCompat
                Helper for accessing features in Resources introduced after API
                level 4 in a backwards compatible fashion.
        */

        /*
            getFont
                Typeface getFont (Context context, int id)

                Returns a font Typeface associated with a particular resource ID.

                Prior to API level 23, font resources with more than one font in
                a family will only load the first font in that family.

                Parameters
                    context Context : A context to retrieve the Resources from.
                    id int : The desired resource identifier of a Typeface, as generated by the
                              aapt tool. This integer encodes the package, type, and resource entry.
                              The value 0 is an invalid identifier.

                Returns
                    Typeface : A font Typeface object.

                Throws
                    Resources.NotFoundException Throws NotFoundException if the given ID does not exist.

        */

        // Get the font from front resource
        Typeface fresca = ResourcesCompat.getFont(mContext,R.font.fresca_regular);
        Typeface gloria = ResourcesCompat.getFont(mContext,R.font.gloria_hallelujah);

        // Set text text views typefaces
        mTextView.setTypeface(fresca);
        mTextView2.setTypeface(gloria);
    }
}