Java tutorial
//package com.java2s; //License from project: Apache License import android.content.Context; import android.content.res.TypedArray; import android.text.TextUtils; 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 View Style as this is the next decendent after being * defined in the View's xml. * * @param context Activity Activity Context * @param attrs View Attributes * @param attributeId if -1 returns null. * @return null if attribute is not defined or found in the Style */ static String pullFontPathFromStyle(Context context, AttributeSet attrs, int attributeId) { if (attributeId == -1 || attrs == null) return null; final TypedArray typedArray = context.obtainStyledAttributes(attrs, getAttributeArray(attributeId)); if (typedArray != null) { try { // First defined attribute String fontFromAttribute = typedArray.getString(0); if (!TextUtils.isEmpty(fontFromAttribute)) { return fontFromAttribute; } } catch (Exception ignore) { // Failed for some reason. } finally { typedArray.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; } }