{"id":48567,"date":"2021-10-13T04:15:51","date_gmt":"2021-10-13T04:15:51","guid":{"rendered":"https:\/\/www.bridge-global.com\/blog\/?p=48567"},"modified":"2021-10-14T04:50:03","modified_gmt":"2021-10-14T04:50:03","slug":"jetpack-compose","status":"publish","type":"post","link":"https:\/\/www.bridge-global.com\/blog\/jetpack-compose\/","title":{"rendered":"Modern Design Practices: Jetpack Compose"},"content":{"rendered":"<div class=\"vce-row-container\" data-vce-boxed-width=\"true\"><div class=\"vce-row vce-row--col-gap-30 vce-row-equal-height vce-row-content--top\" id=\"el-209d16f5\" data-vce-do-apply=\"all el-209d16f5\"><div class=\"vce-row-content\" data-vce-element-content=\"true\"><div class=\"vce-col vce-col--md-auto vce-col--xs-1 vce-col--xs-last vce-col--xs-first vce-col--sm-last vce-col--sm-first vce-col--md-last vce-col--lg-last vce-col--xl-last vce-col--md-first vce-col--lg-first vce-col--xl-first\" id=\"el-f6bfeb93\"><div class=\"vce-col-inner\" data-vce-do-apply=\"border margin background  el-f6bfeb93\"><div class=\"vce-col-content\" data-vce-element-content=\"true\" data-vce-do-apply=\"padding el-f6bfeb93\"><div class=\"vce-text-block\"><div class=\"vce-text-block-wrapper vce\" id=\"el-8263b097\" data-vce-do-apply=\"all el-8263b097\"><h2>What is Jetpack Compose?<\/h2>\n<p>As per Google,<a href=\"https:\/\/developer.android.com\/jetpack\/compose\" target=\"_blank\" rel=\"noopener\"> \u201cJetpack Compose is Android\u2019s modern toolkit for building native UI. It simplifies and accelerates UI development on Android. Quickly bring your app to life with less code, powerful tools, and intuitive Kotlin APIs\u201d<\/a>.<\/p>\n<p><strong>Well, what does that mean?<\/strong><\/p>\n<p>In Android, we normally create XML files for layouts and then update the UI elements every time based on the processing flow or inputs received using Data Binding\/View Binding\/Kotlin Semantics\/findViewById.<\/p>\n<p><strong>But now it\u2019s different!<\/strong><\/p>\n<p>Compose helps us to write a function that has both UI elements and rules for updating those elements, and these composable functions are called whenever information gets updated, i.e., a part of the UI is recreated every time <strong>without performance issues<\/strong>.<\/p>\n<p>Most of the modern frontend toolkits and frameworks such as <strong>React.js<\/strong> and<strong> Flutter<\/strong> follow this pattern.<\/p>\n<p><strong>Read more:<\/strong><a href=\"flutter-vs-react-native-war-of-tech-giants\/\" target=\"_blank\" rel=\"noopener\"> Flutter vs React Native \u2013 War of Tech Giants<\/a><\/p><\/div><\/div><\/div><\/div><\/div><\/div><\/div><\/div><div class=\"vce-row-container\" data-vce-boxed-width=\"true\"><div class=\"vce-row vce-row--col-gap-30 vce-row-equal-height vce-row-content--top\" id=\"el-a026e54a\" data-vce-do-apply=\"all el-a026e54a\"><div class=\"vce-row-content\" data-vce-element-content=\"true\"><div class=\"vce-col vce-col--md-auto vce-col--xs-1 vce-col--xs-last vce-col--xs-first vce-col--sm-last vce-col--sm-first vce-col--md-last vce-col--lg-last vce-col--xl-last vce-col--md-first vce-col--lg-first vce-col--xl-first\" id=\"el-efee04fc\"><div class=\"vce-col-inner\" data-vce-do-apply=\"border margin background  el-efee04fc\"><div class=\"vce-col-content\" data-vce-element-content=\"true\" data-vce-do-apply=\"padding el-efee04fc\"><div class=\"vce-text-block\"><div class=\"vce-text-block-wrapper vce\" id=\"el-9d38f915\" data-vce-do-apply=\"all el-9d38f915\"><h2>Why Jetpack Compose?<\/h2><ul><li>Uses fewer lines of code, so easily maintainable<\/li><li>Declarative Approach \u2013 Just describe the UI, compose will do the rest to update our UI with respect to application state changes.<\/li><li>No more XML, just one language Kotlin<\/li><li>These features accelerate development time.<\/li><\/ul><h2>Getting started with Jetpack Compose<\/h2><p>While composing UI with Jetpack Compose, we need to leave behind various types of layouts and remember just one thing: Everything is a composition of rows and columns. Think of rows and columns as just LinearLayout with orientation as horizontal or vertical respectively.<\/p><\/div><\/div><\/div><\/div><\/div><\/div><\/div><\/div><div class=\"vce-row-container\" data-vce-boxed-width=\"true\"><div class=\"vce-row vce-row--col-gap-30 vce-row-equal-height vce-row-content--top\" id=\"el-d874571b\" data-vce-do-apply=\"all el-d874571b\"><div class=\"vce-content-background-container\"><\/div><div class=\"vce-row-content\" data-vce-element-content=\"true\"><div class=\"vce-col vce-col--md-auto vce-col--xs-1 vce-col--xs-last vce-col--xs-first vce-col--sm-last vce-col--sm-first vce-col--md-first vce-col--lg-first vce-col--xl-first\" id=\"el-b8956eb0\"><div class=\"vce-col-inner\" data-vce-do-apply=\"border margin background  el-b8956eb0\"><div class=\"vce-col-content\" data-vce-element-content=\"true\" data-vce-do-apply=\"padding el-b8956eb0\"><div class=\"vce-single-image-container vce-single-image--align-center\"><div class=\"vce vce-single-image-wrapper\" id=\"el-a9d81659\" data-vce-do-apply=\"all el-a9d81659\"><figure><div class=\"vce-single-image-inner vce-single-image--absolute\" style=\"padding-bottom: 62%; width: 300px;\"><img loading=\"lazy\" decoding=\"async\" class=\"vce-single-image vcv-lozad\" data-src=\"https:\/\/www.bridge-global.com\/blog\/wp-content\/uploads\/2021\/10\/img1_rows-Jetpack-Compose-e1634184412396.jpg\" width=\"300\" height=\"186\" src=\"\" data-img-src=\"https:\/\/www.bridge-global.com\/blog\/wp-content\/uploads\/2021\/10\/img1_rows-Jetpack-Compose-e1634184412396.jpg\" alt=\"Rows and Columns - Jetpack compose\" title=\"rows -Jetpack Compose\" \/><noscript>\n        <img loading=\"lazy\" decoding=\"async\" class=\"vce-single-image\" src=\"https:\/\/www.bridge-global.com\/blog\/wp-content\/uploads\/2021\/10\/img1_rows-Jetpack-Compose-e1634184412396.jpg\" width=\"300\" height=\"186\" alt=\"Rows and Columns - Jetpack compose\" title=\"rows -Jetpack Compose\" \/>\n      <\/noscript><\/div><figcaption hidden=\"\"><\/figcaption><\/figure><\/div><\/div><\/div><\/div><\/div><div class=\"vce-col vce-col--md-auto vce-col--xs-1 vce-col--xs-last vce-col--xs-first vce-col--sm-last vce-col--sm-first vce-col--md-last vce-col--lg-last vce-col--xl-last\" id=\"el-63dd1de3\"><div class=\"vce-col-inner\" data-vce-do-apply=\"border margin background  el-63dd1de3\"><div class=\"vce-col-content\" data-vce-element-content=\"true\" data-vce-do-apply=\"padding el-63dd1de3\"><div class=\"vce-row-container\" data-vce-boxed-width=\"true\"><div class=\"vce-row vce-row--col-gap-30 vce-row-equal-height vce-row-content--top\" id=\"el-a86e4239\" data-vce-do-apply=\"all el-a86e4239\"><div class=\"vce-content-background-container\"><\/div><div class=\"vce-row-content\" data-vce-element-content=\"true\"><div class=\"vce-col vce-col--md-auto vce-col--xs-1 vce-col--xs-last vce-col--xs-first vce-col--sm-last vce-col--sm-first vce-col--md-last vce-col--lg-last vce-col--xl-last vce-col--md-first vce-col--lg-first vce-col--xl-first\" id=\"el-38a3877b\"><div class=\"vce-col-inner\" data-vce-do-apply=\"border margin background  el-38a3877b\"><div class=\"vce-col-content\" data-vce-element-content=\"true\" data-vce-do-apply=\"padding el-38a3877b\"><div class=\"vce-text-block\"><div class=\"vce-text-block-wrapper vce\" id=\"el-20e565aa\" data-vce-do-apply=\"all el-20e565aa\"><p>For our first learning experience, let's recreate a <strong>Counter App<\/strong> using Jetpack Compose.<\/p><p>For a great Jetpack Compose development experience, you should download <a href=\"https:\/\/developer.android.com\/studio\" target=\"_blank\" rel=\"noopener\">Android Studio Arctic Fox<\/a>.<\/p><p>The latest version of Android Studio features a new project template namely, <strong>Empty Compose Activity<\/strong> to help us get started.<\/p><\/div><\/div><\/div><\/div><\/div><\/div><\/div><\/div><\/div><\/div><\/div><\/div><\/div><\/div><div class=\"vce-row-container\" data-vce-boxed-width=\"true\"><div class=\"vce-row vce-row--col-gap-0 vce-row-equal-height vce-row-content--top\" id=\"el-016d2139\" data-vce-do-apply=\"all el-016d2139\"><div class=\"vce-content-background-container\"><\/div><div class=\"vce-row-content\" data-vce-element-content=\"true\"><div class=\"vce-col vce-col--md-auto vce-col--xs-1 vce-col--xs-last vce-col--xs-first vce-col--sm-last vce-col--sm-first vce-col--md-last vce-col--lg-last vce-col--xl-last vce-col--md-first vce-col--lg-first vce-col--xl-first\" id=\"el-95b042ff\"><div class=\"vce-col-inner\" data-vce-do-apply=\"border margin background  el-95b042ff\"><div class=\"vce-col-content\" data-vce-element-content=\"true\" data-vce-do-apply=\"padding el-95b042ff\"><div class=\"vce-single-image-container vce-single-image--align-center\"><div class=\"vce vce-single-image-wrapper\" id=\"el-7b53340f\" data-vce-do-apply=\"all el-7b53340f\"><figure><div class=\"vce-single-image-inner vce-single-image--absolute\" style=\"padding-bottom: 60.1852%; width: 1080px;\"><img decoding=\"async\" class=\"vce-single-image\" src=\"https:\/\/www.bridge-global.com\/blog\/wp-content\/uploads\/2021\/10\/img2_empty-compose-jetpack-e1634184335241.jpg\" data-img-src=\"https:\/\/www.bridge-global.com\/blog\/wp-content\/uploads\/2021\/10\/img2_empty-compose-jetpack-e1634184335241.jpg\" alt=\"empty compose-jetpack\" title=\"img2_empty compose-jetpack\"><\/div><figcaption hidden=\"\"><\/figcaption><\/figure><\/div><\/div><\/div><\/div><\/div><\/div><\/div><\/div><div class=\"vce-row-container\" data-vce-boxed-width=\"true\"><div class=\"vce-row vce-row--col-gap-30 vce-row-equal-height vce-row-content--top\" id=\"el-9ccdcc86\" data-vce-do-apply=\"all el-9ccdcc86\"><div class=\"vce-row-content\" data-vce-element-content=\"true\"><div class=\"vce-col vce-col--md-auto vce-col--xs-1 vce-col--xs-last vce-col--xs-first vce-col--sm-last vce-col--sm-first vce-col--md-last vce-col--lg-last vce-col--xl-last vce-col--md-first vce-col--lg-first vce-col--xl-first\" id=\"el-b1eaa774\"><div class=\"vce-col-inner\" data-vce-do-apply=\"border margin background  el-b1eaa774\"><div class=\"vce-col-content\" data-vce-element-content=\"true\" data-vce-do-apply=\"padding el-b1eaa774\"><div class=\"vce-text-block\"><div class=\"vce-text-block-wrapper vce\" id=\"el-8e7cab97\" data-vce-do-apply=\"all el-8e7cab97\"><p>Let\u2019s create a new project with \u2018Empty Compose Activity\u2019 template and open MainActivity.kt<\/p><p>Here you might have noticed few changes:<\/p><ul><li><strong>setContent{}<\/strong> instead of setContentView{} - The setContent{} block defines the activity's layout where we call composable functions.<\/li><li><strong>@Composable<\/strong> and <strong>@Preview<\/strong> decorators - to add a view to the UI, we need to create a function with @Composable annotation, which makes it a Compose function. Compose UI library defines a whole lot of composable functions to display various elements on the screen. For example, the Text() function defined by the Compose UI library displays a text label on the screen.<\/li><\/ul><p>To preview the UI rendered in Android Studio, we can use @Preview annotation.<\/p><ul><li><strong>Changes in folder structure<\/strong> \u2013 No more XML files.<\/li><\/ul><p>Lets\u2019 clear all those default code lines and start building an app from scratch.<\/p><p>We\u2019ll create a simple UI with just 3 elements \u2013 Appbar, TextView and a Button<\/p><\/div><\/div><\/div><\/div><\/div><\/div><\/div><\/div><div class=\"vce-row-container\" data-vce-boxed-width=\"true\"><div class=\"vce-row vce-row--col-gap-30 vce-row-equal-height vce-row-content--top\" id=\"el-c224045e\" data-vce-do-apply=\"all el-c224045e\"><div class=\"vce-row-content\" data-vce-element-content=\"true\"><div class=\"vce-col vce-col--md-auto vce-col--xs-1 vce-col--xs-last vce-col--xs-first vce-col--sm-last vce-col--sm-first vce-col--md-last vce-col--lg-last vce-col--xl-last vce-col--md-first vce-col--lg-first vce-col--xl-first\" id=\"el-a6293c9a\"><div class=\"vce-col-inner\" data-vce-do-apply=\"border margin background  el-a6293c9a\"><div class=\"vce-col-content\" data-vce-element-content=\"true\" data-vce-do-apply=\"padding el-a6293c9a\"><div class=\"vce-single-image-container vce-single-image--align-center\"><div class=\"vce vce-single-image-wrapper\" id=\"el-35b7c8fe\" data-vce-do-apply=\"all el-35b7c8fe\"><figure><div class=\"vce-single-image-inner vce-single-image--absolute vce-single-image--border-rounded\" style=\"padding-bottom: 54.5898%; width: 1024px;\"><img loading=\"lazy\" decoding=\"async\" class=\"vce-single-image vcv-lozad\" data-src=\"https:\/\/www.bridge-global.com\/blog\/wp-content\/uploads\/2021\/10\/img3_ui_with_3_elements-e1634184471938.jpg\" width=\"1024\" height=\"559\" src=\"\" data-img-src=\"https:\/\/www.bridge-global.com\/blog\/wp-content\/uploads\/2021\/10\/img3_ui_with_3_elements-e1634184471938.jpg\" alt=\"Jetpack - UI with just 3 elements \u2013 Appbar, TextView and a Button\" title=\"Jetpack - UI with just 3 elements \u2013 Appbar, TextView and a Button\" \/><noscript>\n        <img loading=\"lazy\" decoding=\"async\" class=\"vce-single-image\" src=\"https:\/\/www.bridge-global.com\/blog\/wp-content\/uploads\/2021\/10\/img3_ui_with_3_elements-e1634184471938.jpg\" width=\"1024\" height=\"559\" alt=\"Jetpack - UI with just 3 elements \u2013 Appbar, TextView and a Button\" title=\"Jetpack - UI with just 3 elements \u2013 Appbar, TextView and a Button\" \/>\n      <\/noscript><\/div><figcaption hidden=\"\"><\/figcaption><\/figure><\/div><\/div><\/div><\/div><\/div><\/div><\/div><\/div><div class=\"vce-row-container\" data-vce-boxed-width=\"true\"><div class=\"vce-row vce-row--col-gap-30 vce-row-equal-height vce-row-content--top\" id=\"el-b40ac51d\" data-vce-do-apply=\"all el-b40ac51d\"><div class=\"vce-row-content\" data-vce-element-content=\"true\"><div class=\"vce-col vce-col--md-auto vce-col--xs-1 vce-col--xs-last vce-col--xs-first vce-col--sm-last vce-col--sm-first vce-col--md-last vce-col--lg-last vce-col--xl-last vce-col--md-first vce-col--lg-first vce-col--xl-first\" id=\"el-718f104f\"><div class=\"vce-col-inner\" data-vce-do-apply=\"border margin background  el-718f104f\"><div class=\"vce-col-content\" data-vce-element-content=\"true\" data-vce-do-apply=\"padding el-718f104f\"><div class=\"vce-text-block\"><div class=\"vce-text-block-wrapper vce\" id=\"el-bca6f4f4\" data-vce-do-apply=\"all el-bca6f4f4\"><p>As discussed earlier, we have only rows and columns in Compose UI.<\/p>\n<p>So here, we have created a composable function <strong>SetUpCounter,<\/strong> which renders a column of UI elements.<\/p>\n<p><strong>TopAppBar<\/strong> is an inbuilt composable function for adding a top bar to our application. Similar is the case of <strong>Button<\/strong> and <strong>Text<\/strong>.<\/p>\n<p>You will notice the <strong>modifier<\/strong> argument in the Text function. This is the Compose way of adding formatting to the elements, which is uniform across all the elements.<\/p>\n<p>Here we set the Text View\u2019s width to match_parent using <strong>fillMaxWidth()<\/strong><\/p>\n<p>In Button, we have added the<strong> onClick()<\/strong> where we will update the counter value and this updated value will be shown in our Text Composable.<\/p>\n<p>So, our <strong>onCreate()<\/strong> would look like:<\/p><\/div><\/div><\/div><\/div><\/div><\/div><\/div><\/div><div class=\"vce-row-container\" data-vce-boxed-width=\"true\"><div class=\"vce-row vce-row--col-gap-30 vce-row-equal-height vce-row-content--top\" id=\"el-8ac43563\" data-vce-do-apply=\"all el-8ac43563\"><div class=\"vce-row-content\" data-vce-element-content=\"true\"><div class=\"vce-col vce-col--md-auto vce-col--xs-1 vce-col--xs-last vce-col--xs-first vce-col--sm-last vce-col--sm-first vce-col--md-last vce-col--lg-last vce-col--xl-last vce-col--md-first vce-col--lg-first vce-col--xl-first\" id=\"el-a95799ec\"><div class=\"vce-col-inner\" data-vce-do-apply=\"border margin background  el-a95799ec\"><div class=\"vce-col-content\" data-vce-element-content=\"true\" data-vce-do-apply=\"padding el-a95799ec\"><div class=\"vce-single-image-container vce-single-image--align-center\"><div class=\"vce vce-single-image-wrapper\" id=\"el-24430216\" data-vce-do-apply=\"all el-24430216\"><figure><div class=\"vce-single-image-inner vce-single-image--absolute\" style=\"padding-bottom: 41.1535%; width: 1023px;\"><img loading=\"lazy\" decoding=\"async\" class=\"vce-single-image vcv-lozad\" data-src=\"https:\/\/www.bridge-global.com\/blog\/wp-content\/uploads\/2021\/10\/img4_on_create-e1634184516362.jpg\" width=\"1023\" height=\"421\" src=\"\" data-img-src=\"https:\/\/www.bridge-global.com\/blog\/wp-content\/uploads\/2021\/10\/img4_on_create-e1634184516362.jpg\" alt=\"onCreate() - JetPack\" title=\"onCreate() - JetPack\" \/><noscript>\n        <img loading=\"lazy\" decoding=\"async\" class=\"vce-single-image\" src=\"https:\/\/www.bridge-global.com\/blog\/wp-content\/uploads\/2021\/10\/img4_on_create-e1634184516362.jpg\" width=\"1023\" height=\"421\" alt=\"onCreate() - JetPack\" title=\"onCreate() - JetPack\" \/>\n      <\/noscript><\/div><figcaption hidden=\"\"><\/figcaption><\/figure><\/div><\/div><\/div><\/div><\/div><\/div><\/div><\/div><div class=\"vce-row-container\" data-vce-boxed-width=\"true\"><div class=\"vce-row vce-row--col-gap-30 vce-row-equal-height vce-row-content--top\" id=\"el-c6ec8efc\" data-vce-do-apply=\"all el-c6ec8efc\"><div class=\"vce-row-content\" data-vce-element-content=\"true\"><div class=\"vce-col vce-col--md-auto vce-col--xs-1 vce-col--xs-last vce-col--xs-first vce-col--sm-last vce-col--sm-first vce-col--md-last vce-col--lg-last vce-col--xl-last vce-col--md-first vce-col--lg-first vce-col--xl-first\" id=\"el-12b2a5ae\"><div class=\"vce-col-inner\" data-vce-do-apply=\"border margin background  el-12b2a5ae\"><div class=\"vce-col-content\" data-vce-element-content=\"true\" data-vce-do-apply=\"padding el-12b2a5ae\"><div class=\"vce-text-block\"><div class=\"vce-text-block-wrapper vce\" id=\"el-076932f4\" data-vce-do-apply=\"all el-076932f4\"><p>Here, setContent{} calls a composable, preview able function App(), which in turn calls the composable function <strong>SetUpCounter()<\/strong> we discussed earlier.<\/p><p>Now our screen looks like this:<\/p><\/div><\/div><\/div><\/div><\/div><\/div><\/div><\/div><div class=\"vce-row-container\" data-vce-boxed-width=\"true\"><div class=\"vce-row vce-row--col-gap-30 vce-row-equal-height vce-row-content--top\" id=\"el-1fd577e1\" data-vce-do-apply=\"all el-1fd577e1\"><div class=\"vce-content-background-container\"><\/div><div class=\"vce-row-content\" data-vce-element-content=\"true\"><div class=\"vce-col vce-col--md-auto vce-col--xs-1 vce-col--xs-last vce-col--xs-first vce-col--sm-last vce-col--sm-first vce-col--md-last vce-col--lg-last vce-col--xl-last vce-col--md-first vce-col--lg-first vce-col--xl-first\" id=\"el-25b21510\"><div class=\"vce-col-inner\" data-vce-do-apply=\"border margin background  el-25b21510\"><div class=\"vce-col-content\" data-vce-element-content=\"true\" data-vce-do-apply=\"padding el-25b21510\"><div class=\"vce-single-image-container vce-single-image--align-center\"><div class=\"vce vce-single-image-wrapper\" id=\"el-e30a278f\" data-vce-do-apply=\"all el-e30a278f\"><figure><div class=\"vce-single-image-inner vce-single-image--absolute\" style=\"padding-bottom: 197.804%; width: 501px;\"><img loading=\"lazy\" decoding=\"async\" class=\"vce-single-image vcv-lozad\" data-src=\"https:\/\/www.bridge-global.com\/blog\/wp-content\/uploads\/2021\/10\/img5_initial_screenshot-e1634184608399.jpg\" width=\"501\" height=\"991\" src=\"\" data-img-src=\"https:\/\/www.bridge-global.com\/blog\/wp-content\/uploads\/2021\/10\/img5_initial_screenshot-e1634184608399.jpg\" alt=\"jetpack initial screenshor\" title=\"img5_initial_screenshot\" \/><noscript>\n        <img loading=\"lazy\" decoding=\"async\" class=\"vce-single-image\" src=\"https:\/\/www.bridge-global.com\/blog\/wp-content\/uploads\/2021\/10\/img5_initial_screenshot-e1634184608399.jpg\" width=\"501\" height=\"991\" alt=\"jetpack initial screenshor\" title=\"img5_initial_screenshot\" \/>\n      <\/noscript><\/div><figcaption hidden=\"\"><\/figcaption><\/figure><\/div><\/div><\/div><\/div><\/div><\/div><\/div><\/div><div class=\"vce-row-container\" data-vce-boxed-width=\"true\"><div class=\"vce-row vce-row--col-gap-30 vce-row-equal-height vce-row-content--top\" id=\"el-54e57ef1\" data-vce-do-apply=\"all el-54e57ef1\"><div class=\"vce-row-content\" data-vce-element-content=\"true\"><div class=\"vce-col vce-col--md-auto vce-col--xs-1 vce-col--xs-last vce-col--xs-first vce-col--sm-last vce-col--sm-first vce-col--md-last vce-col--lg-last vce-col--xl-last vce-col--md-first vce-col--lg-first vce-col--xl-first\" id=\"el-9870aa0c\"><div class=\"vce-col-inner\" data-vce-do-apply=\"border margin background  el-9870aa0c\"><div class=\"vce-col-content\" data-vce-element-content=\"true\" data-vce-do-apply=\"padding el-9870aa0c\"><div class=\"vce-text-block\"><div class=\"vce-text-block-wrapper vce\" id=\"el-2ffd76da\" data-vce-do-apply=\"all el-2ffd76da\"><p>Moving on, let\u2019s implement the counter functionality using states in Jetpack Compose.<\/p><h2>What is a State in Jetpack Compose?<\/h2><p>State is an object which contains certain data that is mapped to one or many widgets. The value of the state can change during the runtime, and using this value from the state object, we can update the data shown in widgets.<\/p><p>Let\u2019s modify SetUpCounter() to create a variable <strong>counterState<\/strong> of type <strong>MutableState<\/strong> which will hold an initial value of 0. When the button is clicked, counterState value is incremented by 1 and the state would re-compose to redraw the UI.<\/p><\/div><\/div><\/div><\/div><\/div><\/div><\/div><\/div><div class=\"vce-row-container\" data-vce-boxed-width=\"true\"><div class=\"vce-row vce-row--col-gap-30 vce-row-equal-height vce-row-content--top\" id=\"el-26339ffd\" data-vce-do-apply=\"all el-26339ffd\"><div class=\"vce-row-content\" data-vce-element-content=\"true\"><div class=\"vce-col vce-col--md-auto vce-col--xs-1 vce-col--xs-last vce-col--xs-first vce-col--sm-last vce-col--sm-first vce-col--md-last vce-col--lg-last vce-col--xl-last vce-col--md-first vce-col--lg-first vce-col--xl-first\" id=\"el-b6c1cef5\"><div class=\"vce-col-inner\" data-vce-do-apply=\"border margin background  el-b6c1cef5\"><div class=\"vce-col-content\" data-vce-element-content=\"true\" data-vce-do-apply=\"padding el-b6c1cef5\"><div class=\"vce-single-image-container vce-single-image--align-center\"><div class=\"vce vce-single-image-wrapper\" id=\"el-63a58719\" data-vce-do-apply=\"all el-63a58719\"><figure><div class=\"vce-single-image-inner vce-single-image--absolute\" style=\"padding-bottom: 58.7192%; width: 1015px;\"><img loading=\"lazy\" decoding=\"async\" class=\"vce-single-image vcv-lozad\" data-src=\"https:\/\/www.bridge-global.com\/blog\/wp-content\/uploads\/2021\/10\/img6_modified_set_up_counter-e1634184252812.jpg\" width=\"1015\" height=\"596\" src=\"\" data-img-src=\"https:\/\/www.bridge-global.com\/blog\/wp-content\/uploads\/2021\/10\/img6_modified_set_up_counter-e1634184252812.jpg\" alt=\"jetpack -modified setup counter\" title=\"img6_modified_set_up_counter\" \/><noscript>\n        <img loading=\"lazy\" decoding=\"async\" class=\"vce-single-image\" src=\"https:\/\/www.bridge-global.com\/blog\/wp-content\/uploads\/2021\/10\/img6_modified_set_up_counter-e1634184252812.jpg\" width=\"1015\" height=\"596\" alt=\"jetpack -modified setup counter\" title=\"img6_modified_set_up_counter\" \/>\n      <\/noscript><\/div><figcaption hidden=\"\"><\/figcaption><\/figure><\/div><\/div><\/div><\/div><\/div><\/div><\/div><\/div><div class=\"vce-row-container\" data-vce-boxed-width=\"true\"><div class=\"vce-row vce-row--col-gap-30 vce-row-equal-height vce-row-content--top\" id=\"el-edf94f88\" data-vce-do-apply=\"all el-edf94f88\"><div class=\"vce-row-content\" data-vce-element-content=\"true\"><div class=\"vce-col vce-col--md-auto vce-col--xs-1 vce-col--xs-last vce-col--xs-first vce-col--sm-last vce-col--sm-first vce-col--md-last vce-col--lg-last vce-col--xl-last vce-col--md-first vce-col--lg-first vce-col--xl-first\" id=\"el-4b55e660\"><div class=\"vce-col-inner\" data-vce-do-apply=\"border margin background  el-4b55e660\"><div class=\"vce-col-content\" data-vce-element-content=\"true\" data-vce-do-apply=\"padding el-4b55e660\"><div class=\"vce-text-block\"><div class=\"vce-text-block-wrapper vce\" id=\"el-9abd456d\" data-vce-do-apply=\"all el-9abd456d\"><p><strong>remember<\/strong> is a composable function to cache expensive operations.<\/p><p>Compose observes any reads and writes to the MutableState object and triggers a <a href=\"https:\/\/developer.android.com\/jetpack\/compose\/state#composition-and-recomposition\" target=\"_blank\" rel=\"noopener\">recomposition<\/a> to update the UI, thus we get our required output.<\/p><\/div><\/div><\/div><\/div><\/div><\/div><\/div><\/div><div class=\"vce-row-container\" data-vce-boxed-width=\"true\"><div class=\"vce-row vce-row--col-gap-30 vce-row-equal-height vce-row-content--top\" id=\"el-33a6df8c\" data-vce-do-apply=\"all el-33a6df8c\"><div class=\"vce-row-content\" data-vce-element-content=\"true\"><div class=\"vce-col vce-col--md-auto vce-col--xs-1 vce-col--xs-last vce-col--xs-first vce-col--sm-last vce-col--sm-first vce-col--md-last vce-col--lg-last vce-col--xl-last vce-col--md-first vce-col--lg-first vce-col--xl-first\" id=\"el-b6c722e3\"><div class=\"vce-col-inner\" data-vce-do-apply=\"border margin background  el-b6c722e3\"><div class=\"vce-col-content\" data-vce-element-content=\"true\" data-vce-do-apply=\"padding el-b6c722e3\"><div class=\"vce-single-image-container vce-single-image--align-center\"><div class=\"vce vce-single-image-wrapper\" id=\"el-37dd4944\" data-vce-do-apply=\"all el-37dd4944\"><figure><div class=\"vce-single-image-inner vce-single-image--absolute\" style=\"padding-bottom: 204.969%; width: 483px;\"><img loading=\"lazy\" decoding=\"async\" class=\"vce-single-image vcv-lozad\" data-src=\"https:\/\/www.bridge-global.com\/blog\/wp-content\/uploads\/2021\/10\/img7_final_screenshot-e1634184787380.jpg\" width=\"483\" height=\"990\" src=\"\" data-img-src=\"https:\/\/www.bridge-global.com\/blog\/wp-content\/uploads\/2021\/10\/img7_final_screenshot-e1634184787380.jpg\" alt=\"jetpack-final screenshot\" title=\"img7_final_screenshot\" \/><noscript>\n        <img loading=\"lazy\" decoding=\"async\" class=\"vce-single-image\" src=\"https:\/\/www.bridge-global.com\/blog\/wp-content\/uploads\/2021\/10\/img7_final_screenshot-e1634184787380.jpg\" width=\"483\" height=\"990\" alt=\"jetpack-final screenshot\" title=\"img7_final_screenshot\" \/>\n      <\/noscript><\/div><figcaption hidden=\"\"><\/figcaption><\/figure><\/div><\/div><\/div><\/div><\/div><\/div><\/div><\/div><div class=\"vce-row-container\" data-vce-boxed-width=\"true\"><div class=\"vce-row vce-row--col-gap-30 vce-row-equal-height vce-row-content--top\" id=\"el-03c1a247\" data-vce-do-apply=\"all el-03c1a247\"><div class=\"vce-row-content\" data-vce-element-content=\"true\"><div class=\"vce-col vce-col--md-auto vce-col--xs-1 vce-col--xs-last vce-col--xs-first vce-col--sm-last vce-col--sm-first vce-col--md-last vce-col--lg-last vce-col--xl-last vce-col--md-first vce-col--lg-first vce-col--xl-first\" id=\"el-31fd825f\"><div class=\"vce-col-inner\" data-vce-do-apply=\"border margin background  el-31fd825f\"><div class=\"vce-col-content\" data-vce-element-content=\"true\" data-vce-do-apply=\"padding el-31fd825f\"><div class=\"vce-text-block\"><div class=\"vce-text-block-wrapper vce\" id=\"el-cdc11afb\" data-vce-do-apply=\"all el-cdc11afb\"><p>This is just a small introduction to the basics of Jetpack Compose. To explore more please visit the <a href=\"https:\/\/developer.android.com\/jetpack\/compose\" target=\"_blank\" rel=\"noopener\">official Android Jetpack Compose Documentation<\/a>.<\/p><\/div><\/div><\/div><\/div><\/div><\/div><\/div><\/div>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>Read the what and why of Jetpack compose &#8211; Android&#8217;s modern toolkit for building native UI &#8211; and how to get started with it.<br \/>\n<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":167,"featured_media":48591,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[437,438],"class_list":["post-48567","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology","tag-jetpack-compose","tag-android-jetpack-compose"],"featured_image_src":"https:\/\/www.bridge-global.com\/blog\/wp-content\/uploads\/2021\/10\/Understanding-Jetpack-Compose.jpg","author_info":{"display_name":"Shine John","author_link":"https:\/\/www.bridge-global.com\/blog\/author\/shinejohn\/"},"_links":{"self":[{"href":"https:\/\/www.bridge-global.com\/blog\/wp-json\/wp\/v2\/posts\/48567","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bridge-global.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bridge-global.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bridge-global.com\/blog\/wp-json\/wp\/v2\/users\/167"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bridge-global.com\/blog\/wp-json\/wp\/v2\/comments?post=48567"}],"version-history":[{"count":24,"href":"https:\/\/www.bridge-global.com\/blog\/wp-json\/wp\/v2\/posts\/48567\/revisions"}],"predecessor-version":[{"id":48601,"href":"https:\/\/www.bridge-global.com\/blog\/wp-json\/wp\/v2\/posts\/48567\/revisions\/48601"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bridge-global.com\/blog\/wp-json\/wp\/v2\/media\/48591"}],"wp:attachment":[{"href":"https:\/\/www.bridge-global.com\/blog\/wp-json\/wp\/v2\/media?parent=48567"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bridge-global.com\/blog\/wp-json\/wp\/v2\/categories?post=48567"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bridge-global.com\/blog\/wp-json\/wp\/v2\/tags?post=48567"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}