Posts

Store Key Value Pairs Using Jetpack Data Store

One of the ways we can store information on an Android device is by using Shared Preferences. These Shared Preferences help us save key-value pairs. A newer way of saving such key-value pairs is using the Jetpack library Data Store . The Data Store library saves the key-value pairs using coroutine blocks and provides the ability to read the saved preferences using the collect function available in the Kotlin Flow. In this article, we will take a look at the Data Store class available in the Jetpack library and how to save, read the data using it. The first thing we need to do is add the dependency in the module level build.gradle file. implementation "androidx.datastore:datastore-preferences:1.0.0-alpha06" Create an instance of the Data Store using context and by passing the file name to the createDataStore function. Android creates the data store file inside a folder named datastore and this folder will be available in the app's files directory. The name passed to th

Use Java8 APIs on Older Android Versions

Image
The less adoption of Java8 APIs in the Android Codebase is because of the lack of support on the older versions. Even though Java8 was released in 2014, it took a while for Google to resolve the issue. Existing Issue We will take an example to understand the issue in detail. Let's say we have an array list that contains countries and we need to check if the desired country is available in the list. The code to achieve it would look something like below. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { // Find the country with name India val country = countryArrayList.stream().filter { it == "India" }.findFirst() if (country.isPresent) { // Country is present in the list } else { // Country is not present in the list } } else { // For loop to filter and find the item on the older version var countryPresent = false countryArrayList.forEach { if (it == "India") { // Country is present in t

Listen to Back Button Clicks in Fragment

Listening to the back button click event inside the fragments, comprised of a painful implementation of the interfaces and the onBackPressed method calls in both Activity and Fragment, prior to the introduction of the OnBackPressedDispatcher . But now, inside a Fragment, the back button behaviour can be controlled by listening to the callback returned by the OnBackPressedDispatcher . This callback listener is implemented inside the  onCreate overridden method. override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // Listen to Back button clicks val callback = requireActivity().onBackPressedDispatcher.addCallback(this) { // Handle the back button event } } Use onBackPressedDispatcher method to get the OnBackPressedDispatcher and add the callback using the addCallback method. This callback method takes a lifecycle owner. Handle the back button events whenever the callback gets triggered. The same implementation can be done

Run the Emulator directly in Android Studio

Image
In Android Studio 4.1, a minor feature is released to reduce the screen (or window) switching between Android Studio and the Emulator. Using this feature, we can now run the Emulator directly inside the Android Studio. We can make the changes to the code and see the changes reflect in the emulator in the same window similar to layout XML and its preview. To achieve this, open the Preferences window and select the Emulator menu item under the Tools expandable menu. Here we can check the option Launch in a tool window and click OK . Now run the Emulator to see it inside the Android Studio window.

Track Android App Crashes Using Firebase Crashlytics

Crashes irritate the users and ruin the impression of the application while frequent crashes provoke the user to uninstall the application. Most of them might choose a competitors app and some of them might leave bad reviews on the Play Store. Either way, the application crashes negatively affect the business. Minimizing crashes is critical in improving overall user experience and retaining them. Best way to understand the issues in the application is by tracking crashes and solving. We need to find out if a particular crash is impacting a lot of users, get alerts when an issue suddenly increases in severity. and figure out which lines of code are causing crashes. Firebase Crashlytics is one such product that helps track crashes in realtime, understand the severity, get a glimpse of user composition affected by the crash, stack trace to resolve the crash and more. Before we start integrating the Firebase Crashlytics, we need to add Firebase to our project . Once we have added Firebase,

Add a Module to an Android Project

Image
In this article, we will take a look at creating a new module and adding it to our project. First, create a new project or open an existing project. Right-click or tap with two fingers on the app module or the project. This opens an options menu where we need to click on New and then on Module option in order to start the process of creating a module. The other way to start the module creation process is by clicking on the File menu in the top bar, hovering over New option and then clicking on the New Module option. Clicking on the New Module option opens a window where we need to select the Module type. Select the preferred module type based on feature development. For example, we can select the Android Library type if we are building a library, the Android TV Module if we want to expand our app to be available on TV, the Dynamic Feature Module if we want the feature to be available upon user request and not bloat the app size when packaging. In the next window, we have to en

Create Assets Folder, Add Files and Read Data From It

Image
Sometimes, we need to read the data stored in the files during runtime and want to make sure the data is not tampered by the Android build system. In this article, we will take a look at how to create the assets folder using Android Studio and read the stored data from it. First, Right-click on the module name or the package name. This opens an option window where we need to select the Assets Folder option by hovering over New and then on the Folder option. Now, we will see a window where we change the folder location and the source set. Change the values if necessary otherwise keep the defaults and click Finish. It may trigger a Gradle Sync. Irrespective of the sync, the Assets folder is created and is visible in the module. Now, Right-click on the Assets folder and click on the File option by hovering over the New option. Enter the name of the file name and click Enter to create the file. The file with the name user_details.json is created under the assets folder. Add some JSON