Thursday, November 26, 2015

android - How to use custom font in TextView

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    tools:context=".MainActivity"
    android:background="#dee2db"
    >
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Custom Font"
        android:textSize="50dp"
        />
    <TextView
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Another Font"
        android:textSize="75dp"
        android:layout_below="@id/tv"
        />
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Apply Custom Font"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        />
</RelativeLayout>
MainActivity.java

package com.cfsuman.me.androidcode;

import android.content.Context;
import android.content.res.Resources;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.graphics.Typeface;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {
    private Context mContext;
    private Resources mResources;
    private RelativeLayout mRelativeLayout;
    private Button mButton;
    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();

        // Get the Resources
        mResources = getResources();

        // Get the widgets reference from XML layout
        mRelativeLayout = (RelativeLayout) findViewById(R.id.rl);
        mButton = (Button) findViewById(R.id.btn);
        mTextView = (TextView) findViewById(R.id.tv);
        mTextView2 = (TextView) findViewById(R.id.tv2);

        // Set a click listener for Button widget
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                /*
                    Typeface
                        The Typeface class specifies the typeface and intrinsic style of a font.
                        This is used in the paint, along with optionally Paint settings like textSize,
                        textSkewX, textScaleX to specify how text appears when drawn (and measured).
                */
                /*
                    public static Typeface createFromAsset (AssetManager mgr, String path)
                        Create a new typeface from the specified font data.

                    Parameters
                        mgr : The application's asset manager
                        path : The file name of the font data in the assets directory
                    Returns
                        The new typeface.
                */
                /*
                    AssetManager
                        Provides access to an application's raw asset files; see Resources for the
                        way most applications will want to retrieve their resource data. This class
                        presents a lower-level API that allows you to open and read raw files that
                        have been bundled with the application as a simple stream of bytes.
                */

                // Initialize a new instance of Typeface object
                Typeface typeface = Typeface.createFromAsset(mContext.getAssets(),"fonts/SEASRN__.ttf");
                /*
                    Context
                        Interface to global information about an application environment. This is an
                        abstract class whose implementation is provided by the Android system. It
                        allows access to application-specific resources and classes, as well as
                        up-calls for application-level operations such as launching activities,
                        broadcasting and receiving intents, etc.
                */
                /*
                    getAssets()
                        Return an AssetManager instance for your application's package.
                */

                // Initialize another instance of Typeface object
                Typeface typeface2 = Typeface.createFromAsset(mContext.getAssets(),"fonts/FFF_Tusj.ttf");

                // Apply the custom font to TextView
                mTextView.setTypeface(typeface);
                mTextView2.setTypeface(typeface2);

                /*
                    setTypeface(Typeface tf, int style)
                        Sets the typeface and style in which the text should be displayed, and turns
                        on the fake bold and italic bits in the Paint if the Typeface that you
                        provided does not have all the bits in the style that you specified.
                */
                /*
                    public Typeface getTypeface ()
                        Returns
                            the current typeface and style in which the text is being displayed.
                */
                // You can also display the custom font as bold or italic style
                //mTextView.setTypeface(mTextView.getTypeface(),Typeface.ITALIC);
                //mTextView2.setTypeface(mTextView2.getTypeface(),Typeface.BOLD);
          }
        });
    }
}
More android examples