Extensions to get resources like strings, colors or drawables easily, with support for themed attributes.
Supported platforms: Android.
If you want to use this dependency without using one of the fun packs,
you can use Splitties.resources
, provided you have refreshVersions added to the project.
For reference, the maven coordinates of this module are com.louiscad.splitties:splitties-resources
.
All the extensions of this module are currently available on
Context
, Fragment
and View
types.
Each one also has an app
prefixed version (e.g. appColor(…)
) that is
available everywhere but only returns the resources using the
application Context, which means they will not use the theme of the
current Activity.
color(…)
takes a color resource id and returns a colorInt
colorSL(…)
takes a color resource id and returns aColorStateList
styledColor(…)
takes a color theme attribute and returns aInt
styledColorSL(…)
takes a color theme attribute and returns aColorStateList
val brandPrimaryColor = color(R.color.brand_primary)
val accentColor = styledColor(R.attr.colorAccent)
dimen(…)
takes a dimension resource id and returns itsFloat
valuedimenPxSize(…)
takes a dimension resource id and returns its roundedInt
valuedimenPxOffset(…)
takes a dimension resource id and returns its truncatedInt
value
Like for colors, there are styled
prefixed versions that take
a theme attribute.
drawable(…)
takes a drawable resource id and returns aDrawable?
styledDrawable(…)
takes a drawable theme attribute and returns aDrawable?
bool(…)
takes a boolean resource id and returns itsBoolean
valueint(…)
takes an integer resource id and returns itsInt
valueintArray(…)
takes an integer array resource id and returns anIntArray
There are styledBool
and styledInt
that take a theme attribute.
txt(…)
takes a string resource id and returns aCharSequence
str(…)
takes a string resource id and returns aString
. There's also optional format arguments.qtyTxt(…)
(for plurals) returns aCharSequence
qtyStr(…)
(for plurals) returns aString
. There's also optional format arguments.txtArray(…)
takes an array resource id and returns anArray<out CharSequence>
strArray(…)
takes an array resource id and returns anArray<String>
styledTxt(…)
takes a string theme attribute and returns aCharSequence?
styledStr(…)
takes a string theme attribute and returns aString?
There's also optional format arguments.styledTxtArray(…)
takes an array theme attribute and returns anArray<out CharSequence>?
The resolveThemeAttribute
extension function for Context
takes a resource id of
type R.attr
and returns its corresponding resource id as resolved from the current
theme in the Context
.
That means it won't work with theme attributes whose corresponding values in the given theme are declared inline (and unlike the styledXxx
functions which support this case).