Skip to content

Instantly share code, notes, and snippets.

@Takhion
Last active January 1, 2016 20:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Takhion/8197815 to your computer and use it in GitHub Desktop.
Save Takhion/8197815 to your computer and use it in GitHub Desktop.
A android.widget.Switch that accepts the attribute "android:fontFamily" inside a style defined by the attribute "android:switchTextAppearance". Makes sense from API 16+ as before the attribute doesn't exist; however it can easily be modified with a custom one.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="Android">
<attr name="android:fontFamily"/>
</declare-styleable>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<SwitchFamilyName
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/Switch"/>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Switch">
<item name="android:switchTextAppearance">@style/Switch.SwitchTextAppearance</item>
</style>
<style name="Switch.SwitchTextAppearance" parent="Switch">
<item name="android:fontFamily">sans-serif-condensed</item>
</style>
</resources>
package android.widget;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.Switch;
/**
* 28/12/13
* Created by Eugenio Marletti
* https://gist.github.com/Takhion/8197815
*/
/**
* A Switch that accepts the attribute "android:fontFamily" inside a style defined by the
* attribute "android:switchTextAppearance".
*
* @attr ref android.R.styleable#TextView_fontFamily
* @attr ref android.R.styleable#Switch_switchTextAppearance
*/
@TargetApi(16)
public final class SwitchFamilyName extends Switch
{
/**
* Construct a new Switch with default styling.
*
* @param context The Context that will determine this widget's theming.
*/
public SwitchFamilyName(Context context)
{
super(context);
}
/**
* Construct a new Switch with default styling, overriding specific style
* attributes as requested.
*
* @param context The Context that will determine this widget's theming.
* @param attrs Specification of attributes that should deviate from default styling.
*/
public SwitchFamilyName(Context context, AttributeSet attrs)
{
super(context, attrs);
}
/**
* Construct a new Switch with a default style determined by the given theme attribute,
* overriding specific style attributes as requested.
*
* @param context The Context that will determine this widget's theming.
* @param attrs Specification of attributes that should deviate from the default styling.
* @param defStyle An attribute ID within the active theme containing a reference to the
* default style for this widget. e.g. android.R.attr.switchStyle.
*/
public SwitchFamilyName(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
}
/**
* Sets the switch text color, size, style, font family, hint color, and
* highlight color from the specified TextAppearance resource.
*
* @attr ref android.R.styleable#Switch_switchTextAppearance
*/
@Override
public void setSwitchTextAppearance(Context context, int resid)
{
super.setSwitchTextAppearance(context, resid);
TypedArray appearance = null;
try
{
appearance = context.obtainStyledAttributes(resid, R.styleable.Android);
final String fontFamily = appearance.getString(R.styleable.Android_android_fontFamily);
if (fontFamily != null)
{
final Typeface currentTypeface = getTypeface();
final Typeface newTypeface = Typeface.create(fontFamily, currentTypeface != null
? getTypeface().getStyle() : Typeface.NORMAL);
if (newTypeface != null) setSwitchTypeface(newTypeface);
}
}
finally
{
if (appearance != null) appearance.recycle();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment