Best practices for text on Android (Google I/O '18)
Why Stack Overflow recommends it
Stack Overflow users link it when answering questions about spans, custom typefaces, annotation tags, line height and text performance on Android, since it gives the official guidance from the Android team.
Recommended by 4 people across 3 different months, 2018 to 2022.
What it is
A Google I/O 2018 session by Android engineers Siyamed Sinir, Clara Bayarri and Florina Muntenescu on handling text in Android apps. It covers styling with spans, editable text, measuring text, hyphenation and break strategies, and text performance pitfalls, with a short live demo.
What people said
Recommends android developer · 2018-05 · Stack Overflow · on “How can I use TypefaceSpan or StyleSpan with a custom Typeface?”
On Android P it's possible using the same TypefaceSpan class you know of, as shown here [this video] . But on older versions, you can use what they've shown later in the video, which I've written about here [LINK] .
Recommends android developer · 2018-05 · Stack Overflow · on “android getSpans parameters”
I think `getSpans` is inefficient for this use case, so you might want to try `nextSpanTransition` instead. Here, might be useful: [this video] .
Recommends m.reiter · 2021-02 · Stack Overflow · on “Android lineHeight vs lineSpacingExtra”
MaterialTextView [LINK] (MTV) includes the ability to set the android:lineHeight . And if you use the app: prefix, it is backwards compatible until SDK-Version 21. Btw., under some circumstances (i believe its depending on your style, but am not sure about it), standard TextViews are automatically replaced with MTVs. (Check your LayoutInspector, you might already use them without knowing it) In case you're still wondering about the differences, there's an excellent talk by the android team [this video] about text in general; which should solve your question at ~16:50.
Recommends LLL · 2021-02 · Stack Overflow · on “Performance: how to display huge texts smoothly using Recyclerview”
I'm building a note application that must be able to display long texts (10000+ characters) According to Best practices for text on Android (Google I/O '18) [this video] , I followed some tips to achieve that: Break huge texts into parts and display them using recyclerview Prefetch Text [LINK] with PrecomputedTextCompat My code is looking like this: inner class DescriptionViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { private val textView: AppCompatTextView = itemView.adapter_description_text fun bindView(descriptionModel: NoteUIModel.DescriptionModel) { textView.setTextColor(displayTextColor) textView.setTextFuture(PrecomputedTextCompat.getTextFuture( descriptionModel.descripti
Recommends Cheticamp · 2022-05 · Stack Overflow · on “Content Description in Multi Language”
If I understand you questions, I think that you misunderstand the purpose of LocaleSpan [LINK] . This span will not do the translation for you, but just permits the developer to identify a part of a string the may be in another language from the rest of the string. There is a video [this video] from Google I/O '18 that explains this starting at time mark 5:16. You should take a look at Localize your app [this video] on how to present different translations for strings. You should also take a look at App resources overview [LINK] .
Cites android developer · 2018-05 · Stack Overflow · on “How to use support library fonts feature as a part of the TextView content (using spannable)?”
Since both answers of MJM and TheMatrix are practically the same (yet over-complex for me) and both were answered around the same time, I couldn't just choose one of them, so I granted +1 for each, asking them to make it shorter yet support XML tag for easier handling with strings file. For now, here's the much shorter version of how to set a custom font for a part of the text in the TextView: class CustomTypefaceSpan(private val typeface: Typeface?) : MetricAffectingSpan() { override fun updateDrawState(paint: TextPaint) { paint.typeface=typeface } override fun updateMeasureState(paint: TextPaint) { paint.typeface=typeface } } Sample usage : val text = "Hello world" val index = text.indexOf
Cites android developer · 2018-05 · Stack Overflow · on “How to offer actions of my app to other apps, via Smart-Linkify (AKA "smart text selection")?”
Background For some time, as a user, I've noticed that on some apps I can perform common operations on partial texts, as such: [LINK] This is called "smart text selection" (video here [LINK] ), and it has being around from Android O, as shown here [LINK] . The problem Recently Google has shown how to make your app show the actions within itself (meaning inside its TextView). This is called "Smart-Linkify" and the video about it can be found here [this video] . It shows there how to show the actions: [LINK] What I can't find is how to do the opposite. Meaning how to offer my own actions to other apps. For example, I could make a dialer app that can handle phone numbers by either dialing them or se
Cites android developer · 2018-05 · Stack Overflow · on “Android Spannablecontent With Rounded Corners”
Watching Google's video [this video] , they offer this solution: [LINK] [LINK] [LINK] [LINK] [LINK] [LINK] Sadly I see here various things missing, and I can't find the full code, so I can't try it out.
Cites android developer · 2018-07 · Stack Overflow · on “Android autosizing TextViews split up words before decreasing font size”
I've actually reported about this issue in the past ( here [LINK] and here [LINK] , one you've already found out about), but it seems there is a solution, or at least it seems to work for me. I think the tip could help: [LINK] [this video] [LINK] This means you can choose the break strategy for the textView. Example: <TextView android:layout_width="250dp" android:layout_height="wrap_content" android:background="#f00" android:breakStrategy="balanced" android:hyphenationFrequency="none" android:text="This is an example text" android:textSize="30dp" app:autoSizeTextType="uniform"/> Sadly, this doesn't always seem to work nicely as it should. I've noticed that on Hebrew, it sometimes doesn't get spre
Cites android developer · 2020-11 · Stack Overflow · on “Set different font and color to part of a TextView”
Google actually gave an official answer about this on this video [this video] : Also you can follow the docs [LINK] explaining it as well Please be aware that this approach is just Android P+ compatible and strings cannot be formatted (using %1$s params) strings.xml <string name="test">Hello <annotation font="sans-serif-medium">World!</annotation></string> And in code: val spanned = context.getText(R.string.title) as SpannedString val spannable = SpannableString(spanned) spanned.getSpans(0, spanned.length, Annotation::class.java).filter { annotation -> annotation.key == "font" }.forEach { annotation -> val typeface = Typeface.create(annotation.value, Typeface.NORMAL) spannable.setSpan(TypefaceSpa