Saturday, July 15, 2017

android - Font family resource file in support library

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="#d4d3ca"
    android:padding="16dp"
    android:orientation="vertical"
    >
    <TextView
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Regular font"
        android:textSize="60sp"
        android:textColor="#e92121"
        android:fontFamily="@font/custom_font_family"
        android:textStyle="normal"
        />
    <TextView
        android:id="@+id/text_view2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Bold font"
        android:textSize="60sp"
        android:textColor="#1679ca"
        android:fontFamily="@font/custom_font_family"
        android:textStyle="bold"
        />
    <TextView
        android:id="@+id/text_view3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Italic font"
        android:textSize="60sp"
        android:textColor="#48792e"
        android:fontFamily="@font/custom_font_family"
        android:textStyle="italic"
        />
    <TextView
        android:id="@+id/text_view4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Bold italic font"
        android:textSize="60sp"
        android:textColor="#ab1ca3"
        android:fontFamily="@font/custom_font_family"
        android:textStyle="bold|italic"
        />
</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;
    private TextView mTextView3;
    private TextView mTextView4;

    @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);
        mTextView3 = (TextView) findViewById(R.id.text_view3);
        mTextView4 = (TextView) findViewById(R.id.text_view4);
    }
}
res/font/custom_font_family.xml

<?xml version="1.0" encoding="utf-8"?>
<font-family
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    >
    <font
        android:font="@font/ubuntu_mono_regular"
        android:fontStyle="normal"
        android:fontWeight="400"
        app:font="@font/ubuntu_mono_regular"
        app:fontStyle="normal"
        app:fontWeight="400"
        />
    <font
        android:font="@font/ubuntu_mono_bold"
        android:fontStyle="normal"
        android:fontWeight="700"
        app:font="@font/ubuntu_mono_bold"
        app:fontStyle="normal"
        app:fontWeight="700"
        />
    <font
        android:font="@font/ubuntu_mono_italic"
        android:fontStyle="italic"
        android:fontWeight="400"
        app:font="@font/ubuntu_mono_italic"
        app:fontStyle="italic"
        app:fontWeight="400"
        />
    <font
        android:font="@font/ubuntu_mono_bold_italic"
        android:fontStyle="italic"
        android:fontWeight="700"
        app:font="@font/ubuntu_mono_bold_italic"
        app:fontStyle="italic"
        app:fontWeight="700"
        />
</font-family>