Java tutorial
//package com.java2s; //License from project: Apache License import android.content.Context; import android.content.res.Resources; import android.content.res.TypedArray; import android.util.SparseArray; import android.util.TypedValue; public class Main { private static final SparseArray<int[]> ATTRIBUTE_ARRAY_CACHE = new SparseArray<>(10); /** * Last but not least, try to pull the Font Path from the Theme, which is defined. * * @param context Activity Context * @param styleAttrId Theme style id * @param attributeId if -1 returns null. * @return null if no theme or attribute defined. */ static String pullFontPathFromTheme(Context context, int styleAttrId, int attributeId) { if (styleAttrId == -1 || attributeId == -1) return null; final Resources.Theme theme = context.getTheme(); final TypedValue value = new TypedValue(); theme.resolveAttribute(styleAttrId, value, true); final TypedArray typedArray = theme.obtainStyledAttributes(value.resourceId, getAttributeArray(attributeId)); try { String font = typedArray.getString(0); return font; } catch (Exception ignore) { // Failed for some reason. return null; } finally { typedArray.recycle(); } } /** * Last but not least, try to pull the Font Path from the Theme, which is defined. * * @param context Activity Context * @param styleAttrId Theme style id * @param subStyleAttrId the sub style from the theme to look up after the first style * @param attributeId if -1 returns null. * @return null if no theme or attribute defined. */ static String pullFontPathFromTheme(Context context, int styleAttrId, int subStyleAttrId, int attributeId) { if (styleAttrId == -1 || attributeId == -1) return null; final Resources.Theme theme = context.getTheme(); final TypedValue value = new TypedValue(); theme.resolveAttribute(styleAttrId, value, true); int subStyleResId = -1; final TypedArray parentTypedArray = theme.obtainStyledAttributes(value.resourceId, getAttributeArray(subStyleAttrId)); try { subStyleResId = parentTypedArray.getResourceId(0, -1); } catch (Exception ignore) { // Failed for some reason. return null; } finally { parentTypedArray.recycle(); } if (subStyleResId == -1) return null; final TypedArray subTypedArray = context.obtainStyledAttributes(subStyleResId, getAttributeArray(attributeId)); if (subTypedArray != null) { try { return subTypedArray.getString(0); } catch (Exception ignore) { // Failed for some reason. return null; } finally { subTypedArray.recycle(); } } return null; } private static int[] getAttributeArray(int attributeId) { int[] array = ATTRIBUTE_ARRAY_CACHE.get(attributeId); if (array == null) { array = new int[] { attributeId }; ATTRIBUTE_ARRAY_CACHE.put(attributeId, array); } return array; } }