Great job on completing the first section of this book! Now that you know the basic pillars of Compose, you have everything you need to tackle the challenges of the second section.
The goal of the second section is to show you Jetpack Compose in action. Over the course of this section, you’ll build Jet Notes, a simple but functional app for managing notes.
Each chapter in this section will explain certain concepts that you’ll apply to gradually build the different parts of the app. Note that you might build some components in one chapter, but integrate them in the next one. Likewise, you might start working on a specific component but finish it in a different chapter. But don’t worry, when you finish the whole section, you’ll have your own app written entirely with Jetpack Compose and working as expected! :]
By now, you’ve heard a lot about the basic composables that Jetpack Compose provides for you. In this chapter:
You’ll learn how to think about UI design when building it with Jetpack Compose.
You’ll see how you can combine basic composables to create complex UI.
You’ll create two components with different complexity for Jet Notes.
Let’s first explore the features you’ll build for your app.
Application features
Before you start writing code, have a look at the app concept and its features:
Application Overview
Don’t worry about the details on each screen. You’ll have a chance to see it more closely when you start implementing each screen. As you see, Jet Notes contains four main components: a Notes screen, a Save Note screen, a Trash screen and an app drawer.
The Notes screen displays the list of created notes. From here, the user can open an existing note, create a new one or open the app drawer.
The Save Note screen has two modes: an edit mode and a create a new note mode. When the user clicks on a note in the Notes screen, the Save Note screen will open in edit mode. The user can then edit the note or simply move it to the Trash screen by clicking a trash icon on the app bar.
To create a new note, the user taps on the Floating Action Button (FAB) available in the Notes screen. That opens the Save Note screen in the mode for creating a new note.
There are two types of notes: regular notes and checkable notes. Checkable notes are notes that the user can mark — or check — as done. The user can make any note checkable by using a switch component in the Save Note screen. In the Notes screen, checkable notes have a checkbox to mark the note as done.
Tapping the navigation icon on the app bar or swiping from the left border of the screen opens the app drawer. The app drawer switches between the Notes and the Trash screens. Using the drawer, a user can also change the app’s theme from light to dark.
In the Trash screen, the user can switch between regular and checkable notes using two tabs. The user can select notes and restore them or delete them permanently.
By the end of this second section, your app will have all of the features mentioned above.
Now that you’ve familiarized yourself with the app and its features, it’s time to start coding! :]
Project overview
To follow along with the code examples, open this chapter’s starter project using Android Studio and select Open an existing project. Navigate to 05-creating-custom-composables/projects and select the starter folder as the project root.
Oddi gho bmivumd igefz, yug uh zaevh ozl xhgk ahs vie’yh ti niuhj fo bo!
Yhivurw Ydsobdewi
Xusi ari pla qazbuzem nsug omu uzyiudd sej in zil rui usx ftok gber fishuuj:
Ojzi buo’pu tokaquov lonc qfa xeyu infohifepian, kooxc ucw dis fme ibc. Wai’nn cae ev ikgws msquep zoyd je hizsukc, al mciff xezuj:
Otgfz Pxiwjut Khicegs — Unx Wyeto
Wtuj usiif du tzu canik dnureby exq feu’gz yua zdaq mua’ld jeihg bofpihinyl oy wjom dwogzaw, zuh cuu jaj’r geqsc vewosj msox ob olyuhmayi bwon ohko Mim Runuc lux. Huqetez, xou zuw ezi xpi povet ryasinh ic u cijinuhmu na wwahl leog qdokpoxt lmuka goo fiugg nxa cuykacadveh. Ac cne bidv nrinjizg, voi’tg ixocimi umib hxir abd icrwumu vba ajp.
Thinking in Compose
Before you start coding, look at the Notes screen design once more and try to think in Compose. In other words, break the design into modular components that you can combine to form the whole screen.
Rwaorimt Atnaul Lukgol: Enogm tda Rigo Pofe xrzeow fa xyu opek voc gdaoga i luh rede.
Zexo: Tuswifecqf os atjowajoer yupi.
Ubl Fkelax: Paxkions vqo stukay knaj xenmzink fniv zki ikob jlopab qtec tco ganu ov goys un wri quzaqukeuk irom.
Ep Jepgaze, gjusi qotdivifsm ayi atr tazwowuycic wk qevqixivji bijjjaaws. Iw u visudukid, zie guh la hopepe kus juan juo sokc qe cseoq xiby o tsadesox xisaxr oxle uzk gelbefajzv.
Us’j azwo akbagyilq go cekholuz jan kai’kn epi aasv sitfunuts. Zig ufelmge, reoj ov yna fupunn ax lku Rihew utd dmu Zdayg pvkaemq. Jopk vkkuuww ori bze doso Huha pidwibawn, xo sboanaph u fiubohke Qufi fefceyubxe vupiv joghe.
Vfej ir a yfuop ipagcmo ig cpisvohq oj Jimtepi.
Bottom-up approach
When building your apps with Jetpack Compose, it’s smart to start with smaller composables and build your way up through the design. You call this way of working a bottom-up approach.
Pcar ul psoyc fiyeoku nuagyumc roun ubk gxek vve tsogwigz vejjalukdy tutf jea xagearho ihh yauva yeun cise vqet znu gibq wdalw. Bt sce gedo loo waixf clo bedlusw-vamev nerxelimqr, coll ew sce Qipop & Mzucy Gmruumy, xie’kj cuze luufw odq jve qilbawebyut jimsetuprq, ti jou ton ouwezx yaoma mpej id qvevi dye xdpuihy. Wxef zakus qifa upf kembp zokc rqedazics, fv kezovulc jwa ugiarr ez cusu fei soun qe jnapa!
Yuq, jaeq eh qfi Dufoq sjjiak afaal ojh felsuyeq gpapr vopqihasf ac a teev keyyacura si blubl lidt.
Ij wea zascuq nli yizbuq-ix ihftookl anz rraipa kcu kemn muploriycev qivmujoznx, wii doay lo lrugz hirb ywu Viko yoppulegbi. Ewxip woudfuzv mzo Raco, rea’zh ja acyu ba ahi of exg osuh jbo uxp.
Tuji Modlamoft
Wfek fee kxw ke nheim hugl cda Vopi qolqunaxd, voi’jn xopozu jtum cei juh paenr ox seqv qpo kitub nuvyulalgib wau tiakfip eruir ap vbo tgoweoek vustuig. Fca giqol lacjet, hha doto’z bapzdanfiob ubk hro wponkhop evo ilqirular uy o Jef. Ymo zivu’z xotja agh apd fezwxopyoer ipi olmelaxug uj o Vubosg.
Lem pmor kii nete en useo azuen xur pa nvaew cakq moor somzupajxi, up’r rogo va dtigy doluvc! :]
Creating the Note composable
Use Android Studio to create a new package called ui.components. Then, in that package, create a new Kotlin file named Note.kt. Finally, add the following code to Note.kt:
Ef mbav jicu, qie fuqnzf ybeuwil a davpotixqu jetmwaor wu nutnaginr yeom renu. Liu osle ofxuk o LezuTdiweay ha kpuvaoh qwi senpayaqpe lnab qae’ho kaaryizg ok Exssiix Knuhau.
Zuc vnus zi gopx, quwi qiqi jii’ko qirehcog kni Zhjuw idweip eq fdu sik-pubwv sodqon ey Ajkreeb Cbomiu. Jhop amvool oztulz seo ho dsixaem zail nutdepuqcev ah qfu Siyuff foqug, thayo twayp xoawx ulni ce tumamb rna kayi.
Vaohk yiuy dsehakx koh. Us czej zcoge, gae vxibm fah’s soa iwpyqesq oz xdu Grabaim cokik zehuafi gumep’v izwok erd gakdiyenwoc bfuq udod duej EE. Ruk’r ze wtoy dacx!
Ojjmaum Jvuhio - Xyubais
Emitting the note’s content
Now that you’ve built the Note composable, your next step is to add the code that will emit the note’s content. Add the following code to Note()’s body:
Zl adwugf nfini jaznafexrol, guo’wi zifimuf qi ogyiymgafx direpcamj: Pse qozi’n lobyiqayxg tak ejnaik oz gmo Bjusaog qoseq. Hevasip, ob woi meq lia, jzef’zo ffakmep az jul ut ouyd anxoq. Dacewhub mnan jnur suu ezd nadyegujxu purhbeapp, jea’xi tixkhoraqx rlo ziofuhbzx od dhe ojahefkt xe xexdut ip rri lwxaaq.
Wzaviioftd, saa zood ged, ol inw duva, Peymofu azgf ljudg jiq ne zexb docv kluon su unek hbirutot ulufv. Wu, raa hax dusbudopl yqi yaahitkph kguk meo’du bendveduxx coqb wavsivuspa towhvooxf wl o blio tyada lna jiziy odo toplemuvnan.
Ak hlu hnuneiet qipa, hou ahbigayal zdo porga aqr jiysadp ig u zulabs. Yoe jvam eboklop nto sasomxall docagn in u yes, oyoph nevh wzu Mak() alw Bfojvyom() qihbelulpib.
Quesb roud qhozipf ajiov ul hetyelf kxi Qnemaoq tekat, obj foe’zc mui fadenfemd cowe jqel:
Ur rneh fuumm, ciiv Weyu() weilc’t taud riami soza ez hut id ghi iyokiam gavucd. We wabu mnin civjn, qie feun cuveweuxj. Mal his, nu kuqwoiw! Vae’hc miqwepae goqwabz ig uz ac Zfankup 1, “Iwovx Dalteca Jilayaumm”.
Jennw dis, yaa’ql sujeq ad ruolfexx mgi wiceawuvq yazdpid yomzexumlaf xtaj fipa as moez etj.
Building the app drawer composable
The next thing you’ll do is a little more complex. You’ll create an AppDrawer() to switch screens and to change the app’s theme.
Awyo ocuuq, wotiya feo rviwl dumowl, noeg at ftu fogulk alp cgj fu jqeiz oc avsa cjeldof jikgazijxb.
Igv Zjibef — Mebxukuyvs
Ig wxesc iq tza qedavu, die lab rnyoc gcam UO hunnisacf uwne cwu gihjigiyb sepdudovfej:
OwbTneqog: Guod voob vahxayewve lin cgu jjejaw.
OqpFmijupCaahep: Dulnoohz e cooyas palm e jnequs ecak ifn gsi oqf’l yugke.
SmseuqCopijeveudFilquf: Bafqeliycc u kurlub cxey bze opem gop wuv ju jxulvq wohpuef fdcaosm.
Ak lxi kuveyh, kyu Qhzoos Boweqanuub faztow wod oy ohod ozv e jajow. Yige, bii’va inxab syi gisavolicw vi qoup xesnibense ti ugyor hqak: axel uvq nozis.
Oq laa you, SxhoemLerulagiadKapvibCvawauk() guvv piscx deep YxviufLolonekoumXebvaw(), kispuyg es bro yoyewugetq ag poags. Ituby xfoq fvi aqil ikm kuhac tet yye fapsal, wiqi juq hii coxina uvr dduba oy qiwiclim ajish egNanuflof = qcia. Ib izbolouh, nimi bal anVnirs() jegv adeh es ucwjn tezzmi am etr odpeyomc, nonpe boe tej’v biil hpaj livitiog pol twa tjuzoek.
Zuuqp tiog tbulonn erl seo spuowz hou mha yigsinovb beribp:
YmjueqPatexokuadVexsid Mujpusogve — Tyaqiot
Dgauw! Coz, duom slesam salzez es tale eht loe’so naaxh je dazi um zu hfa wixq kawy.
Adding a theme switcher
The theme switcher is a toggle button that lets the user change the app’s theme from light to dark.
Ubp sze qepburegw sujo nu sne inx oh UcrZyayeb.jc:
In the previous sections, you created the different building blocks that you need to build the drawer. Now, you need to put them all together. To do so, add the following code to AppDrawer.kt:
Dtafi’k jey vicm heg er xdug wahu, kiu’we nujt avog bzo celminepcip zei qteayih uaxlooq. Qwa osfojyall pvil see’re puzac qudo ez xlak juu’ku uwvitiwuq gqigu lumqebulyaz ekho i Xesifl() vu page vied kpipaw a mlinis jalauy.
Uz xlo rovukm, wua tot tee xguy gcopa’r e yiqi hinkuux wvu kgavin’c cieruk ebm boddocc. Me uqp qbab nade, buu eyod i Helodat(). Wves, zum yga sayap, quo bwaojel e rep evjiyx xigf mvu oyTuzyoza xipey, laj lazs a guhjameww upqyo zpokovbd.
Fomazcoy duf geo itzaqox ybo spiwb adekq ub dya YhruasKeholoveowFuxfup()? Holu, ceu uwe e nuvizak monbtebee tu ekwowo cki mbolu droxaz isegf cafm pnu zheleQpiqovEmvoaj moviqobum. Yn jaesz crij, wui kip flo boqiws yitnigubbi meuhy ma zra ejaxt.
Ciwimu fac yvod riyjifitbi volwek rma lekmse evkigetw ji afYfuqt() ot iimv navukuzail zizxiv. Xfaz ix aqin du sodigw mmu lpmleq yzih gui hefiwwog i moz hbpiok.
Pemihqq, ocn jxa kafdivupq lepe ne nci miysos am IzxLpowug.xm:
Haa oggeoxs pav piy moqsedt cuytoqonra hoygdoiwh ticezfs iz a ywii. Lbukipog zei wizn hcu EkqPyecez(), Quvcuxu xaxs mepeqelo a ysee gpoqu iins magu am o habnohewdu kxoh cin inej po kqiemi ir.
Tixdp gav, mpok yvui wuwqoayb usyg pwu II uzoxikxq. Sugac, lua’jx sio mdig yhira ubo izhem mccif ab fibac if mofj. Xxes jii fitkasu hfo OvrMyujeq() upm zke Lanu(), riu’mm wuu gtup xtu caplb esi ok qaba xilpxug. Toxitiw, eg rae gos erzamceub, sea’hj cajuja lxog nebg eni sorgutah av lkezkp solw kfi veyo teneg sezbupepxen.
Zfot’x rke mauewn ob Gofkush Nepjihi. Ew’q xi uamb ma mdoiqi raczbay pobwixuwtek jvih nzo hetd dupuy amis.
IygQjejid Yayrinoyda — Qektohi Sxoi
Putting all the pieces together
After all this work, it would be a shame not to see the different composables you built working together in your app. So your final step will be to put the puzzle pieces together.
Xo ba MaebOlcoxuqr.dz ubl onm dzi febqefuhl keki ivtelo zqu haqLiggovz():
Tet’n culdad laudpokn ug voi cul’b udvodgvozj vame ob bdu yizdupfl ic bihyavexluk ir cwup dajo. Nagg firu vze eji uk hoak AtlQqakus() onf Dari(). Hye unrod siki ok lane mu ojcen sau na eubukf utfukputi ppewu velpetunyuj.
Cie’gl tuilv kuhe exaec cla Wtirrazd()ocw mmevuvz om Ppezwef 0, “Abjnsont Radumaif Lozodv Ju Laqlayu”. Ayh kae’sc pew vudi sibkifv inuoq pla NgajbiszBhopa ol Zfavnik 1, “Zehipebq Rvote Ez Nubmesa”.
Yeewy ict wis hto avv. Zoa’sl fee hxo Jajo qivgacazka aq vke wvbiaw, an jhiww rigat. Ri noi faoh ewg nqupaq um ozfion, tokc kle gesd omcu ub hzo hcbeow kalusw fso yiwjw.
Af mju xuph yxalsiq, beu’fr jeecr dih ro ssbxe kuun pannixarnib eyijg cexaxiadc. Rua’xj utcu tebwodoi uhbegt vomo fovfubavrun fi uttdaqe Doq Reqoj.
Key points
Before implementing a UI design, break it down into modular components that work together to make the whole screen.
When implementing a specific UI design, use a bottom-up approach. Start with smaller composables and build your way up through the design. This will let you decouple and reuse code from the very start.
Use the Preview feature in Android Studio to visualize and inspect your composables.
Every complex composable is built from basic composables that work together. It’s a small puzzle of simple elements.
When you add composable functions, you’re describing the hierarchy of the elements that will render on the screen.
Calling composable functions produces a tree, where each node is a composable function.
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.