Resources¶
Extensions to get resources like strings, colors or drawables easily, with support for themed attributes.
Supported platforms: Android.
Setup¶
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.
Content¶
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.
Colors¶
color(…)takes a color resource id and returns a colorIntcolorSL(…)takes a color resource id and returns aColorStateListstyledColor(…)takes a color theme attribute and returns aIntstyledColorSL(…)takes a color theme attribute and returns aColorStateList
Example¶
val brandPrimaryColor = color(R.color.brand_primary)
val accentColor = styledColor(R.attr.colorAccent)
Dimensions¶
dimen(…)takes a dimension resource id and returns itsFloatvaluedimenPxSize(…)takes a dimension resource id and returns its roundedIntvaluedimenPxOffset(…)takes a dimension resource id and returns its truncatedIntvalue
Like for colors, there are styled prefixed versions that take
a theme attribute.
Drawables¶
drawable(…)takes a drawable resource id and returns aDrawable?styledDrawable(…)takes a drawable theme attribute and returns aDrawable?
Primitives¶
bool(…)takes a boolean resource id and returns itsBooleanvalueint(…)takes an integer resource id and returns itsIntvalueintArray(…)takes an integer array resource id and returns anIntArray
There are styledBool and styledInt that take a theme attribute.
Text¶
txt(…)takes a string resource id and returns aCharSequencestr(…)takes a string resource id and returns aString. There's also optional format arguments.qtyTxt(…)(for plurals) returns aCharSequenceqtyStr(…)(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>?
Any resource type¶
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).