{"id":808,"date":"2023-09-11T15:02:10","date_gmt":"2023-09-11T12:02:10","guid":{"rendered":"https:\/\/barisuslucan.com\/en\/?p=808"},"modified":"2023-09-11T15:03:31","modified_gmt":"2023-09-11T12:03:31","slug":"pull-to-refresh-in-android-with-jetpack-compose","status":"publish","type":"post","link":"https:\/\/barisuslucan.com\/en\/kotlin\/pull-to-refresh-in-android-with-jetpack-compose\/","title":{"rendered":"Pull-to-Refresh in Android with Jetpack Compose"},"content":{"rendered":"<p>Pull-to-refresh functionality is commonly used in Android applications to enhance the user experience. It allows users to refresh content by simply pulling down on the screen. Implementing pull-to-refresh with Android Jetpack Compose is straightforward. In this article, you&#8217;ll learn how to add this feature to your Android app step by step.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-809 aligncenter\" src=\"https:\/\/barisuslucan.com\/en\/wp-content\/uploads\/2023\/09\/pull-to-refresh.gif\" alt=\"pull-to-refresh\" width=\"156\" height=\"320\" \/><\/p>\n<p>Compose is a powerful tool for building user interfaces, and adding pull-to-refresh functionality is easy. Here are the steps to implement it:<\/p>\n<p>First, you need to add the material library to your project. This library contains you to quickly implement pull-to-refresh functionality. To complete this step, add the necessary dependency to your project&#8217;s build.gradle file. <strong>I also used faker in order to show random data while pulling refresh.<\/strong><\/p>\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"dependencies {\n    \/\/ Other dependencies\n    implementation(&quot;androidx.compose.material:material:1.5.1&quot;)\n    implementation(&quot;io.github.serpro69:kotlin-faker:1.15.0-rc.1&quot;)\n}\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #88C0D0\">dependencies<\/span><span style=\"color: #D8DEE9FF\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #616E88\">\/\/ Other dependencies<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #88C0D0\">implementation<\/span><span style=\"color: #D8DEE9FF\">(<\/span><span style=\"color: #A3BE8C\">&quot;androidx.compose.material:material:1.5.1&quot;<\/span><span style=\"color: #D8DEE9FF\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #88C0D0\">implementation<\/span><span style=\"color: #D8DEE9FF\">(<\/span><span style=\"color: #A3BE8C\">&quot;io.github.serpro69:kotlin-faker:1.15.0-rc.1&quot;<\/span><span style=\"color: #D8DEE9FF\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"@OptIn(ExperimentalMaterialApi::class)\n    @Composable\n    fun PullRefresh() {\n        val faker = Faker()\n        val refreshScope = rememberCoroutineScope()\n        var refreshing by remember { mutableStateOf(false) }\n\n        fun refresh() = refreshScope.launch {\n            refreshing = true\n            delay(1000)\n            refreshing = false\n        }\n\n        val state = rememberPullRefreshState(refreshing, ::refresh)\n\n        Box(Modifier.pullRefresh(state)) {\n            LazyColumn(Modifier.fillMaxSize()) {\n                if (!refreshing) {\n                    items(100) {\n                       Text(\n                           modifier = Modifier\n                               .padding(10.dp),\n                           text = faker.name.name(),\n                       )\n                    }\n                }\n            }\n\n            PullRefreshIndicator(refreshing, state, Modifier.align(Alignment.TopCenter))\n        }\n    }\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D8DEE9FF\">@OptIn(ExperimentalMaterialApi::<\/span><span style=\"color: #88C0D0\">class<\/span><span style=\"color: #D8DEE9FF\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    @Composable<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">fun<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">PullRefresh<\/span><span style=\"color: #D8DEE9FF\">() {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #81A1C1\">val<\/span><span style=\"color: #D8DEE9FF\"> faker <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">Faker<\/span><span style=\"color: #D8DEE9FF\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #81A1C1\">val<\/span><span style=\"color: #D8DEE9FF\"> refreshScope <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">rememberCoroutineScope<\/span><span style=\"color: #D8DEE9FF\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #81A1C1\">var<\/span><span style=\"color: #D8DEE9FF\"> refreshing <\/span><span style=\"color: #81A1C1\">by<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">remember<\/span><span style=\"color: #D8DEE9FF\"> { <\/span><span style=\"color: #88C0D0\">mutableStateOf<\/span><span style=\"color: #D8DEE9FF\">(<\/span><span style=\"color: #81A1C1\">false<\/span><span style=\"color: #D8DEE9FF\">) }<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #81A1C1\">fun<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">refresh<\/span><span style=\"color: #D8DEE9FF\">() <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> refreshScope.<\/span><span style=\"color: #88C0D0\">launch<\/span><span style=\"color: #D8DEE9FF\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">            refreshing <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">true<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">            <\/span><span style=\"color: #88C0D0\">delay<\/span><span style=\"color: #D8DEE9FF\">(<\/span><span style=\"color: #B48EAD\">1000<\/span><span style=\"color: #D8DEE9FF\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">            refreshing <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">false<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        }<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #81A1C1\">val<\/span><span style=\"color: #D8DEE9FF\"> state <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">rememberPullRefreshState<\/span><span style=\"color: #D8DEE9FF\">(refreshing, ::<\/span><span style=\"color: #88C0D0\">refresh<\/span><span style=\"color: #D8DEE9FF\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #88C0D0\">Box<\/span><span style=\"color: #D8DEE9FF\">(Modifier.<\/span><span style=\"color: #88C0D0\">pullRefresh<\/span><span style=\"color: #D8DEE9FF\">(state)) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">            <\/span><span style=\"color: #88C0D0\">LazyColumn<\/span><span style=\"color: #D8DEE9FF\">(Modifier.<\/span><span style=\"color: #88C0D0\">fillMaxSize<\/span><span style=\"color: #D8DEE9FF\">()) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">                <\/span><span style=\"color: #81A1C1\">if<\/span><span style=\"color: #D8DEE9FF\"> (<\/span><span style=\"color: #81A1C1\">!<\/span><span style=\"color: #D8DEE9FF\">refreshing) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">                    <\/span><span style=\"color: #88C0D0\">items<\/span><span style=\"color: #D8DEE9FF\">(<\/span><span style=\"color: #B48EAD\">100<\/span><span style=\"color: #D8DEE9FF\">) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">                       <\/span><span style=\"color: #88C0D0\">Text<\/span><span style=\"color: #D8DEE9FF\">(<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">                           modifier <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> Modifier<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">                               .<\/span><span style=\"color: #88C0D0\">padding<\/span><span style=\"color: #D8DEE9FF\">(<\/span><span style=\"color: #B48EAD\">10<\/span><span style=\"color: #D8DEE9FF\">.dp),<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">                           text <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> faker.name.<\/span><span style=\"color: #88C0D0\">name<\/span><span style=\"color: #D8DEE9FF\">(),<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">                       )<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">                    }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">                }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">            }<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">            <\/span><span style=\"color: #88C0D0\">PullRefreshIndicator<\/span><span style=\"color: #D8DEE9FF\">(refreshing, state, Modifier.<\/span><span style=\"color: #88C0D0\">align<\/span><span style=\"color: #D8DEE9FF\">(Alignment.TopCenter))<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    }<\/span><\/span><\/code><\/pre><\/div>\n\n\n<p><br \/>As can be seen, it is quite easy to use. Here, while refreshing, you can reissue your API requests. For more information <a href=\"https:\/\/developer.android.com\/reference\/kotlin\/androidx\/compose\/material\/pullrefresh\/package-summary\">click here<\/a><\/p>","protected":false},"excerpt":{"rendered":"<p>Pull-to-refresh functionality is commonly used in Android applications to enhance the user experience. It allows users to refresh content by simply pulling down on the screen. Implementing pull-to-refresh with Android Jetpack Compose is straightforward. In this article, you&#8217;ll learn how to add this feature to your Android app step by step. Compose is a powerful tool for building user interfaces, and adding pull-to-refresh functionality is easy. Here are the steps to implement it: First, you need to add the material library to your project. This library contains you to quickly implement pull-to-refresh functionality. To complete this step, add the necessary&#8230;<\/p>\n","protected":false},"author":1,"featured_media":789,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[29],"tags":[],"class_list":["post-808","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-kotlin"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/barisuslucan.com\/en\/wp-json\/wp\/v2\/posts\/808","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/barisuslucan.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/barisuslucan.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/barisuslucan.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/barisuslucan.com\/en\/wp-json\/wp\/v2\/comments?post=808"}],"version-history":[{"count":1,"href":"https:\/\/barisuslucan.com\/en\/wp-json\/wp\/v2\/posts\/808\/revisions"}],"predecessor-version":[{"id":810,"href":"https:\/\/barisuslucan.com\/en\/wp-json\/wp\/v2\/posts\/808\/revisions\/810"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/barisuslucan.com\/en\/wp-json\/wp\/v2\/media\/789"}],"wp:attachment":[{"href":"https:\/\/barisuslucan.com\/en\/wp-json\/wp\/v2\/media?parent=808"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/barisuslucan.com\/en\/wp-json\/wp\/v2\/categories?post=808"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/barisuslucan.com\/en\/wp-json\/wp\/v2\/tags?post=808"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}