Refactored using TypedValue as suggested by @jeremiemartinez