Certain applications show real-time data to the users, such as stock prices, availability of a product on online stores, etc. Showing real-time data requires continuous syncing of the application, and could be possible by implementing a program such as a thread. A Thread could be initiated by the application and update the real-time information implicitly or explicitly. The deal here is continuously updating the data (maybe from the servers) at the cost of additional RAM, Cache, and Battery of the device resulting in low-performance, as a Thread that runs forever occupies some space and requires power. To avoid using such programs, developers explicitly developed a feature for refreshing the application, so that the user can perform it whenever necessary. This brings us to conclude that manual refreshing has advantages such as:
- RAM Optimization
- Cache Memory Optimization
- Battery Life Optimization
- Avoiding Unnecessary Callbacks.
For example in the following image when the user will swipe down the screen then the string “Swipe to refresh” will be changed to “Refreshed”.
Step 1: Before started writing the code it is essential to add a Swipe Refresh Layout dependency into the build.Gradle of the application to enable swipe layouts. This dependency is:
Step 2: It is important to start with the Front-End “activity_main.xml“. Create a SwipeRefreshLayout to refresh the Layout and add a TextView to display the string on the screen and provide them with certain IDs.
Step 3: Coming to the “MainActivity.kt” file, a preview of the same is provided below. In this file connect the swipeRefreshLayout and textView to it’s XML file by using the findViewById() method. And also call the setOnRefreshListener() to change the text after the user swipe down the screen. The users can also write the required codes as their needs inside this method.
Output: Run on Emulator
Advantages: Of course, it’s not just the users who benefit. Assuming an application, where the information is fetched directly from a cloud repository. For every callback request (towards the cloud), the developer who owns such a repository pays a minimal amount towards the service, may it be Google Cloud Platform (GCP), Amazon Web Services (AWS), or any other thing.
- How to create Swipe Navigation in an Android App
- How to install Android Applications in Mobile Phone without USB Cables using Android Studio
- How to Add OpenCV library into Android Application using Android Studio?
- Android | AdMob Banner Ads for Android Studio
- Android | AdMob Interstitial Ads for Android Studio
- Android | How to Create/Start a New Project in Android Studio?
- How to create project in Android Studio using Kotlin
- How to build a simple Calculator app using Android Studio?
- How to create a Stopwatch App using Android Studio
- How to build a simple music player app using Android Studio
- Guessing the Number Game using Android Studio
- How to populate RecyclerView with Firebase data using FirebaseUI in Android Studio
- Toasts for Android Studio
- Guide to Install and Set up Android Studio
- 10 Important Android Studio Shortcuts You Need the Most
- How to scale different Views to all screen sizes in Android Studio?
- How to Upload Project on GitHub from Android Studio?
- Logcat window in Android Studio
- How to Generate Signed Apk in Android Studio?
- Android Studio Setup for Flutter Development
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.