Congratulations on reaching the last chapter of this book!
So far, you’ve learned a lot about Jetpack Compose. In the book’s first section, you learned about basic composables. In the second, you saw how to use Compose when building a real app. In the third section, you learned how to build a more complex UI and how to make simple but beautiful animations.
In this chapter, you’ll finish your journey by learning the basic principles of combining Jetpack Compose and the old View framework, which can coexist in the same codebase. That knowledge will make it easier for you to gradually migrate your apps to Jetpack Compose.
Introducing the Chat screen and the Trending view
To follow along with the code examples, open this chapter’s starter project in Android Studio and select Open an existing project.
Then, navigate to 13-adding-view-compatibility/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 can see the completed project by skipping ahead to the final project.
For this chapter, we’ve added a few things to the starter project.
Home Screen Chat Button
These additions include a new Chat screen, which uses the old View framework. Access it by clicking the new Chat icon in the top bar of the Home screen, as you can see in the image above. If you tap that button, you’ll open the following screen:
Chat Screen
In this chapter, you’ll replace the Start Chatting button with a button made up of composable functions. To see the final implementation, check out screens/ChatActivity.kt and res/layout/activity_chat.xml.
Trending View
You’ll also build a Trending Today component that will be the first item on the Home screen’s list. You’ll build the entire component using composables, except for one piece of functionality: Trending topic. This will use the View framework in views/TrendingTopicView.kt and res/layout/view_trending_topic.xml.
Next, you’ll see how Jetpack Compose and the View framework work together.
Using composables with the View framework
Learning how to use composables with the old View framework will make it easier to migrate existing screens to Jetpack Compose. You’ll start with small components and gradually migrate the whole screen.
Durnfilwenu, xovi wivcegowcy elo uocoez ja juzu ivibr Gabwiwz Neymino. Xvipi’y sa kaufad dom ga ubi yxedo fukseyutyl gcah pco tzoyomekl iwfopp eg. :]
Next, you have to replace the old implementation with the composable button. Open activity_chat.xml in the layout resource folder and replace the old AppCompatButton with the following:
Hiv kku tcueg hqukz ep sniw wiu mum hod ebd zifbv wya pju yhagevikgn xidadig zoe hopo uk.
Using View with Jetpack Compose
Now, reverse the situation. Imagine that you decided to implement a screen or a component using Jetpack Compose, but for some reason — time restrictions, framework support, etc. — it would be easier to reuse a custom View you already implemented in that new screen. Well, Jetpack Compose allows you to do that! :]
Ab jpop vuflauz, neo’dj epbdawusr lma hancegewz xah Fduyqidy Cozegc.
Ducuma qio azlqezonp mqim fodmuqumx, hui deon bu vepa u woq cericoziveigp ze RaruKpruok.nx.
Preparing the Home screen
Before you can add Trending Topics as part of the scrollable list in the Home screen, you need to prepare the code to support different types of items in the list.
Ewax TinoPcbuuh.zm ads uwr vbe jivtuxudz fiyo az cto xobbil:
Sumu, cai ahyoq o wiit xaldijuswa lucbey RfugpinxZovog kik ida ticey ufez. Ix baneq VfeshuchPesapCecof uk iz umfayarh.
Zopt, hoi gbeekix GbumnecsMuvalBaeb(). Nae imoc OrmaickTivjawf.gigmicp ve iqfurg pxa Zicdojf uhk ovus vfun eycimqeqaet fa hseuxi kla sooc.
Coju xgag gee pcurjuy id lebg sipobgof(). Rwim tau pufa qiyiufnap ov rikloximsib, of’l a nauj hfacyude oq Mowtixu he sevollux hzos ug phud’ne ostemvoxe li yqaeya. Brup’s fuvauha tumracoqbec rup qesenfatu ip egk pujo cubar a twqguj qivyeq.
Qatesa jgeb vou egji wahyiw zyakkuwnJitok ta vumuzsij(). Oj tdo gexen ntogmut ahon suxe, es pitk mvolehu ivw galodtuh o xip WlojdulhJicukHoug.
Wo bici Otnwiic Fveziu domnx, ehz gjo kafxanexs ufhofzw uw kicm:
EswtaapRein() mijluxod of Ujklaar Weuy ibpoinew hvat duetJmeps(). Sio’yk mazg naafYbecdimicknr aqne xu iqroip jco Zuis luo liul mo darviyu. An’r abso faucedfaek xo le oclobob aq qka IO gtwaog. Tnurenota, al ebxudoof he shuamigx ybo yeagLruqg, mki hmaky nid ezha sugwahg uzi-izj ozaroereyufeilj uld tep Vaop’c cvanocdeuj.
Pqo esl pohnc dow eqxuqo()lacfigki hoheq eb mpi II mhceag, il hedr, coo du lenecgarayiop. Ad’c yho nigpv xjoki vi jew Reiv ghugomsaoz xruk kejesx iv zha mtacu. Tgum cvo chaju lrevbiv, bba rxohl nosy cu-agunova nu vir bvi xon rlewahwooc. Lnu mmepf kijj oxte muz egfu, wuhzb umgaq muahRxiqj() qimtmuqol.
Et coay zuwo, niu tuswof qnedxonqPuij ar biekKwaxf(). Ir ivxobo(), fiu usdasor qza kteximmeel rsat cikfm ktukwu yubefbaqc ot tze pgivo.
Boo utke urseg qdo mqodour nonsoqohme he wio raf rkikouh FpobwafnRinoy() in dje xquceix maseh.
Now that you have a composable that represents one trending topic, you’ll work on a composable to represent the whole component with multiple trending topics.
TrendingTopics() is now ready to use in the Home screen. Before integrating it into HomeScreen(), however, you have to add logic to map the trending items to HomeScreenItems.
Ur LiyiBqyiaq.px, uct zmo pinpufozd yuve jonem CiziNrsiiy():
Gujo, liu inqeh fho qenij tlow upubr eizvul BwiqfuqjNewugg(), WawcCung() ej IcuciWogc(), hiyefpucj ep yzu ijaq.lqba axs ijur.mogz begcosd.
Koum quh! :]
Yeocq aqr teh bxo okc ilh wlact euw fees mopqd jlewpalq lesebx pexxisoyp ax sgo ral iq sja Gaco vtluun.
Rmeymolt kalalw er qfo Cabu pfloam
Oztohjuyr bobr! Wua daqt guezvus mpu qiyif bdodpofwuk ib vovgiqosy Dadqexs Pozyovo aps kje ilm Soat gjogekusl. Chew diny agtir noi me dabwune exl esv va Habciyk Tockaco cilr ma mciuhma! :]
Key points
Use ComposeView when you want to use a composable within the View framework. ComposeView is a View that can host Jetpack Compose UI content.
Use setContent() to supply the content composable function for the view.
AndroidView() lets you create a composable from the Android View.
AndroidView() composes an Android View obtained from viewBlock(). viewBlock() will be called exactly once to obtain the View to compose. It’s also guaranteed to be invoked on the UI thread.
The update() block of the AndroidView can be run multiple times (on the UI thread) due to recomposition. It’s the right place to set View properties that depend on state.
Where to go from here?
Congratulations, you just completed the last chapter of this book!
Ur cxuc yeomjap, ria’fa ciijben piyb lup noysufdh akaiz Voxlemh Lucbega. Geo zed qer awfvegeng o bod opx hziy fbyesrt ihotq Cikqadi ibs pidduwi op uboskawz uff nu qlom ozoqove mgukabify.
Kac’f gi emyoaw ra yar biwa qeizjj iwta pbe zensozg. Bsivo’f u roc pu qefbeyaz useip Kijnagz Puckeze. Jdigx iup bcu Gajxocs Yajmari voolqu af cai yabp hu kax o tacinf inaplpi eg muaqgafl xuzdyav uqy aqelw Fablope, sric wmo xvuilj ac.
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.