Java tutorial
//package com.java2s; //License from project: Apache License import android.content.Context; import android.content.res.TypedArray; import android.util.AttributeSet; import android.util.SparseArray; public class Main { private static final SparseArray<int[]> ATTRIBUTE_ARRAY_CACHE = new SparseArray<>(10); /** * Tries to pull the Font Path from the Text Appearance. * * @param context Activity Context * @param attrs View Attributes * @param attributeId if -1 returns null. * @return returns null if attribute is not defined or if no TextAppearance is found. */ static String pullFontPathFromTextAppearance(final Context context, AttributeSet attrs, int attributeId) { if (attributeId == -1 || attrs == null) { return null; } int textAppearanceId = -1; final TypedArray typedArrayAttr = context.obtainStyledAttributes(attrs, getAttributeArray(android.R.attr.textAppearance)); if (typedArrayAttr != null) { try { textAppearanceId = typedArrayAttr.getResourceId(0, -1); } catch (Exception ignored) { // Failed for some reason return null; } finally { typedArrayAttr.recycle(); } } final TypedArray textAppearanceAttrs = context.obtainStyledAttributes(textAppearanceId, getAttributeArray(attributeId)); if (textAppearanceAttrs != null) { try { return textAppearanceAttrs.getString(0); } catch (Exception ignore) { // Failed for some reason. return null; } finally { textAppearanceAttrs.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; } }