public static string GetAttributeValue(Android.Content.Res.Resources.Theme theme, int resId) { TypedValue typedValue = new TypedValue(); theme.ResolveAttribute(resId, typedValue, true); return(typedValue.CoerceToString()); }
/// <summary> /// Retrieves a 32-bit integer color value from the specified resource ID /// </summary> /// <returns>The specified resource ID's color value if found, the specified default value if not found</returns> /// <param name="context">The <see cref="T:Android.Content.Context"/> context to be used for the retrieval</param> /// <param name="resourceId">The resource ID to retrieve the value for</param> /// <param name="defaultValue">The default color value to return if the specified resource ID isn't found</param> public static int ColorFromResourceId(Context context, int resourceId, int defaultValue) { int result = defaultValue; if (typedVal == null) { typedVal = new TypedValue(); } try { Android.Content.Res.Resources.Theme theme = context.Theme; if (theme != null && theme.ResolveAttribute(resourceId, typedVal, true)) { if (typedVal.Type >= DataType.FirstInt && typedVal.Type <= DataType.LastInt) { result = typedVal.Data; } else if (typedVal.Type == DataType.String) { result = ContextCompat.GetColor(context, typedVal.ResourceId); } } } #pragma warning disable RECS0022 // A catch clause that catches System.Exception and has an empty body catch (Exception) { } #pragma warning restore RECS0022 // A catch clause that catches System.Exception and has an empty body return(result); }
internal static string PullFontPathFromTheme(Context context, int styleAttrId, int[] attributeId) { if (styleAttrId == -1 || attributeId == null) { return(null); } Android.Content.Res.Resources.Theme theme = context.Theme; var value = new TypedValue(); theme.ResolveAttribute(styleAttrId, value, true); TypedArray typedArray = theme.ObtainStyledAttributes(value.ResourceId, attributeId); try { string font = typedArray.GetString(0); return(font); } catch (Exception) { //failed for some reason return(null); } finally { typedArray.Recycle(); } }
/// <summary> /// Last but not least, try to pull the Font Path from the Theme, which is defined. /// </summary> /// <returns>null if no theme or attribute defined.</returns> /// <param name="context">Activity Context.</param> /// <param name="styleAttrId">Theme style id.</param> /// <param name="subStyleAttrId">the sub style from the theme to look up after the first style.</param> /// <param name="attributeId">if -1 returns null.</param> internal static string PullFontPathFromTheme(Context context, int styleAttrId, int subStyleAttrId, int[] attributeId) { if (styleAttrId == -1 || attributeId == null) { return(null); } Android.Content.Res.Resources.Theme theme = context.Theme; var value = new TypedValue(); theme.ResolveAttribute(styleAttrId, value, true); int subStyleResId = -1; TypedArray parentTypedArray = theme.ObtainStyledAttributes(value.ResourceId, new int[] { subStyleAttrId }); try { subStyleResId = parentTypedArray.GetResourceId(0, -1); } catch (Exception) { // Failed for some reason. return(null); } finally { parentTypedArray.Recycle(); } if (subStyleResId == -1) { return(null); } TypedArray subTypedArray = context.ObtainStyledAttributes(subStyleResId, attributeId); if (subTypedArray != null) { try { return(subTypedArray.GetString(0)); } catch (Exception) { // Failed for some reason. return(null); } finally { subTypedArray.Recycle(); } } return(null); }