Rich text in your strings.xml.
Take a look at a picture above. Think about how you would implement this design in terms of Android.
Yes, with different colors in the same text. Oh, highlighted parts should perform some action on click as well.
Aren't frightened yet? Well, make it use wildcards and placeholders (e.g. "group by: _, sort by: _"). And don't forget about localization.
B r e a t h e o u t.
If you're not new in modern Android development, then you've certainly done things alike before. Indeed, it's pretty common business' request — to reinvigorate monotonous Android TextView
with some styles in order to provide a better UX.
For my personal surprise, I've never found industry-standard solution, like ones we have for almost everything else in Android. From my experience, every time, when developer needs to do something like this, they either assemble their own solution from related answers, or use copy-pasted piece of code, which they have been using for last few years with minor adjustments each time.
Well, no more :)
StringAnnotations is a library that absorbed all best from old good familiar solutions, and enhanced it with great customization, extendability and convenience of use.
- Annotates string resources in your strings.xml, preserving ability for localization.
- Works with
Android Views
system andJetpack Compose
UI — choose what you need. - Provides neat API to use inside
Activity
,Fragment
and@Composable
scopes. - Extendable and customizable: add your own annotation types and arguments for them.
- 100% documented.
- Covered in unit tests.
io.github.mmolosay:string-annotations:views:VERSION
Artifact to be used with Android Views system.
See string-annotations:views README.md for more information.
io.github.mmolosay:string-annotations:compose:VERSION
Artifact to be used with Jetpack Compose UI.
See string-annotations:compose README.md for more information.
The library has not been published yet.
Arguments
provide values for your annotations.
You should pass Arguments
instance, containing all declared values, when obtaining annotated string.
Default Arguments implementation provides values for all out-of-the-box annotation types.
Since default implementations of AnnotationProcessor
use DefaultValuesParser to parse arguments, default placeholders' format is $arg${QUALIFIER}${INDEX}
, where QUALIFIER
should match the qualifier
of corresponding annotation type and INDEX
specifies index of desired
value.
Below you can see simplified example, how Arguments
can be used.
For more examples, view samples.
In your strings.xml:
<string name="demo">Text with <annotation color="$arg$color$0">multiple</annotation> <annotation color="$arg$color$1">colors</annotation></string>
In your code:
val color1 = ContextCompat.getColor(this, R.color.red)
val color2 = ContextCompat.getColor(this, R.color.green)
val args = Arguments {
colors(color1, color2)
}
yourTextView.text = getAnnotatedString(R.string.demo, args)
There are types of annotations, supported by default. You can extend it and implement your own ones.
Specifies background color of its body.
- Annotation attribute:
background
- Arguments qualifier:
color
<annotation background="$arg$color$0">text with background color</annotation>
Specifies foreground color of its body.
- Annotation attribute:
color
- Arguments qualifier:
color
<annotation color="$arg$color$0">colored text</annotation>
Specifies ability of its body to intercept click events.
- Annotation attribute:
clickable
- Arguments qualifier:
clickable
<annotation clickable="$arg$clickable$0">clcik me</annotation>
Specifies trivial graphic, e.g. line.
- Annotation attribute:
decoration
- Arguments qualifier:
decoration
<annotation decoration="$arg$decoration$0">decorated text</annotation>
Specifies absolute size of its body.
- Annotation attribute:
size
- Arguments qualifier:
size
<annotation size="$arg$size$0">text of some size</annotation>
Specifies typeface style of its body.
- Annotation attribute:
style
- Arguments qualifier:
style
<annotation style="$arg$style$0">styled text</annotation>
Check out Android Views and Jetpack Compose samples.
Copyright 2023 Mikhail Malasai
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.