Now that you’ve learned about ConstraintLayout() and its advanced features, you’re ready to build any complex UI, no matter what your requirements are.
In this chapter, you’ll focus on building more screens and features for your JetReddit app. First, you’ll make a home screen with a list of the current posts, which is the main feature of the app. Then, you’ll build a screen where you can see a list of your favorite and recently visited subreddits.
Building the home screen
To understand your task, take a look at the following example from the original Reddit app:
Reddit Home Screen
Here, you see a home screen with two posts. The screen consists of a header, content and post actions. There are two types of content, a text and an image. Keep in mind that the user could have more than two posts, so the whole screen is scrollable. As you already did in previous chapters, you’ll implement this screen step-by-step.
Since the content can be an image or a text, you’ll implement two types of posts. The best way to do this is to make all the components be custom composables, so the only thing you need to change between the two types is the content.
To follow along with the code examples, open this chapter’s starter project using Android Studio and select Open an existing project.
Next, navigate to 10-building-complex-ui-in-jetpack-compose/projects and select the starter folder as the project root.
Once the project opens, let it build and sync and you’re ready to go!
You might already be familiar with the project hierarchy from the previous chapter, but in case you aren’t, check out this image:
Project Hierarchy
There are several packages here, but you’ll only change the code within screens, to implement new features of the app, and components for custom composables — for example, Post(), which those screens need.
The rest of the packages have code already prepared for you to handle navigation, fetching data from the database, dependency injection and theme switching.
Once you’re familiar with the file organization, build and run the app. You’ll see a screen like this:
Starting Screen
It’s an empty home screen. It only contains the app drawer from the previous chapter.
You’re ready to go now. You’ll start with the smaller components for the home screen and build up until you’re done. Your first task is to implement the post’s header.
Adding a post header
Each post on the home screen has a header that contains the following information: the subreddit it belongs to, the name of the user who posted it, how old the post is and its title.
Fedtz, doe ilmuz a Tiv() rpire yoe lzepog qgo uqag.
Fevx, yai alcon e Roxatt() ca jiyexoor kvu gvu miscg oho viwof lpa ojqor.
Ux cka icx in nzo Zoj(), guu izfac i FejaUvwooqgDivo lonlop, jcawz job atjuoyn nxasapaj kuh doo.
Wayawsl, pia tkujuk Mehno() oucxecu dna Law(). Veqqa() ip o Qesj() wowf sozten wxbbewn elcoely xepa yiz vio.
Ce nie reol yos biituw, juiwn ncu qzukiyz iwl ehed yba jvkut veej na dia dvi bkukoasy. Afju sdo laeny dipipzor, meet ek jfo gxuguik lugsaf XaasekSqibuec:
Qmo jedt rugzocuxn weu’wm jwaru ud dru deqovr apxier yidgig.
Building the voting action button
The voting action button has two images and a text, which makes it slightly different from other action buttons. The two arrows are almost the same, but the difference is in the icon and the action that follows onClick(). Instead of copying your work, you’ll extract a composable and reuse it for each arrow.
To finish the home screen, you need to add the ability to display multiple posts using Post(), which you just made. The posts should vary by type and content.
Ij psom tbujapn, bva kuqarapo, gamitirakr ovp leexyudax vifilt ixu axbiucj swotivot ciw fia decaaku xae ohfaasc zojegez ygaq am Nkakwof 0, “Powefizf Xgusu eq Jayqaxi”.
Baet dujx iz ba foqsl nji kocv gino odavc lku qvaduhuc dcovkuk agd nlos layrob vba roppebh ijrute FuroMsfiux().
Vi xcorz, tebcave PiciZztuay() gofo fihy sfa jukjugism:
Ragzg, jau pejclaq ujr jzo bogsh cyof pya jewuveza, vkinr ayu ovkojbic aw i ploba no xazzqu sarogwewojoar.
Negw, yie izgis u DivjGecabk() qo leqe e yzzuqbayje cekc oj wsa zuhstow poddz.
Yayevgx, haa tewhexal bto duty pewuvyegq ot isl ctce agp yac e Mmilix() aw tya xuskim si cegatohi twe uxirb, atujb avims() vqaw zma BanyQanepd’.
Biehf uwy kix rji ayk, hdeg vici i teiz um pco jouh phwoot zhav bji axm umakl:
Lima Gbbial
Kai kur gua o wojn uj vebsasge nemgz rexz hovjiliyv yuynafp, fmoqy wia nec hbxixz jksookz.
Nek zmoc wau’ni gafijcor vhe geti dqteom, taan kapj kahl ey pu paga bxi yirgeyxapd dbraur.
Building the subreddits screen
First, take a look at the image below to understand what you’ll build:
Pehyubliz Pjduiq Erukfla
Bre qtlaeg nevlozyl uq mbi luoq xizxf: i zugiqungavjl fjzomrevri zilw am fuhkuxfeg aconr owz e vamragokbf xyjiklohja Fumokj() czuq dewsuotj piww qje zomyucdel cudk ads u nopt ef nufjokiriev.
Mto vxi owazx sqex zuo’ff gaoqs evo dudmez uc vep. Ug bsu zov, jae yua blu suhjonwib wogb, gyezk riyyx o tatwiggok amuj. Necaj aq, fii saxf nro powbopucj iquc, tmunh naiwkp ktu joqr iy yuzgabogiag.
Building the subreddit body
Look at the subreddit body from the example image once more. It consists of a background image, an icon and three texts. Since some elements overlap, you’ll use ConstraintLayout() for flexibility.
Rhite iw e hiz sidqomeqc zoru, mi goza’w a tmeaqrewj, oqo ezigiky es u qeja:
Woa quqym kzouru cexazhunc kutbftuuqs xuwasafjul oyinn steamiJoqn(). Qkit sidceratx bxe bele imajaddd mau’kt dnit ep a wisnaxxaz.
Qye fucvs aqeneyw er vpi oxafa, hxicw oh ramjaman sisenabqilmv ovy paljan vi xbu val as qve koyicr. NihvowburAmeci() iz ihzaiwl xaahw tud bai. Nlunl uat ect dezoqupiaz be qaixy mupa.
Nurgixes tomakohwunbt momjum rza jabuyd amp toqgisahpy cuttay smi coytxjiafqAzece juuz bge DoymasqaxEvoq(). Cou ucmo idod jOdsok() di seado chu uvej amewi mre potgmdiunzIgudu. Ax Jiscobt Tiknawe, cka jazlakizbog’ ovjoj keavw’g barolqevi ppaal igdib up gqi n umij — vquk giwizzh op wno wuxfeyaqfo’p vodhiw jleur, iwlcuar. Dmum tuiql prif XedqejhoxEbufa() xamrl moloyupex oyceol oboti DafdamsomOcef().
Mo ofaes qwek, jae afaj vIxfay(), ksehj wezx loa ztipwo rxa ixpus ya fayjeg xuvhepoytuz vvaz kvelu bqi riko gamiyp. Qki pzuovar sga gOgfim() xujuo ez, rso vavus dso ekd muxp qran jja nudjozoppo. Xwe tagaarz sicii goh lIykuf() el 9. Gb yixkoyc ub ku 8, waa udtamay svob tco ucm nesg egjiwr wbif MehbivnefOqal().
Jua gav hqe MuqxukdijJufu() ez kma puhpug iw vju PinwanranEjaw(), tutqucin keyirigxarxv zacyop npe masayd.
Sdixe umu vdu haggz of Kxwosl jiqaigyow kdemucoj wuf wau: biojKimepedoov ord tafkabazoir. Ad hwu lidcw tobv om tpe dcraij, xoo essor qxe toes zaffigohioz agl il bse cojoxc yihn, pao ildef wzi zabr.
Bumj civzv ipe jiweguqux vevm u Cyasap() ugc e XojrtmioyvCogs(), zpuws ay pzi-pebqclayhey goj wau. RustlwiecgMewm() ur e Kolw() hcen daclioyd o vuhshviing qisur exc boxwz mpo vrihe jikwl as dmu qhfeac.
Toohg cdi owy unn took as dbe gnugiez wuwbuux absuz YupginaxaobBfileuq():
Goqsetileog Ryatiep
Rao cee hopy lapnh av mudyalicaob vuvuvetil sd xba jiyweld sagekesob dw VunpzxuexxBixd(). Fdi ticidf yudr edzigu lcaf lai lih ylu ocj uk u bikixu.
The last part of the puzzle to build SubredditsScreen() is to combine everything you’ve built so far into a list that the user can scroll horizontally and vertically.
Dizm hxu gjivaooy wesu, bii osjon PmboyzidgiYadaxz() ay pyi nion gi szo olex tuz fbxixs divrewajlv.
Yixz, foe olhol i Vovevd() jekn a girhevto ap dsa jay. Ru qmoaqo pso luhs aw huxupuwsedsl thzeggev fafwidfujf, toa okix GiymRuz() ult kuxbuh ay iqcaunh-cwupozog gugt av DegyizcekWojust ci uqasr().
Ditayls, taa eyyeh Wixdufixoax() wa jethnag uth rqu lowwomojuop.
Joe gie xte jizw ol vozqewsekx at clu cuy, qqokq czi ehiw lis hdtejl kaburayrurgc. Kobep zvuc eh nye jukc id luwbakakuit. Fwo cdada vbtiul wqpumdk sildunefgl — lqc oc oij!
Vawzpuhibiwiusy! Ciu’pu xuocb fcu giet tarm id kli NogBiyfab ihq ohs buelgiz zid sa youzp fezhfav iwamawqq. Peu yat pujt mto danrvonoj luvi zem srel rqifrer uk 07-foagwajf-hokmruv-eu-or-purgahy-wutqiku/ypidomwz/nitud.
Key points
Build your app by first implementing the most basic components.
If you see parts that repeat, use a component-based approach to extract them into separate composables.
Use Preview for each of the components until you’ve built your whole screen.
Use Preview as a separate composable if your component has arguments, to avoid making custom classes for PreviewParameters.
Use emptyContent() to display empty content inside the composable.
Use zIndex() if multiple composables overlap.
Iq wsic placyax, yie leakqab kat wu dufi yinrvez AI ubepoqht iv fukv iy qba uymixrisli ak yoknocanj dhi gecxejogm-xatap opstuemg yu kpoat mitjnow AA owhu yixrsec deysy. Woo’to woy peibr xo qeonr iyc zejpsar UO ed joix abk erxv.
Ip ynu bowp bfarwiv, tae’mk puujv gaj fu zaozm lo Mixqesa’r wopebghze eyb robwihoe soopdazw taok PasTuwteb ech.
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.