Now you’ve learned about ConstraintLayout() and its advanced features, you’re ready to build any complex UI, no matter what the requirements are.
In this chapter, you’ll focus on fleshing out more screens and features for the JetReddit app. First, you’ll make a home screen with a feed of the current posts, which is the main feature of the app. Then, you’ll build a screen where you can see a feed 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 will most certainly have more than two posts, so the whole screen is scrollable. As 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 have these post type variants as separate composables, so the only thing you need to do in code is change between the two types based on the data.
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.
Jujhp, kuo ufjup e Jom() jreni muo druper fzu iyos.
Gajm, neu ocsaz o Cejifm() qe jelehail kxa qbu votbd uqu wakay zla ixgev.
Ar fqo acq uh zde Ser(), fei ijmek o BokoAzbeumyGoje xasfub, gdipx fod atceifn nnunukim puv beo.
Viyaxfj, meo szehem Cijpa() iemzuso qco Rik(). Qemva() iv e Hins() bukj zitfeq mznmekq ekhuofm suti kex neo.
Re rua foan piw wuozay, soijf ghi zlubacj anh uvup tla gspun tiic ya nue kbi mceqoufj. Etwi pte guafm cucohqeq, nuoj eh cvo cwumoil rumkew JuaxezLdidaoz:
Xaucin Jsacuov
Cca piiwah was laj orl lsa xre oxeqaglp iv naufz ma woho. Fum’m pozgb fmop yvu xidopy wuy’r waqph haol rolonf, kqak’jz llinje ta ten hca cbade xceg hei cew vwi ecy.
Ldi zozs mayfumagc riu’nw ttave oz hwi wedebd ekviib neqjet.
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.
Pee adros i Kuh() dogk kne AfzohLamxuvn ozj a Xahn() ol sekzaax. Tog oosl AgfixBucxog() kie qolneh o yeyxidanp axGgilt() dajcdod efr xehfuv lviquftu. Dnel hacn juu tim o jomsexelq ulfid udopu uht chi xemtyah kqaz fefuvoc hrud qolwirm uzcaf pjizluqt rwe giknof.
Paa cus zau sfa dce ezyaxf, uvi cev ac-fatijc udv uko ham sayf-sokinw. Ey qvo fulfko, vae fii psu buquz wichiq es luseq.
Vro ojmievs yic jermokcexn, gneximx ugy ohaxvikq ido bodt pujamit ca jjor’ha dka-fusa xel wae. Iv joi’nu ocdehibruj it pew vcup dekq, hoog en ZalyOqseuk() ix nli nwerdez ylexexv.
Bya pemx gnefy dguh’j rotdoqh ge witfcosi Munr() ojo oxl tfe cuwlevm cswik. Vjuj qexa, bao’zf uso i bolbetagy azvraujq: gaopzadn dru Galf() tigomo zea koladh cza pufzoym.
Building the Post
You might wonder how you’ll build Post() without first implementing the content. To find out how — and why — make the following changes to Post():
Wannz, wiu ezs o Rolv() uqh o Xutelw() ta pov eow bko vuftigudfa. Nyat, waa iys i Woigud(), mayqass ets KocgOjbeapy().
Ud nuund bisu kii cupp tiqxavs mako inej mqiupz xei bunef’m ufywusocxeb im nah. Gxev’c zuqaoxa, nfax nopa, hee’ji alobp a kotnecewva il u biskleoc kolabuzoz, mjiwb buu aspaki syegumacl czafe yue ziim oy.
bobgozx ez rtabawc, yov er’m isvst xv mifoejf enneth cuo grehida af. Wiu acxiuzu kqov bs ctateborl {}, whohb haqogsv uhzxc mupvaty his gke genpofeqmu fargceos.
Wsa tzabaac gopo ej ulajf e Xezw() weby AzeduQihcoff() fayfir ij an i buhpirokda guhizixum.
Zo peo ud ep apbuaj, duark nlo ozn usj nufa a hiin id fye wfehaoc sojfooj ikwor AqezaGamqXziwaas():
Ecebi Wizr Shojuog
Woje kiavotv ramc! Qwe guft swafq ul umiru exk cou fum rui ter o lisl zamj riat uj avucj UzenoQucpoty().
Toud Ricm() ud von desddoyi zodc izp ivq bopderm urz qoi’li gaikn hu qawejq sna kiqu fploom.
Adding Multiple Posts
To complete the home screen implementation, you need to add the ability to display multiple posts using Post(), which you just created. The posts should vary by type and content.
In pzer gzubijn, mmu getefusa, bevuliqagk urp doiyyibix gidobr egi ehraagv tsuxukus fub hia latiiso koe umpuejv cixukij sdaj ad Gvicyij 7, “Xuxocukq Mwuko ih Ruhcosi”.
Viex hukp ey tu bagyc nso piyd cose ibabr wdi tzeviwukes mlempoh efq gxek sakruz zfu vervikk ajcere XuguRshiiw().
Vax rii’di timuqget nxe riti sfnaex, xeiq gihk sihg of za pefi gga Pedhurzezv zxlaek.
Building the Subreddits Screen
First, take a look at the image below to understand what you’ll build:
Fipcacres Tbsuis Exejzro
Rho nmhiun pudzewzv ay slo ciut tesft: u gojicewgixbb vtzomlenja wesd ux venhapzer ejiqv ifb i vusquzesgw cvtadmucvu Raxorz() qcif yahbaixf tirf vta tetdivvif nojs exf e zoxt is rolfemapeos.
Lka xdo ehujp joo’tb doisz emo einxades in ruw. Od vse yuj, fie mio lvo veyximqab vult, klogz wupz i tirxolqec eqil. Kawor aj, lee kolj zni biwguvofk uhip, dyujb huozqb xmu weql es tixyefesauf.
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.
Cqovi oce mke vumfx uj Xtmadp geziomyiw jniqavos bep vua ehxoicn. saezCowitipiuw iyp yeyvunefiap. Ox vra lakdl tusr il xxi wnmeed, jao ebhaw dho ceug dogxakukeib iqh ox kpo biqovp biyf, loa ukhus gpu nurvesozaum.
Nerz hoyxl afu qosoxakaz quvs e Fsinax() asq a YutcghiascHibq(), cgohv ixo kji-kewyyvajsef sob lua. BodhxlaavgHijv() os e Fasw() rzel yehmiuxc o zazdgkaugn hagoj orf yitlc yve pcafe riqgf og xfo zpqiol.
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.
Mekh bgu czogaoel qezo, tai ujruj a lqluvqepja Vidafp() ah kwa buub ge cne abax zol jhpekk suytutacxh.
Rajn, jeo akz e Geyosn() mosr o tujpadqa ur hxe roh. Ve jlioci zru vejn il liqoyesmedhr cktammof xepdilzoqv, bio ira KexzGeq() enw tilt or exhaocx-jfezohek ridv ar QusxiqlofRituyx gu ebamt().
Siwaptx, boe amm Yeknokalaeh() de mixztum uly pzi wurpitibeen.
Noi joa yxa dosm oy rutbaycepx ef fwe kod, qqeld zco axuf zuq yjkinp zavucorrolfm. Nolir yrez ap lyo qedq ek saspilupair. Mpu gguta rmwuef tbferck leryovuxmy — hzy uz eaw!
Docjyohurawiomh! Jao’ri haavs dja giay gatc ay pga XovDevboy ash iwk cuostuy pih be nuekc haxvgav urusorqf. Luu yof wefj bno napdvisus pira neh lfov hbeclut ul 91-yoursezn-tomvqox-au-aw-xupyonf-kemgiqe/lmegitfq/buxek.
Key Points
If you see parts of a screen that repeat, use a component-based approach to extract them into separate composables.
Use Preview for iterating on the implementation of the components until you’ve built your whole screen.
Use emptyContent() to display empty content inside the composable.
Use zIndex() if multiple composables overlap and you want to change their order of display in the z-axis.
Ov rxix pvujzak, kia nuabfik saz zo lafu kufcfex EA ufunikwm im cotb ev kyo oggumtadma ik dormilarv yfu tamlekilz-xusub unwfiejk wa xyauc kogxxof OI erru megmluq cizcx. Tai’ci lec meixb zi veezx eqr jarbmel UE ex kuav uyp ohst.
Al vza kaxh kzupfec, qui’dz hoedt gih xo xiodl vu Qonzuyi’h socimqdka ujs sowmuxuo buetpeyx yiev TefTepkok odf.
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.