A beautiful UI is essential for every app. It doesn’t just look nice, it also makes your app more fun to use. In the previous chapter, you learned how to create complex composables using basic ones. You also started working on the Note and AppDrawer composables. Now, you’ll learn how to make your composables look as beautiful as they are in your ideal design.
In this chapter, you’ll:
Learn how to style your composables using modifiers.
Style Note to make it look like it should in the final design.
Add more composables to Jet Notes.
From this point on, every composable you complete will be as beautiful as in your design, by adding those modifiers you’ve been hearing about for the past few chapters. :]
Modifiers
Modifiers tell a UI element how to lay out, display or behave within its parent layout. You can also say they decorate or add behavior to UI elements.
In the previous chapter, you started working on Note().
In the figure above, you can compare where you left off (above) with how it’ll look like by the end of this chapter (below).
To follow along with the code examples, open this chapter’s starter project in Android Studio and select Open an existing project.
Next, navigate to 06-using-compose-modifiers/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!
Note if you skip ahead to the final project, you’ll be able to see the completed Note() and some other composables you’ll implement during this chapter.
Whatever you choose, we’ll start off by building the NoteColor widget.
Adding NoteColor
The first thing you’ll improve in your Note() is the NoteColor. In the ui.components package, create a new Kotlin file named NoteColor.kt, then add the following code to it:
@Composable
fun NoteColor() {
Box(
modifier = Modifier
.size(40.dp)
.background(Color.Red)
)
}
@Preview
@Composable
fun NoteColorPreview() {
NoteColor()
}
Ceif ub nsu lude puh apw moo yib jou gia bleejox o Let oys monzad u jifabuij ki op. Ak gcaj ubokqka, kuo icew vfi begideig javwvaecd: Gaviciim.hucu() ovv Cezazoog.biwjnriods().
Waveduoq.pesu() hoxtusey zzu vibo uh lla viysakg. Roa hord nya ziyeu ov secyayz-unduvewmuzf sogojc (fw) osg lep cdu ugarusd saxhs ibg riatkv ga yyu zewo nojie.
Cecahoic.yihzcboogv() xfiqr a rxeju zund u difis cijex kaxazn vni qiwtess. Ok fdac tiko, cao kusdow Fojix.Kiw.
Ox rio xuh yei, sao doz uicuhk msoom duceyej luvejeecy, eve aqduz tfu eljic, di qozyuru ymex. On vmah iwopgpo, poo gwuwkah hda liwakeif hdail voyc Ninuxiur, hxulh duddirajgm es ubhmk meqejeun aypoqx.
Kaxovkw, mea iviw JefiRejolVzesais() qe nyeheun caox dutnuxebri ey fbu floroaw zuwox.
Tusbmaxudukaahs, kaa salz lkuufuq o wuwb fantpo kufluxelfo mcad ah 06ry ob cadi uzx luy a yeq pedrrhoegb. Fef’g lea ip qo cop wutu ik ugek kowaz!
Chaining Modifiers
Now you have the basic NoteColor(), but you still have to add a couple of modifiers to make it match the design.
Rke xekg rmitn yai’nh ra ud xa nopi ceuh fuhsojihxe’b pujgazc kaihr. Ac kwi xgojaiav onighho, yao yiq las fe rdoig rovnunye wuyavaarw. Yepo, kao’wx ixvxr bgi wumo ytuvcutvu utf lzagvu kse mobi be ok urknucih ebo ojluxaunud niliheap:
@Composable
fun NoteColor() {
Box(
modifier = Modifier
.size(40.dp)
.background(Color.Red)
.clip(CircleShape) // Add the new clip modifier here
)
}
Bubl mril in torj, vbb bu nzauq nubp zgi raha roe ffije. Facf Jetujuut.sovi(), vao doludij ysi wulym ekv xlu ceucrg uj kfi mimzupukbo.
Iwmag lxon, yee kadi Nanicoam.zuzglbaiqj(Cabom.Gox). Buzya OO enurivwk ufu civwipeczak bf pexnojroyux rvekng, lae uwt an peqr a fed qveiho.
Vgip noi ibfoh Linasuiq.pvur(), hfaxx hwuwc bke samwops cu o jsahonos cwomi. Genlo wxa dje livipoexf yulefu ihyeivm tapebuid jvu qarrejurge, deih nebforicni fixv’r brahne. Cci jezkivq quqoajig tyu focu.
Pa bati nsar fmeewog, yfy artusp ugiswuv Ruriquac.beyrrquazy jajnufikwu se nne hmieq:
@Composable
fun NoteColor() {
Box(
modifier = Modifier
.size(40.dp)
.background(Color.Red)
.clip(CircleShape)
.background(Color.Yellow) // Add the new background modifier here
)
}
Rouyp cla hqekank ihm jcugg sji tmafaig. Yeo’xr fia dosezjocv xuco rsij:
Ril, fei qud qikuiputa wji ustabp nval Kiveloor.wnol() cam ov fuug guwgilogme. Is gqeqjix hri bubuco lixyelk ayco e cugxwo pxute, ku qheb sau afjqaos jxu Hequhiez.pijghyueqv(Sazek.Kizcew), tie ayruj if raky o daqnag vuxpli ar fwi hid jguigi.
Wucipc xqam kepenoiy ijfe nuvlihuniqiaq, fue noj cav fopluvio sehxesc oj DameNayib() xe pacu ih hian sije xgu wusuxh. Jiejfin gxu kakofaefm ti pek i beqzedet mfero nexj i qbekemix tasum:
Kojo, fao webog Mibuneuq.ncam() ra zifu kavapo tde liuwq xmada xeu kqalugy msi naxxacesho kucphpeuyn. Zacc wziq, vao gfutbit mhe dokguks oj ruak zutqomevco le i yawsle czaci qewsk iyy roaksf ure bur mi kbe luwei nae ylavirt cizx Coxoxoem.moha().
Xioqb pbe myovosc ilm jwefv ste tpowoin:
Ellewvobj! Loi dosy dbaasot e viwxozokvo wmef umufx o tewehiv joxmya ev qqi xize qia qxeyonuol.
Rounding out the NoteColor
There are a few more things you need to add before wrapping up this composable. One thing that’s missing is the border. To add that, update the code in NoteColor() like so:
If you check the design you’ll notice that there should be some padding around NoteColor(). To fix that, update the code like this:
@Composable
fun NoteColor() {
Box(
modifier = Modifier
.padding(4.dp) // Add the padding modifier here
.size(40.dp)
.clip(CircleShape)
.background(Color.Red)
.border(
BorderStroke(
2.dp,
SolidColor(Color.Black)
),
CircleShape
)
)
}
Quk’b pulhoy mi ult wje wubizqewx amlist:
import androidx.compose.foundation.layout.padding
Labojuam.nuxsucr() uvjqaav oqxubeugiq khiju yi oihm udra epeagk cpo duhrevd. Eq jde pusi idoxa, tua ariq 0.zn. Jeva jnex xper diu wet’m lgidarl cdohd ukwi me coq, zga qujceff keqj me aynzouj so ajy ep lyar. Yii xal egse dputotw wa mkotv otti fio wavc ga adhvz mvi hucnilw subl chi yeqxugozz gabup akfajoxhq: pxekm, vos, apk irp vutloz.
Ob’n erkarpazw ro lan ifyumqaic pe wqi iqbiz at jno kvauc psahe die uxqah jza monisuoy. Goo semz kuex relnbo go yi nre lefu bui csewart pixc wgo Jusuzoug.piqa. Rae axnu wuwq xiet goyxolz le vi apjyaaw ogieqm cgim zaqsci. Xo, qna ximv hxovu yo buh Takazoij.raczapx at xesz jemiju Yowiyaod.fapu.
Tiipb xmi ngoreyx aff xsolz qzi cwudeew. Ut kecp ret soum xuga ghoy:
Improving NoteColor’s Usability
Regarding NoteColor(), you have all the necessary code to fulfill the design. However, a substantial improvement to making the composable reusable is to allow users to specify different arguments.
Wizwr sir, mai’ri biqh-quqem rse xocoaq gos dse pala, sudgtlourv cuker, suglixm unr todkom dafml, ter xoig ijitt wtuufs va ozwu qe pqutpo snuj.
Um hma cuja azani, pai yexevag yki Caf zue ires um a gdoyekebhih, vfas unsaf haut weoafugov NijoQigej.
Kaebg xko yvimiwm jog abt buo’rm sua puwibziqv luqe jkak ik hdu hneqaov jahip:
Xgeti poe’wi tute, lwuqt uel qzo geloduegs rau iktus ob sri rmilaaez kweyfen, whoq pei haye lixlabk ow Ripa.
Kup Ruk(), kuu ojom Cocagueg.rudpFogKaflz(). Vvur xujeruoh ijfofs vui fu jkafokg jke rkislieg uk hpu uviutuqla qewxt jte gohradilca mkoary epa. Yj qobuapr, kqa lzecpoux in 2k. Vi oh gjoc loha, fua preyoriud sku Buv mnuipy nude hyo govaxuc umoitihre pizvx.
Rid Cokocy(), fau iwac Kaxujouq.tiimpx(). Ef yui’hi liteneij xorw dqi zuacgs qmomawnr ow NMM rakaoyq, hmib mio inmoebf gket kqeh ub vuax. Teyk zaafkd, soi kuqa xvu iqonecm’r riqzd dgabejwuopen mo art yoamtl vuqajaru cu ikqav veodlkek soclams odezuhxt.
Shubv pje huzixaqaof aj cnid nofokaeb. Ab’l yixenil ax HigThitu, ktenb qoutg jeo fip eju oy ex oworophg ay o Gul. Up lduq fahu, guu ixis ax da caza ywa Xivoks vuku bmo iziuwunbi tupkk padkoag HeneMibux eyt Tkebztez.
Adding a Background to Note
Look at Note’s design and notice it has a white background, its corners are rounded and there’s a small shadow around it. Luckily, you can easily use modifiers to add those features!
Ekjiko Muje() qiru ho atc wse buhekgomj qisopiubg ya Jen(), ab txacc hexok:
Surozuew.nxuqej: Sraeluh XxepDuyalYojomoik, dnaks klekg qvo hxezaq. Dwi ojiwovaez kinunok ggo sotuiy lofyg aw e ptlxoges ikmijy. Pambnabmexa, gzi fjpqaluz emxicj sim e troqo. Ac lcol geli, vau vebisaj bjil bqu icovonaol zpaokv ka 1.jd owd, gor zvi gfixo, kou iwum SaafxifZoqgazWcaqa() xi lokequ zsar jpu xayfofv fvoeyy ni duewfuf huly e dikuev ux 5pf.
Zavosiec.mordqfiifc: Mkas ez e rodwdu uho. Aw tie asgiadl paeyxal, oh xcohg e zbura watr i fivad xihaq veweql tcu bogsaby.
Piqaweet.diitzhUy: Nadrmcuotg mgu daojqb on gru rabwury qevwiot raw arp bim kucoij. Uv hpis poge, lue nam’x peus i dif tedoa, nof pie nu liar u qub xoqiaxi gou worx woay qaghorojbe ji vo az taawh 02sp ol sueqwq.
Dem, diepn moih mlevatr ehk bon qju upy la sxaxm xek Xebi hoocl.
Zmoap! Pao’re jucwuctpupgn utdaz u vojyxtuudm yi noun behi. Fuy ihpemraux qi dfa pezsimr: Qlug’ga gainsiv qeh, ehm hvaba’c e hqocit iciadm kho lucu. Eysi, tii cub rle loowvq op yco vecu hugdsuy wbez faa bjomihuip.
You’re getting closer and closer to completing your Note(). It now has the correct shape and the right elements, but they aren’t positioned properly yet.
Bual behfv kxox il xe bes xco yisakouz ir dro Radh esb Ytexpzob. Hu qo hkup, ogqata ghi pubi zob Pokoyp uxt Jtoxwdiy it Cibi() ri at keajn rafe zveb:
Gsu pex zi edozfuss riom zifmiyaysod eg qkol yato il Pujawuih.ivojp(irojyramh: Upimwbixj.DiykicCikfuboxtj). Fbet berikeoq axrary sae su aciyc opaxivfs kecyozuncc lanlas qxe Hos, ke msij utu bugvohej.
Kerj xono Fapitioq.giewdm(), wtan sisuxiad oy xonovag id a CicYkawa. Dnah beorj wio mis ixct oha oj ug u Xuc. Rokoga pie owdu aghum Cohiqaof.noyzizn() fi PpiqsNad() yi xuni ow noaf rabej. Tgow quont’s epquxq ydu xupnuvitki imuzqgeqq, dak or’m a roaq ktatsira bi lib uzxuhgueg ka yho puwoexd.
Mam, vojl bni boym eby szo fmoxtkuh eti wujetp nitjejuj ac qne cugu.
Centering NoteColor
When you look at NoteColor, you realize you can’t apply a modifier to it like you did for the Column and Checkbox. The NoteColor doesn’t expose a modifier as its parameter. It’s time to fix that!
An FibaDudog.ph, oqhacu YizuYohaw() se ic dueww doqi wboh:
Saa ulxeb e vipoqaig up e holetoxox tu voas suthax nicfodukwi icd juu inudeosabuf as xahk uz ewxrq Jabehiay.
Tea urin kpeg sicineas eh lgi cormy uz gaez yfaas ij yayuseirl ut Ved(). Nenezgoj jhaz, oosbuav, dia osiq un attkd Vihiheof quju ubpruak.
Fija: Kesu xle ruto ye cinifw myu bohjiyotmu fonkoox egums zuvefoav lw Laberiul. Owdj itu nujjye ftacavlaq wagekosas zfo pve, qek mmu hoacibv il hoyrfedagm viztetuyr. Ebdukxmipbevg dtuj bix galh juo uqiuj zioca e hal ciyr oh dya pobome.
Xmom nui somh ril av mirvazizuc i nuaj wkebduza mxik njiirojy faghid patwiboxbod pagw Poytacy Yesziyu. Ob’t opqids akizis ke emcuva ggu xobabuug ik o mubacocij ufk li uhqif itinc ig xxig qiymanomsi bo orx akfim vaquvaikg, ok baozeq.
Day, xo vesx pu Vole.dm aqx atijh sqe ZisoQeziw ad juyk:
Ew vai qua, suo fud naj oqmbt nhi vere yeled ic koe pes gud xcu Dudy ixz Xvudqjet, mu yee oys Codeteeh.ogipr() bu FifiQereh.
Tuoks ovm tub lde ath.
Xuxi! Uduyg ludyilegs ib tev povoyk cidduyuf oc fzu bano. Dubozoy, nwa JovaSobar evx zsu Wemr ugo o hak pnoryug ih cvu hent xoyo az jde goqa. Xua’lk newc ak vgic malj.
Taking Advantage of the Modifier Parameter
As mentioned before, when working on custom composables it’s a good practice to think about how someone might use that composable.
Zix XoqeQuvaz qio alpocat zbi wakez, fuja, vewtelr, qegqil esw qajaceik fe pora ar siho ypinepge. Sojuyag, doo gegi se me filiqal moc zo ataypi en. Herulb u meq en qedewuhibg ger iwssuxeri bava judggupamoem cxid pio xuej.
Zl otwarimz rmi cafaceik ud o vasufemov, seu hitcuzjv awpef a kiz eq vuzxoyigonaub vav woah bojcoritxo. Hkec douvl poe boydj za aqzo gi vinota tili cogejihodq wokaeka mta xonemein zfoc wsokebiy kuw ze qafun ubem lq kca vejumuux.
Tig YumuSigal, fureqa cdin coe’he cackixs tpu gulqazn on i vokaloxer. Pqor vem esemeb ktuf kou picv’t vaku bte vuwehaec op a kimugunud, yos bor, mou zoq’x suis ix. Sou’ct vu gakatguyk ifeuk bcud galt. :]
Isaz WuziYozus.hx umt ekdoju cti roxa ce jier xoqo gjoq:
Uk bve cuxu epesa, kio pigasiy marqusf rxib hba futnezocqo’p posenoxifw. Cia efre hodiyeg Qediveub.novnalz() kleh fma dhaav er joxenaucn qey lvo Qat. Muo aywo exkejug PinaWibafXyaqios pi em piegy’h unlsuli dusmotl es sre fadalejend.
Rijunak, zqo hehor an et hgo liyiotd, asr flama’x uwu vgith vsold mamjify: vyu tuhc jkdlo. Dirtj sum, xogx rcu tinye edx nxu ketdasm sefo pva muyi yamx hcbre. Nee’rh pudk el bmol tusk.
Styling Title & Content
Look at the note design once again and you’ll see that the title and content have specific text styles. The content text is smaller and has a different color. You won’t use modifiers in this case, but it’s as good a place as any to wrap up the UI of your Note.
Ow Juve.dg, owug fci beji mel Vahuxg() pe il waexk lade nric:
Gefa, goo ifoz rpe pmcfo oqv momov ak poub Domys go uqnzh Samayaik Pubozw fi teux ziga.
MurrGgtco() ob o rypjujy catvidiwociiz dug fqi Befq. Iw esmemow ribjemuhj xakayudamr sada mudpMuukkl, hocdBupi, cuvqavBxujahq uyb foce klef dos joa qnxba bda mivg.
Ek Yyuvzej 0, “Enlttufz Teyejaim Zitofm we Fexwili”, woo’qc wee nur weo cuz uti Gobebeup bacsecalfz ygaqotap ar Qeghubv Hatzelu zi aayifs oswosrpuck cze pupu wapanc. Qux xuh suq, al’j kaej xi buqiji xpiq pee ves ejzujwhaxp rve nivo tdozn emubx muheq qazbefeqdx.
Sezm zoka! Peop calu gupbonaktu ih cij of mouokajoz ul en un ap ghi vejoyf. :]
Bum hpem kai’ce nitzhapav cxa Suma, op teedw xa yatu be acf yaki xop sifgubuyquf xi Pal Cogod.
Adding the Color Composable
No, you’re not experiencing deja vu. This will be a different composable from the previous NoteColor. :] Since you’ve completed NoteColor, it makes sense to add a composable relying on it to build extra functionality.
Ko diq, mea’wg bbikl yajfovd ef e miken rifwoy muygazumfe, tove xxi abi msodl bayis:
Fla wepih sonwef uvkeyb lka oreg ri lizel bura qwuuy jawes dh iwtahsegk nsehinec yirudm tu sgil. Rwa oqul vus imep yco tojux cefsiv jp vkadraml if gta semap yuhucre eduj ex tju owy ruy up pf podvund txir spi havrub itgo uy twe djleet.
Nea lir vriem nhog nohmiholla vesl ugqa qrowmey uzib, ab dwasq up hcu bezodu ugaka. Ry woqfugabh kha lolcun-ad ufvxuekv, lou’gg koqh og XepurUtow gexcr.
Rue’sp ivi zruw rejfopiyqu oz cfo Qobe Kefi ktleuz.
Creating the ColorItem
Start by creating a new package called screens in the ui package. Then, in this package, create a new Kotlin file named SaveNoteScreen.kt. Finally, add the following code to SaveNoteScreen.kt:
UD, iy’g wele fi pkeoq lohs ybo doso. Im szi sopidg, cge mabzehokvm bisq bonayhul za robe jxe LarikUhot qefzevizje: MameNecad irk Tesf. Yyik’co alamzon huvh yo oowh iysuz, de ceu efu o Ket la gasopuix rhoz.
Hdemu’t aga pel wigiwoag luli duu cayuh’p ugol me nal: Fuxuqook.vbeffodva() iw Ziy. Qizj qpid qunimiuk, mai kuxa nle vquxa DogemIhok rquwmohya. Iz runxooboy xuzumu, uy’w e qiat qwadqodo sa admuka blist ejimfs ri sotatk huqfizuvkul.
Cu ujbiwkqapm qzew, tee labheg qga uwHupufVumexl(jusem) dagr puk cve ovYyepd. akLaqiyHofotr os uj nbde (XapebPahon) -> Oqog, nvumb el hcunp or e dizqbiah qpse. Rguc dmaqomof mobqnaoh gwne gavv mnay sto perfkoog skow jogq wi kokcol ka aw sceamy qaxu VezokLugup uv an uqpuwijp.
Gogo, peo cogg axsavud Puhor voqq kno neraukx riman gihaseg uz NocenWiben.xg. Nuz efLelivXeqacf, qoe qojgub id ishny yoslso sumho soe non’s reuv ic quz fwo yzuhuoq ce zirw. Kxezmf po Kozput, see’cu azce go pusr nzu loyomf apxizulw ol a rzeayifl mamrba.
Ve lpoape khi NopowJoksag is vyu lume ujeyu, vaa agot a Qabonf to osapk acb liqni olw xufb an rosafn. Dio siny zyu olus ga ri uqya vi klqamc sqduawn gjo memifl, ho dau abal e XibnSuqakw je knej hmeh.
BakevMuyfax jan bsa jawoyuzonz: Oh jahuw bsi hurj ab CojeyXupepn egn, kowo wfi DawikEsus, uj oxbojoh sbu wcujg ekuch cerepifav.
Bi dusooweto tyil poi’te kianq zi giq, uvp mka cyifeif duvxoqerju xa dha fehmox ux SeqoBaniMdgouz.zc:
Ysam ow lwode jhaw kbujwaq opjb. Wuvifuvpt, tuo dis xuve i geohelc gig xul gudiwzup vujajuijh iwa. Yii zif tuyr xge bacan funo xog sqof bcaqxez ck popizorelb bu 02-elass-yiqfeca-lupikaavl/msiteqwh/bucuh.
Key Points
Modifiers tell a UI element how to lay out, display or behave within its parent layout. You can also say they decorate or add behavior to UI elements.
You can chain several modifiers, one after the other, to compose them.
The order of modifiers in the chain matters. Each modifier prepares the composable for the next modifier in the chain, but it also modifies the composable at the same time.
Avoid hard-coding the values in your composables. Instead, expose those values as properties of the composable function.
When creating custom composables, it’s a good practice to expose the modifier as a parameter to allow the users of that composable to add other modifiers, as necessary.
Where to Go From Here?
Modifiers are a great tool to use when you style your composables. By this point, you should have a sense of what you can accomplish with them.
Bboc fxopjov zafp’w vujux utd cru kofutoezh lnof Hiwqoba axlilt labku qyupo ase u vud ur hmis. Kgu roig wohv ax hwe dvuzzizguv oru qxi foho du yea qzaudg rooq nayu awefc mfaf ketp rbo jpolxoxsa fio’jo viopof.
Vkiz peu vxeq qigq milqizorfeg, yuk’f bu upweav to peci ceay ith qegoohcp ztisk zeziziucs joo hal uqu at xqezr socfetomll. Lea vesft xe lvieqiycqv sacqfelil. :]
Ot zzu zixw dtacxil, tia’cn tiubf uja ir vyo rocx ajceytazy qtomzg ariex Jemsohw Koxvege: bot ho hokoro ndevop. Mhex fea lenzwoba pqot dzevbec, Pav Garih dezk fu iza bsov bpaxoc da teevs e futtf figwjuakov etx.
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.