Skip to content

Instantly share code, notes, and snippets.

@davebren
Created February 4, 2015 19:09
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save davebren/c95ea1ec7f9bb9872853 to your computer and use it in GitHub Desktop.
Save davebren/c95ea1ec7f9bb9872853 to your computer and use it in GitHub Desktop.
Material Design - Android Typography
package com.eski.android_utils.view.typeface;
import android.graphics.Typeface;
import dagger.DaggerApplication;
public class Typefaces {
private static Typeface robotoRegular;
private static Typeface robotoLight;
private static Typeface robotoMedium;
public static Typeface robotoRegular() {
if (robotoRegular == null) robotoRegular = loadTypeface("Roboto-Regular");
return robotoRegular;
}
public static Typeface robotoMedium() {
if (robotoMedium == null) robotoMedium = loadTypeface("Roboto-Medium");
return robotoMedium;
}
public static Typeface robotoLight() {
if (robotoLight == null) robotoLight = loadTypeface("Roboto-Light");
return robotoLight;
}
private static Typeface loadTypeface(String name) {
return Typeface.createFromAsset(DaggerApplication.assets(), "fonts/" + name + ".ttf");
}
}
package com.eski.android_utils.view.typeface;
import android.graphics.Paint;
import dagger.DaggerApplication;
public enum TypographyStyle {
Display1, Display2, Display3, Display4,
Headline,
Title,
Subhead,
Body1, Body2,
Caption,
Button;
public void setStyle(Paint paint) {
switch (this) {
case Display1: case Display2: case Display3: case Headline: case Subhead: case Body1: case Caption:
paint.setTypeface(Typefaces.robotoRegular()); break;
case Title: case Body2: case Button:
paint.setTypeface(Typefaces.robotoMedium()); break;
case Display4:
paint.setTypeface(Typefaces.robotoLight()); break;
}
switch(this) {
case Display1: paint.setTextSize(getDimension(R.dimen.text_display1)); break;
case Display2: paint.setTextSize(getDimension(R.dimen.text_display2)); break;
case Display3: paint.setTextSize(getDimension(R.dimen.text_display3)); break;
case Display4: paint.setTextSize(getDimension(R.dimen.text_display4)); break;
case Headline: paint.setTextSize(getDimension(R.dimen.text_headline)); break;
case Subhead: paint.setTextSize(getDimension(R.dimen.text_subhead)); break;
case Caption: paint.setTextSize(getDimension(R.dimen.text_caption)); break;
case Title: paint.setTextSize(getDimension(R.dimen.text_title)); break;
case Body1: paint.setTextSize(getDimension(R.dimen.text_body1)); break;
case Body2: paint.setTextSize(getDimension(R.dimen.text_body2)); break;
case Button: paint.setTextSize(getDimension(R.dimen.text_button)); break;
}
}
private float getDimension(int id) {return DaggerApplication.app.getResources().getDimensionPixelSize(id);}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment