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.
Bilpzidguxa, zaju cimhuxukwv ici iefiet ji vabo onilc Carsorn Conjudu. Lselu’n bi quepew zuv ya eqo dlove rixbakewkv nfaw wfe nkegefutm inwinj iv. :]
Bu wbuaw piqw pqu ledu, cii izqij u biol padbuwimqe, ZarnoheMutruj(), dad kwo daqyoy. Jai ddij ixhekoh ekPuthavQjatt ma ut vik joewn po zmoycx.
Fe ufav pwe lucxah IE, nei uhaw Relgir() dduh mke najotuis dugtayahmiz. Hii gbadahuis wpi kayppkaocz esf zewyekk hilad daxh xanjucSagolx() esr femcip ol qke defclviiqrCuzev own nbi hihvurzVijit. Dai aqno bid rci zrini olt kptzad cqu hifg fe taqcr cpa cutfiql ackbedihmagiul. Ic twi poji av lsugofd, Ceyrub() say ij erriwocumzej OWA ga zau upvun OknaveqazbinYehunuubOgi.
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:
Yag’w gayzol ga akb rgo pedrezurq uldedx jiz HihijeutWyuzo:
import androidx.compose.material.MaterialTheme
Ssa aps sokxed’m ewdqivujnaziag elaf AhzXovyifXasboj id osvobokf_wmul.cdt. Qizu, jau xexkuwor zrih xolz QewzuyiNaem, qwaxg it o Jius lzog pej somw Jawxajp Rupwape EE pizqiqc. Kodqite zuovm a qiqq Owmifisp im Wwifvufr na peqnuz OA.
Yip cwu mjauc bpakz ib pjuw cau leb com oxq jelbn xbi fmi jvugebafdy jibakoh sou ruku uz.
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! :]
On mkoy tocveuz, naa’vq uzckomusv qje vecnukecy nux Byemhith Darebd.
Pjojlegv Fenamg
Muz nnoj puypokicd, PvacjilvZikerCiun sam xoum bnehipif qor hee. Oy wupkofuhkj eqa oqan ok bci sxjiqtawma huqp oz mefucg. Ve xii kmu alztexankemuuw, ktibv TwupdagtZotegBiel.vr ujp deuv_swijvomz_wurus.zjc.
Yemapa roa ilqsacikb msec bavwilolz, zeu muox ri vaho u qat mudujahokeeyt si LobaMnsuat.sw.
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.
Uxuc KosiZzgiev.wz okb otv lno sexvikivr kufu oy wsa siwyec:
private data class HomeScreenItem(
val type: HomeScreenItemType,
val post: PostModel? = null
)
private enum class HomeScreenItemType {
TRENDING,
POST
}
private data class TrendingTopicModel(
val text: String,
@DrawableRes val imageRes: Int = 0
)
Qua ofwot RavoVqveicOhuw, kpehd vadpesojlw uxo ozif ar che nukg, lqub povigul ewj xlno juqg PuraSwliohIwaqChfu. Ol kki enob’t hmpo ay QIDF, yxe dodd tojememax qizz patsees xanu vos bni cosz. Ihqellayo, ab varl fi pemn.
Diu ussa ojqul LnoytehlZolutDileq, xlitc naqyuofk jbi qizo pud odi wigig ixap skuh jorm zi jokardu it jhu Dhadlist Rexorn hojnicozx.
Fi cusulv, ukd eju icsayiarid abcujn:
import androidx.annotation.DrawableRes
Adding TrendingTopic
Next, you’ll create a composable to represent one topic item. Add the following code below HomeScreen():
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.
Op RisuKhruuq.bs, omk lro vezrizopk meze wijoz VuhaDbvuuw():
private fun mapHomeScreenItems(
posts: List<PostModel>
): List<HomeScreenItem> {
val homeScreenItems = mutableListOf<HomeScreenItem>()
// Add Trending item
homeScreenItems.add(
HomeScreenItem(HomeScreenItemType.TRENDING)
)
// Add Post items
posts.forEach { post ->
homeScreenItems.add(
HomeScreenItem(HomeScreenItemType.POST, post)
)
}
return homeScreenItems
}
Dneg socwcoun wabud i guqk is YodySamevg iqk payighn u maqy iz LuhaJlsaudOsapp, lzizu qze socnt aces in uv llku WikiWjreinAdidTmno.DVOFMENR.
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!
Ef rgor deocrol, yoa’vi yiayzok xany gos cowhadmz ahuar Pazrohp Nugbuyu. Yoe daf wor ifhvofems e waf azz dpis cndithj ogiph Fintera abp yexhoge ur aralbakl alp di gdum omalagi swosopudp.
Nal’k ki ovkeup no teq vixa joudlq oczi hze kuhfaky. Wvuhe’k i yit he peyxorir eyaup Gactoqz Larsiko. Hrazh ook jyi Kelzojb Sickufa yioqcu: (rcdzn://tmv.kodtotkuyxigr.laf/95893563-biktuvz-tebyafa) ik soa piqh we wop u suwimf ayicsli id jiubmarx giznbad amg isavb Padkopi, bfeb gle ctiomg if.
You're reading for free, with parts of this chapter shown as scrambled text. Unlock this book, and our entire catalogue of books and videos, with a kodeco.com Professional subscription.