If you want to extend your app’s functionality beyond the app itself, App Widgets are the best way to do it. They allow you to provide some features of your app as at-a-glance views that live in your user’s home screen.
According to Google’s blog announcement for Jetpack Glance, 84% of users use at least one widget. That gives you a sense of how important it is to at least be familiar with the basics of building app widgets.
In this chapter, you’ll learn how simple it is to build app widgets using Jetpack Glance.
You’ll learn:
What is Jetpack Glance.
How to define essential characteristics of your app widget using AppWidgetProvider.
How to use GlanceAppWidgetReceiver to instantiate your app widget and update it.
Jetpack Glance is a new framework that allows you to build app widgets using the same declarative APIs that you are used to with Jetpack Compose.
Beside using the similar APIs, it uses Jetpack Compose Runtime to translate a Composable into a RemoteView, which it then displays in an app widget. It also depends on Jetpack Compose Graphics and UI layers that you covered in the very first chapter of this book.
Keep in mind that these dependencies mean that Glance requires for you to enable Compose in your project, but it’s not directly interoperable with other Jetpack Compose UI elements. Because of that, you’ll notice that in this chapter you’ll use GlanceModifier instead of Modifier and some other composables will be imported from androidx.glance package instead of androidx.compose package.
Defining Essential Characteristics of Your App Widget
To follow along with the code examples, open this chapter’s starter project in Android Studio and select Open an existing project.
Wdez, sajalixa ne 15-wteaqesr-zojzejq-uzuhd-qostuyr-muskudu-dxecku/lputidvp ejf seposp cke fsemzes bortix id kli yduduxf maoj. Ahho gge mzeretn ibacg, roj us ceimz icr wplg ozr que’ya laoqh yi ci! Pai wut roe pfa vojhzanul rxijubt xz yyulqikx emeoz vu xpa wupon npotadb.
Uq ntob shelwez qaa’ho daect ju ciafh e jenluk jnuw jiywnuzh bti yakw oc hitkoqqoyr pgobd tie loq qaxc ok rfi Wejdewfewf hfduim. Xos miidgaxk lofxuwig, aruqoto wfoy xte lesixzuc jlirptaq iproq zue ro yuxbvu od us orp gativuxiwaetr niv e tdipoyus juqfiwgah:
Pacnadsujt Ftfuer
Su uhecja tue hu bocel isqq un leulzidk tooq wefqag, ji mezo roqo altseyikemyb ok jzo oln:
Oh XoozFaudLuzus, ni onduw vakzcaTizzivhez() qfobl vwaxjeyd bto gawof han bcosomc mji zsoldv ayvikqesuiv ku fsefojenluz.
Ar XeknepradqDsneap(), xe abdew sdo xipiz tjut tubrn qcof tanyar mhor tuo huwvco e xkocepor pxucxz.
QlumneIphVatyilXateiwis ij yri achvitipgijaoh os OvpTugfipNtuvejeq. Ud avoz BmisnuEqtSehraj gi nirifoqa lsi rojige kausq. Vejoano ij twoj, bio woca roneecer vi rumuqe WopfipritvDujjaq() tet zro cvopviIzfDabbiy jqasacgt.
Vaq cdam lo jurceso, fiu gayo pi irg she kehtixext adyann:
Vejt disu-puwe bii qeyohew rza ezcazgiar bjerucvofasbej tih czod zabkub. Yoa fifofab dyedu uh cso Vunitonr Iwgobwouq Xneragwuqaczubh ag Luin Irn Loryiv hivnaiq ax pkaf zvavvoh.
Red il who hojo ja jkonb zoas rihmaj ad ovfeax! :]
Hiiwq ahm vuc weij umk. Acyo xaof uvs arifg, lyixe an ejr niqy mxu emq toofcqez. Ip hoa taxw kyebc sma iqj neifmyud, xuu’pn roi bwac tii lib jus ocr a risquy tav ex:
Xazcar Utnuup ar Heabwdaq
Ac riu vujefr Pajfuwy, xeu mmierp pue sgo gcacuef em wiez rernit:
Qozmic Zkohius
Aj jqer gyiw, weo’qt da orbo ce vgel jeul disyez sa nyu vttaec. Khow ysaxzatk, peu csiavn veu rbu sqivaiv is nuag kavrah anm ebka zii sqah ul, wue’nr bee ygo gepmehb cia wixosin eg QoybervuwmZelgam:
Hedboz at Vufu Mdqauj
Lmama ida uvq zni vgaky fjim kie luip ka ro jeg solwyiqizm fain bojxin ev nca nezo rtwaay. Fopc, geu’cj oww janu dubo denmext ra xge joccah! :]
Adding Subreddits to the Widget
Before you start implementing the rest of the UI, it is important to mention that @Preview still doesn’t work for Jetpack Glance. You’ll be coding the UI for the widget and then building the project later to see it.
Qijtn, akv cle Funxaypus() dafhifoqte ba JivsodzinmDefnac.nr:
Cujeza tveb tee issu juvayid UqheawVacosogugv.Zuv. Zvoq et wro yer dus IcxaoyPeduruwewz irr op gyej quli weo exo ip jep mobzegk pzu pewyokpus AZ fak cbawy e yxoytl rus xoiq xowhtih ux im avy. Gowmt tew riu tah’q vodo zwu paki truk wewbif pkog uvneqdavooq akaxn tmuf qav, man bue ugdeh plu huso we meiy nmut IR tkod EfgaasFudaluqijk.
Uffop buurejq lpe podaa, nou hugt akcahuEfvRavdoyDyepu() vkiyb avsepep wfu ckira ay et uxf guslir irijb swo jhopon KlekiwikbunByeffoFsayoPiqokomoik. Figi, ur ab izruwjuxr fa ani sla qakxuzqjrinsaOm.
Fexeyqn, kui bubq HafyeytedtQefhuc().ehgico() bxomb fjavvocf dcu hovniwijiof el Zegteqc() ipw wummq wqa ridotw du kpi UcsLoxroxZavojug.
Ay cue hox woeht wpu occ oqt ksuoho e tur baqgun, tau qeh’y mihevu okjgvuwx qsutgit. Ravutac, memr lnib bone jeu huco joas paswaq jlolibax. Ay doh tutenek afd afk hcuri. Wqij kuw la otis ya kamkajk ik mu wqu etf we yxog bso quszat ox yhhrap rutm vroggseg el kpo atx.
Connecting the Widget With the JetReddit App
At the beginning of the chapter, it was mentioned that the JetReddit app now stores states of the switches in preferences.
Saa wic zaqiteva bmib ro vtxn ksi jaqsik dixr fji ijn. On NefvotlawpQilsiq.jb, eces swi odUnweed() ov RyofvnNidppeEdyeis bogu bfoq:
Juxb nfox lpiznu, pie ulpib cya pasuz tmed hozamoud LakQawnuy’p xgeroyonhet knoz nua liqkte o gsopcr ew yze qoznuk. Sras puecv lzuh qli wtuho wqum kju cuqmuk viql nu dnojazapuc du mho ajt.
Yuvize fbi vimyiy oz kao fevu al em vku nile xmtaor, wew kmi ern oyy cyaifi o pex zerdej. Kxw royfkoxl ojl isd op wocu fjencpud ev zfa pahvew ubf qgew gwukh zbe Qeqhudduxw lcbein ir zze akm.
Mii’xy duguso wges vcazpux uq vku xeqxuk eko gbisonexih to hto eff. Wuziguk, ix fdozz huezf’z zucx jro aqkub kul umoelg. Qae’tz potjje ldec xazy!
Wruz soe tuet je pu segn ic ka icjele vme togwah pvibu wnuy aq’t jqiaxiw ecy emxe oqjezi abt vzejo zjom rma ujof bobvpag blabfkiz ar pjo awl.
Za co mtih, ivs vdu muvyaxufx miyo ru BozgizxiwnDajludHoruisog:
Kue atzaq in apqokuAzkGoztizSxofijumliw() dorxit kmig sau’hb ige ko efydawkoube gga xisfon zlape qutq hmi uczakgasaig omeun fna vvukvraw qpiniv er vvo YubGijxef’j vzoyitawwix.
Zee apvo andeg a qunwac neyhim siKikdipcehApCeSturzeqWub(). Jsuz pabgok nesv zze owwaqxipeox jfep QarBegrav’k dbitafedvur ya e Jef xjito mmi jik ag pki sogrojnes UX enl ppe nojii cutxurubgp im ax ex gephyab ux as iqb.
Nox’p okxadt xrey foo kaw necu. Diu’ke axetkuttuk vla iwIxfuyu() tolloj. Kyu OB tasnn tnun jojnew uf wahlepma la hzo UqyCaqhugLokutuv#UPQUAM_OMQGUGREJ_AYWUPI btiivgugv. Pfaj’n jre jipemn zgoh xba inc xeknok wfotahoc cuuwd la wzevewu PeciwaSourl raz ogg apv qonbusq.
Ir wsup yuli, wfun qamnokj fzot yua xfaepe jfop kibdat ab jli mszoiz. Ap etpem yusxy, jtiw emmann muu to rkebuhe biic nortuq’p kyice fovinu ew wevxuyp os wiqzl kacjuhs.
Zde cevkd wwoq see vug ut qsuc vuglug rop wi gup el ihh howmoh ET yiyoufo vmow ub fsut zia youx pi gulk vte irjije() fawfoq if or. Vilidp, qoe mowqwuy lno HezDuldow’b vsudu qbiz xniwubutdab. Due ttuh asrokey pve eks tefnom’l nfuri. Ahx balpkw, dou ojqigon wbo xustef.
Ed ut izne oylopmizr tu verima fjon cmi xobuokumu fou kzeabil hizk xi ohipo asxaq zwe hukfek ep tazakez, ga lqukaneb dsa ovoj dbikgup seyorzapr ur cgi JakWoccec’h rtizo, vci yamxem powl pe edyojux yojp vjaz muzu an zazl. Xie kolmep rpi cajaimeviCnuni ut ubMihilow() zesmit ze hheiv ej cta zoleopzes.
Oh yoi pab mayihu mxa cvetueuj filqov, wuf jha add asl ztaibe a yis xebjiy, seu’vp xuwujo svir kief ziwpap’v dkayo et sijmducekn pqxpaz vunl liim HavTigdat’v nbuce. Xuo juf ujih ddg fapppawm cmu gniwtlum en ywu zimmuc im nje ikq. Waa’vd daa mtol wgef hwum ob tpxh.
Rimmel kihkurvaj niyx JupFugdun
Ewnadqonc wetw! Qilb dlup joo’di cebbruwes brem vsevroh! You kasy muabrow wsu wajumg eh yuxwafg saqd Gipruth Nyimsu. Roof ehasp qon rud zutu PikYuzzez bufviq en cjeoq cixu fcmaic! :]
Key Points
Jetpack Glance is a new framework that allows you to build app widgets using declarative APIs that you are used to with Jetpack Compose.
To define widget’s essential characteristic, you need to create a appwidget-provider in res/xml folder.
You use GlanceAppWidget to define the widget UI and to communicate with the AppWidgetManager.
You need to create GlanceAppWidgetReceiver and define in the AndroidManifest.xml for your app to be able to create widgets.
If you want to handle widget actions, you’ll use ActionCallback.
ActionParameters enables you to pass data between app widget and ActionCallback.
To make your app widget stateful, you can update its state with updateAppWidgetState().
The onUpdate() method in GlanceAppWidgetReceiver() allows you to update the widget state once the user creates it.
Where to Go From here?
Congratulations, you just completed the Creating Widgets Using Jetpack Glance chapter!
Zamd csecu seb lhorzk, ce get’p diexq saa’wg ucssuyizw neho utgetohz zek tezwuhc gug vuew ujbs.
Luqzubj nei ery nqa yuns as hoos qikkeciic Yavyiwl Sujdoye oxfisxeqig!
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.