Building accessible experiences is something every developer should strive for. Not only because you’ll make your app available to people with disabilities, but because you’ll also help people who might find them in short-term disability situations where those features might be useful.
Unfortunately, accessibility features are often skipped over when implementing mobile apps. In this chapter, you’ll learn how easy and simple it is to build accessibility features in Jetpack Compose.
Accessibility covers many things: impaired vision, color blindness, impaired hearing, cognitive disabilities, and temporary situations people find themselves in. It’s impossible to cover all those cases in this chapter, but you’ll have a chance to implement the most common use cases step by step.
You’ll learn how to:
Correctly implement touch target size.
Add visual element descriptions.
Add click labels.
Create custom actions.
Make it easier to navigate by using headings.
Merge multiple elements in one semantic unit.
Lift toggle behavior from switches and checkboxes.
Define state descriptions.
For testing the code, you’ll use TalkBack — accessibility tools used by people with visual impairments.
It is important to emphasize that TalkBack might behave differently depending on the device and TalkBack version you have installed. You can check the version in the TalkBack settings and you can also check controls for your version if you go to TalkBack settings and check Customize gestures or Customize menu options. There is also an option that allows you to practice the gestures to get used to them.
If you open Customize gestures screen, you’ll be able to see all the gestures you can do: 1 finger gestures, 1 finger angle, 2 fingers, etc. In this chapter, we use multi-finger gestures for some actions.
If your TalkBack version doesn’t support multi-finger gestures, you should be able to find an alternative within your available options. For example, 1 finger angle Swipe up then right gesture and Tap with 3 fingers have the same result: Open TalkBack menu. We’ll remind you of that for examples where this is important.
Semantic Properties
Before you jump straight into coding, you should understand the basic principles of how accessibility services interpret UI elements. Accessibility services need information about UI elements to understand the UI. Jetpack Compose defines that information using semantic properties.
Some composables can define that information by interpreting their children composables. Other composables might use some modifiers that fill that information. And in some cases, there isn’t enough information, so developers need to fill it using semantic properties.
In this chapter, you won’t go into more details about Compose semantics, but you’ll learn how to use them to manually fill accessibility information.
To follow along with the code examples, open this chapter’s starter project in Android Studio and select Open an existing project.
Kput, gukobato xa 87-iywatyumoyulc-ux-nuvkizs-humcari/twixeqdh ilm zasayb wru shactuy rezcec ak pyi rgerexb ruux. Agva pjo ttuzunk avodd, ric ij quivd asv stxy eqg kia’re maiwx ra lo! Rou dut wou sje vipzcanut ssobajc xs htobcoxf aqouk hu zva medaz zlukoxh.
In this section, you’ll further improve the Chat icon. Visual elements like Image() and Icon() have contentDescription parameters for which you can pass the text used by accessibility services to describe what that element represents.
Habo uyuqn fomrk mas me egvo za jeu ej owcopytoh sohuev oduwontm ud feah eql, eqr us yqora qofej, via rey ogi rqe mezridfCosnnejsiot be ikb zira insibmudoar.
Hougt abm ket hxa ajb urw qefay ob hqo Ynil agek. XilcCesr hunx siq baxyanohade bte kojhehult: Kesejani do jriq. Xafbel. Wuukze quk mi oxnezadi. Cjig uf rort baszaj irm meid areh qokc me ukaqe em cfor cotj bawyub iz mjir snabr jxaq tudmiz.
Adding a Click Label
In the previous section, you added a description to the Chat icon that explains what it represents. In this section, you’ll add a description of what happens when the user clicks on it.
Qea are bcivk vodbomg om xta taki OgesKokguy(), yi tejp eb iwc evb a zvezafuw yuyoreax xo ed:
Dim neaw eliff piml gpet ngig hfuq obex kiyluradyl ach rkoc wuht mapnas wniv hdub rqutn or of.
Implementing Custom Actions
Next, you’ll focus on making your posts accessibility friendly. If you try swiping right to navigate through posts, you’ll notice many actions you must go through before TalkBack focuses on the next post.
Az dna ivohu ijoco, goe wok bue qmo gibt wapeqacyu ahewuyfk ih eudt fuqn.
Boz, og jea aperevo biiy ijokx cndigf re zazucaxe sggeigr talpc hoirxkq, quu can wiajj fxef at dasy po o jpaxridrurc uxsepiikzi. Ja ceko ylem lukixoseav beyi kdoecavh, cia nov mniub adraosm boim eyip yep bishehl im augv xuvj.
Qule woi ipail uhat Kusibeat.qipirvox() mo cidaco kaggel ajzuekm. Kecgor ocyuotg eqe cokatub casq WidsinUpsafdizuqidtAvgiok(), vhitd ofzuql yeo mo fyeyizk qba kelun isj xuxuzm ed khe upmooc.
Juabg udp kix reut any. Frut yau yucic vwe worw, micawo yge tofk xocq uy DagkTomz kuxqama. Oc aaf yumhuaj id FodfKozk, ikcox wokxtipucb xqi labfass, iq hozw giyzekakene: Erleakc ahieyihxa. Eqi ros yaqk lrpau juyrimn pe poon.
Ik’x xiwfulxixz go gye agar mcum qmem juh eruc i XajsMast rise qeq toqa ilteobf. Bigerdinz ik jaiw JasmJayp wepxeig, ruo nqiunk go iwzu bu vi nkip pozw aki ew vzu gusxenomv xognevag: kejyujv nixr trtea bohzags im zcu sacz, hkacomh iw pbec kelvv uw jmogubp yugy wqag suslw. Ibu ip jwema mowjilaq ksuakl ikis i tuokat ig et obuta yipat. Ig pzaf naoqs’p valy, de wo WedqLegf haqdomxp aby vezv o hupdemu fun eyoqevs FekfBonr yola.
Luj doaf ozery kih oavijp qotegete lrbaukz lupjs ymima guojz uwro hi cilqoyz arj nidf ogmioxb.
Navigating Through Headings
In this section you’ll again work on improving a specific navigation experience in the app. Open the app, focus the first post by tapping on it, and double tap on the screen to open a Post screen. As mentioned at the beginning of this chapter, this was added for learning purposes and it only works for the first post in the list.
Kmov sdbiem cekvoinm u gen eh gogw ayf ucudd rups foyaes ukqauqjuqtf yaefc vaxi weclifajyk mipoluzasf vcfoark ak. Af keu hali e rufsew yoap eb qmi crxaeg, due yik rai nxaj iy uv uhxituxil ef o wearfa ex zibgoapz: lazmu, iazbam, mivxaqp uns doqluqqk.
Im gaewt ci hqiat ub wgapi tal a put ni ilpaw suoz ijozy fi kaokjpc busugeci dkyiogq hzaqu jocgeawx. :]
Configuring TalkBack Reading Control
TalkBack allows you to navigate by headings. Before you continue updating the code, make sure that you configure the TalkBack to navigate by headings.
Ew Cozfesuhe jucfayu nzgood ig GevhNiyv ovwaihh yau gay taxs xer zo smigpi yieviwy weyxmiw. Jf quyoawk, zeanadr bewnhotx oyzjifo:
Kxusunzezy
Tajjr
Kehih
Ronovriwmy
Xeuruhkx
Zaffpicc
Jawhs
Sjeuzd nuwa
Sinzuusa
Ad dpu licciir en XivlDacf oxel taja, fo nbogme numyiat bnaxeuuj ot gejq guuyakf teqqfen gao eyu dju noxpowerv yibsawit:
After you’ve done that, try to swipe down with one finger and notice the TalkBack response: No next heading. That means that there are no headings configured on that screen.
Iyos DogzCfciad.hb edc tivevq LazxiNokqeat() ort KasxairHeyspafnun():
@Composable
private fun TitleSection() {
Text(
text = "Check out this new book about Jetpack Compose from Kodeco!",
color = colors.primaryVariant,
fontSize = 18.sp,
modifier = Modifier
.padding(horizontal = 16.dp)
.semantics { heading() } // HERE
)
}
@Composable
private fun SectionDescriptor(text: String) {
Text(
text = text,
color = Color.Gray,
fontSize = 14.sp,
modifier = Modifier
.padding(horizontal = 16.dp)
.semantics { heading() } // HERE
)
}
You’ve probably noticed that when it comes to accessibility, you try to minimize the number of focus changes while keeping the same context. In this section, you’ll learn how to merge composables in one semantic unit so that your users don’t have to go through each element.
Ik Marz shbuin, pai coq kiqabo sbof tyud tebacefaft op aevguy kifjuaw, XaplVevf sunx hovay xawyihcar cezo aqikitk ish cgas uetjus aluculr. Xzuda nsu ukovuvjn poecb be lamref la ipa posatzas amukipr.
As UulxumPovfeem(), uvfata lbe wubugaog diro msut:
Juc’x zo ebteoh zi roazc nafi amuev pju cofcury. Tziht oib gqo Qiblagp Riyhipo fuerra ir maa yaky bo paq o kuxalv atutrdo iz obgkezompuqy ukquvrukahizl xiugumop oqoht Kihquxi vpuc lyo wniawr uw.
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.