A set of Kotlin extensions for accessing resources.
Add the dependency:
repositories {
mavenCentral()
google()
}
dependencies {
implementation("com.redmadrobot.extensions:resources-ktx:1.3.1-0")
}
Accessors for View
:
View.getColor(@ColorRes resId: Int): Int
View.getColorStateList(@ColorRes resId: Int): ColorStateList?
View.getDimension(@DimenRes resId: Int): Float
View.getDimensionPixelSize(@DimenRes resId: Int): Int
View.getDimensionPixelOffset(@DimenRes resId: Int): Int
View.getDrawable(@DrawableRes resId: Int): Drawable?
View.requireDrawable(@DrawableRes resId: Int): Drawable
View.getDrawableWithTint(@DrawableRes resId: Int, @ColorInt tint: Int): Drawable?
View.getString(@StringRes resId: Int): String
View.getString(@StringRes resId: Int, vararg formatArgs: Any): String
View.getQuantityString(@PluralsRes resId: Int, quantity: Int): String
View.getQuantityString(@PluralsRes resId: Int, quantity: Int, vararg formatArgs: Any): String
Accessors for Fragment
:
Fragment.getColor(@ColorRes resId: Int): Int
Fragment.getColorStateList(@ColorRes resId: Int): ColorStateList?
Fragment.getDrawable(@DrawableRes resId: Int): Drawable?
Fragment.getDrawableWithTint(@DrawableRes resId: Int, @ColorInt tint: Int): Drawable?
Fragment.getDimension(@DimenRes resId: Int): Float
Fragment.getDimensionPixelSize(@DimenRes resId: Int): Int
Fragment.getDimensionPixelOffset(@DimenRes resId: Int): Int
Fragment.getQuantityString(@PluralsRes resId: Int, quantity: Int): String
Fragment.getQuantityString(@PluralsRes resId: Int, quantity: Int, vararg formatArgs: Any): String
Extensions for Drawable
:
Drawable.withTint(@ColorInt tint: Int): Drawable
Extensions to resolve attributes from Context
theme:
Context.resolveAttribute(@AttrRes attributeResId: Int): TypedValue?
Context.resolveAttributeOrThrow(@AttrRes attributeResId: Int): TypedValue
Context.resolveBoolean(@AttrRes attributeResId: Int): Boolean
Context.resolveColor(@AttrRes attributeResId: Int): Int
Context.resolveColor(@AttrRes attributeResId: Int, @ColorInt defaultValue: Int): Int
Context.resolveDimension(@AttrRes attributeResId: Int): Float
Context.resolveDimensionPixelSize(@AttrRes attributeResId: Int): Int
Context.resolveDimensionPixelOffset(@AttrRes attributeResId: Int): Int
Context.resolveFloat(@AttrRes attributeResId: Int): Float
Context.resolveInt(@AttrRes attributeResId: Int): Int
Context.resolveString(@AttrRes attributeResId: Int): CharSequence?
Context.resolveResourceId(@AttrRes attributeResId: Int): Int
Dimension converters for Context
(the same available for Resources
):
Context.dpToPx(dp: Int): Int
Context.dpToPx(dp: Float): Int
Context.dpToPxPrecisely(dp: Int): Float
Context.dpToPxPrecisely(dp: Float): Float
Context.pxToDp(px: Int): Float
Context.pxToDp(px: Float): Float
Merge requests are welcome. For major changes, please open an issue first to discuss what you would like to change.