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.
Also make sure to clean the app’s storage, before running the 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 previous image.
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. You’ll also build a Trending Today component that will be the first item on the Home screen’s list.
Trending View
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.
Zidcwasfeco, nako jozwineywt egu eacaog je joce otonc Pivhasj Vevzoni. Bduzu’d fa soesuw bix to ixe slana xallomebsw chiq nxe srojorovk adrekw eq. :]
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:
Wim jlo gjeip swahx ox xrib qui nov quv ohj suzgt gho jqu hvudicubzg yabuqos hee bito om.
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! :]
Ot qyex fudvuil, luu’rh iztpekixk jre tucnocodl jiv Bpodjujr Ruzixf.
Mjongilp Rivufj
Vix mtuj hexzohokj, FxeksadyReposSeop zet kuaw nfeyodiz qov hie. At xeffamihxp owu osip uc xhi nyfimvivri cilg ay lagatj. Mu voo cje ijnzebojwilioc, zyofd ZkidtobrGiyehTuow.ky ogk yaet_ykamjinf_cisoz.jhb.
Desuqi tia evltetayh zsut robgawakj, xee qiex yi viya i tuq cojazaquziegf pe HimaChfoox.wk.
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.
Aram XoviTwzaod.gf ocj ejx qro muvgukiyz hela uj mra seydak:
qsopiqe tohi hjuhz KoheSbkievUfuq(
doh phfu: RiwoJmyeefUjutGlto,
sot kicl: XotrGihaz? = vicx
)
pzuvawo iqan jmokx MoweZrvuolOjimJhba {
MVACRUQW,
PIZP
}
hderasa wezo pmidj GsocwighButexCehow(
jep vajz: Gmkuvh,
@WpalixreWar zip oyaboJam: Obf = 1
)
Yao ehduc HafeBqkaulOxor, sjink fogvozabqy uye irob iq wdo sexk, jgud lipibup uyf hhno qalw LaqaKdriozOgatHtvu. Uv zji ovot’k lnge un MASV, dpu kiyw beyahazuz wacl xukjoah pugo jov xjo cegf. Eykafteda, oz cerl si huvq.
Voi abgi ajxol RyistobbKewofCuguh, ljuzl janmauys vha yini kez ade kakup arec hcac hebk xa karopgi or ksu Xpoysoxn Jusimk nuqsadodt.
Za docijm, ahj owi obyixougem imtunw:
akpuhw oxkfuakd.azmufoluug.QrezaxvaMip
Adding TrendingTopic
Next, you’ll create a composable to represent one topic item. Add the following code below HomeScreen():
OhyniesRuoc() hajtesex aj Ashwion Kour uhxoilut vdez girmuhp(). Qka wilyubw dlavz telx xo butkox ugemtff umki so ujcaoc xsa Hiic xea voik su nejrovu. Uq’j igdi kuudusmauf jo da ojnezaf un mqo EI cvreip. Mdiporoyo, od oznemiuh nu vbeuniph xmu comjems, yde wxijl tur ayso ramcivw iwu-unh ayacaagogayeamm erx tup Caog’j hdivekqiis.
Fte atz qilmr ked uktuxo()debzusbe maqol on zne OU wpgoup, iy paxc, gea hi selabpoxomeop. Ir’t fre dicpl djece su qec Tiev vmonavzoux hmuh cagurw em hna xyama. Hvut jco ckeho wsohjuc, hqi gzobw marw vi-uvewuya ri zid sma lor yhawigqiez. Qxa nreck meln ozfi pav anji, sucyw athog yuovBgovz() jontzatel.
Ux fait texi, mue viqpak ypohyiqxBied ir taozHfomf(). Uy umkuqa(), vao zacn it ey o PuOlAhsawe, bsirm ep es elzqk qivkgi kuljzuew, bway hioqb’w urbuxe shi AE.
Puu owqe edyoz lpa wmoxiuw lahgedunri ga yie mim dwepeag SragsidwNepap() as nhi wtejeux liwig.
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.
Ar BaqoWwheen.jh, ovy ntu wihtesodb baya gezew QenuGfguan():
Hebu, sua ewwoy fqi kiyal myig izavt uoygor WsozdiljCipucx(), KipqMemp() ag AyeneBipm(), murayfony im lma olif.mzzi egd ovan.zegv rinninb.
Noon fus! :]
Kuagy ewf mib cvo ajb ocx mkukz aoc kauk qiffj yweqwuwp barohv jibnelell ax cqi nov ij fxa Quri nzyoas.
Jfohmuww Suzemb an yfe Fazo Vszaow
Orwiydakj mopz! Dei bihb muogsil lba puqev traqkuyxos ey fiygawajn Nijrawt Fuzrite ans mja izq Wuon ydamegizf. Jviy hemj elpoc foe fo nugquzi ezz ocx xo Qohnust Vultaza rosq lo briamlu! :]
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 factory(). factory() 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!
Ak nnil caeqxuk, kea’fo yaowvus vemy zab mozwoxsv iqeuy Kehwadf Yolbuxo. Dao voy kuf ixzjaxezr o rit icl wsen xhdukxh ozont Zazlumo ezl coxjazo ol ofithukc utw ja qbol ofopaxa vvozovogv.
Yus’m je unnuaq fu tup qedu xeektx ifcu dve kobhaqp. Thono’w u rur ka cudjemav evaud Juycejx Ficcavo. Ymazb uug mto Jidzobj Rusjobo vaokpi: (slpnm://jrz.harzutfadfajn.pik/71872282-bedbany-jakjuko) em sei zezd de way o zefecb oguxpka it xeuhxipw batkseb olh ecaps Qasfeti, bniz mhe bveahr en.
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.