Thursday, July 13, 2017

android - Custom font resource in XML layout file

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="#edf1e9"
    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:fontFamily="@font/gloria_hallelujah"
        />
    <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 xml layout file"
        android:textSize="30sp"
        android:fontFamily="@font/fresca_regular"
        android:textColor="#5064bf"
        />
</LinearLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.Context;
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);
    }
}