Skip to content

Instantly share code, notes, and snippets.

@arthtilva
Created November 28, 2018 10:33
Show Gist options
  • Save arthtilva/f7571fc2d9352263771057dfa20f4178 to your computer and use it in GitHub Desktop.
Save arthtilva/f7571fc2d9352263771057dfa20f4178 to your computer and use it in GitHub Desktop.
Google Fonts Online
package com.example.android.downloadablefonts;
import android.app.Application;
import android.graphics.Typeface;
import android.os.Handler;
import android.os.HandlerThread;
import android.support.v4.provider.FontRequest;
import android.support.v4.provider.FontsContractCompat;
import android.util.Log;
import android.widget.Toast;
public class MainApplication extends Application {
Typeface regular, bold;
@Override
public void onCreate() {
super.onCreate();
requestDownload("Montserrat");
requestBoldDownload("Montserrat");
}
private void requestDownload(String familyName) {
QueryBuilder queryBuilder = new QueryBuilder(familyName)
.withWidth(1)
.withWeight(500)
.withItalic(0)
.withBestEffort(true);
String query = queryBuilder.build();
Log.d("TAG", "Requesting a font. Query: " + query);
FontRequest request = new FontRequest(
"com.google.android.gms.fonts",
"com.google.android.gms",
query,
R.array.com_google_android_gms_fonts_certs);
FontsContractCompat.FontRequestCallback callback = new FontsContractCompat
.FontRequestCallback() {
@Override
public void onTypefaceRetrieved(Typeface typeface) {
Log.i("TAG", "onTypefaceRetrieved:1 ");
regular = typeface;
}
@Override
public void onTypefaceRequestFailed(int reason) {
Toast.makeText(MainApplication.this,
getString(R.string.request_failed, reason), Toast.LENGTH_LONG)
.show();
}
};
FontsContractCompat
.requestFont(MainApplication.this, request, callback,
getHandlerThreadHandler());
}
private void requestBoldDownload(String familyName) {
QueryBuilder queryBuilder = new QueryBuilder(familyName)
.withWidth(1)
.withWeight(999)
.withItalic(0)
.withBestEffort(true);
String query = queryBuilder.build();
Log.d("TAG", "Requesting a font. Query: " + query);
FontRequest request = new FontRequest(
"com.google.android.gms.fonts",
"com.google.android.gms",
query,
R.array.com_google_android_gms_fonts_certs);
FontsContractCompat.FontRequestCallback callback = new FontsContractCompat
.FontRequestCallback() {
@Override
public void onTypefaceRetrieved(Typeface typeface) {
Log.i("TAG", "onTypefaceRetrieved:2 ");
bold = typeface;
}
@Override
public void onTypefaceRequestFailed(int reason) {
Toast.makeText(MainApplication.this,
getString(R.string.request_failed, reason), Toast.LENGTH_LONG)
.show();
}
};
FontsContractCompat
.requestFont(MainApplication.this, request, callback,
getHandlerThreadHandler());
}
private Handler mHandler = null;
private Handler getHandlerThreadHandler() {
if (mHandler == null) {
HandlerThread handlerThread = new HandlerThread("fonts");
handlerThread.start();
mHandler = new Handler(handlerThread.getLooper());
}
return mHandler;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment