Skip to content

Instantly share code, notes, and snippets.

@monmonja
Last active September 22, 2015 01:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save monmonja/117b428fd48c52324fd7 to your computer and use it in GitHub Desktop.
Save monmonja/117b428fd48c52324fd7 to your computer and use it in GitHub Desktop.
Extending Components and AppTheme with Material Design
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:font="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<com.monmonja.tutorial.RadioButtonCustomFont
font:name="Regular"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Yes"
/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="font">
<attr name="name">
<enum name="Regular" value="0"/>
<enum name="Thin" value="1"/>
<enum name="Medium" value="2"/>
<enum name="Light" value="3"/>
<enum name="Italic" value="4"/>
</attr>
</declare-styleable>
</resources>
package com.monmonja.tutorial;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.widget.TextView;
/**
* tutorial at http://www.tutorialforandroid.com/2015/03/extending-components-and-apptheme-with.html
*/
public class FontUtils {
public static int FONT_REGULAR = 0;
public static int FONT_THIN = 1;
public static int FONT_MEDIUM = 2;
public static int FONT_LIGHT = 3;
public static int FONT_ITALIC = 4;
public static void setTypeface (int fontName, TextView textView) {
Typeface font = null;
Context context = textView.getContext();
Resources resources = context.getResources();
if (fontName == FontUtils.FONT_REGULAR) {
font = Typeface.createFromAsset(context.getAssets(), resources.getString(R.string.font_regular));
} else if (fontName == FontUtils.FONT_THIN) {
font = Typeface.createFromAsset(context.getAssets(), resources.getString(R.string.font_thin));
} else if (fontName == FontUtils.FONT_MEDIUM) {
font = Typeface.createFromAsset(context.getAssets(), resources.getString(R.string.font_medium));
} else if (fontName == FontUtils.FONT_LIGHT) {
font = Typeface.createFromAsset(context.getAssets(), resources.getString(R.string.font_light));
} else if (fontName == FontUtils.FONT_ITALIC) {
font = Typeface.createFromAsset(context.getAssets(), resources.getString(R.string.font_italic));
}
textView.setTypeface(font);
}
}
package com.monmonja.tutorial;
import android.content.Context;
import android.content.res.TypedArray;
import android.support.v7.internal.widget.TintRadioButton;
import android.util.AttributeSet;
public class RadioButtonCustomFont extends TintRadioButton {
private int mFontName;
public RadioButtonCustomFont(Context context) {
super(context);
init(null, 0);
}
public RadioButtonCustomFont(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs, 0);
}
public RadioButtonCustomFont(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(attrs, defStyle);
}
private void init(AttributeSet attrs, int defStyle) {
final TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.font, defStyle, 0);
mFontName = a.getInt(R.styleable.font_name, FontUtils.FONT_REGULAR);
a.recycle();
if (!isInEditMode()) {
FontUtils.setTypeface(mFontName, this);
}
}
}
<resources>
<style name="FrameworkRoot.Theme" parent="Theme.AppCompat.Light.NoActionBar" />
<!-- Base application theme. -->
<style name="AppTheme" parent="FrameworkRoot.Theme">
.....
<item name="colorControlNormal">@color/component_normal</item>
<item name="colorControlActivated">@color/component_activated</item>
<item name="colorControlHighlight">@color/component_highlight</item>
<item name="colorAccent">@color/primary_accent_1</item>
.....
</style>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="font_regular">fonts/NotoSansCJKtc-Bold.oft</string>
<string name="font_thin">fonts/NotoSansCJKtc-Bold.oft</string>
<string name="font_medium">fonts/NotoSansCJKtc-Bold.oft</string>
<string name="font_light">fonts/NotoSansCJKtc-Bold.oft</string>
<string name="font_italic">fonts/NotoSansCJKtc-Bold.oft</string>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="font_regular">fonts/Roboto-Regular.ttf</string>
<string name="font_thin">fonts/Roboto-Thin.ttf</string>
<string name="font_medium">fonts/Roboto-Medium.ttf</string>
<string name="font_light">fonts/Roboto-Light.ttf</string>
<string name="font_italic">fonts/Roboto-Italic.ttf</string>
</resources>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment