public static int GetColor(Context context, int color) { var tv = new Android.Util.TypedValue(); context.Theme.ResolveAttribute(color, tv, true); return(tv.Data); }
/// <summary> /// This method can be extended to get all android attributes color, string, dimension ...etc /// </summary> /// <param name="context"> used to fetch android attribute </param> /// <param name="androidAttribute"> attribute codes like Resource.Attribute.colorAccent </param> /// <returns> in this case color of android attribute </returns> public static int fetchContextColor(Context context, int androidAttribute) { TypedValue typedValue = new TypedValue(); TypedArray a = context.ObtainStyledAttributes(typedValue.Data, new int[] { androidAttribute }); int color = a.GetColor(0, 0); a.Recycle(); return(color); }
/// <param name="context"> used to fetch display metrics </param> /// <param name="dp"> dp value </param> /// <returns> pixel value </returns> public static int dp2px(Context context, float dp) { float px = TypedValue.ApplyDimension(ComplexUnitType.Dip, dp, context.Resources.DisplayMetrics); return(Math.Round(px)); }