Skip to main content

Posts

Android Kotlin: WorkManager get status live data example

Introduction In this article, we explore an example of how to use WorkManager in Android with Kotlin to manage background tasks and observe their status through LiveData. WorkManager provides a flexible and reliable solution for deferrable tasks that need guaranteed execution. By observing LiveData, the app can dynamically react to changes in the task's state. The example demonstrates downloading an image in the background and updating the UI based on the task's progress and outcome. The example code consists of two main components: a WorkManager implementation that performs the image download, and a MainActivity that triggers the download and monitors its status. By following this example, developers will learn how to set up WorkManager, handle task status updates in real time, and display results on the UI. MainActivity Overview The MainActivity.kt file initializes the app's interface and manages interactions with the WorkManager. The layout contains a button ( btnDo...

android kotlin - DataBinding onClick example

MainActivity.kt package com.cfsuman.jetpack import android.graphics.Color import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.view.View import android.widget.TextView import androidx.databinding.DataBindingUtil import com.cfsuman.jetpack.databinding.ActivityMainBinding class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // Inflate view and obtain an instance of the binding class // ActivityMainBinding came from activity_main.xml file name with Binding suffix // Make your data type as this format val binding:ActivityMainBinding = DataBindingUtil.setContentView( this, R.layout.activity_main) as ActivityMainBinding // Define the student to data bind in xml layout binding.studentClass = Student("Hasiba Yeasmin",20) // Define the listener for binding binding.listener =...

android kotlin - DataBinding visibility example

MainActivity.kt package com.cfsuman.jetpack import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import androidx.databinding.DataBindingUtil import com.cfsuman.jetpack.databinding.ActivityMainBinding class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // Inflate view and obtain an instance of the binding class // ActivityMainBinding came from activity_main.xml file name with Binding suffix // Make your data type as this format val binding:ActivityMainBinding = DataBindingUtil.setContentView( this, R.layout.activity_main) as ActivityMainBinding // Define the student to data bind in xml layout // Test the example by alternating the true false values //binding.studentClass = Student("Hasiba Yeasmin",true) binding.studentClass = Student("Hasiba Yeasmin",false) } } // Mak...

android kotlin - DataBinding example

MainActivity.kt package com.cfsuman.jetpack import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import androidx.databinding.DataBindingUtil import com.cfsuman.jetpack.databinding.ActivityMainBinding class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // Inflate view and obtain an instance of the binding class // ActivityMainBinding came from activity_main.xml file name with Binding suffix // Make your data type as this format val binding:ActivityMainBinding = DataBindingUtil.setContentView( this, R.layout.activity_main) as ActivityMainBinding // Define the student to data bind in xml layout binding.studentClass = Student("Jenny Jones",25) } } // Make a student data class // Class name should start with capital letter otherwise may cause an error data class Student(val name:String, val age:I...

android kotlin - ViewModel LiveData DataBinding example

MainActivity.kt package com.cfsuman.jetpack import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import androidx.databinding.DataBindingUtil import kotlinx.android.synthetic.main.activity_main.* import androidx.lifecycle.ViewModelProviders import com.cfsuman.jetpack.databinding.ActivityMainBinding class MainActivity : AppCompatActivity() { private lateinit var mModel: RandomUUIDViewModel override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // Obtain the view model component mModel = ViewModelProviders.of(this).get(RandomUUIDViewModel::class.java) // Inflate view and obtain an instance of the binding class // ActivityMainBinding came from activity_main.xml with Binding suffix // Make your data type as this format val binding:ActivityMainBinding = DataBindingUtil.setContentView( this, R.layout.activity_main) as ActivityMainBinding // Set t...

Android Kotlin: ViewModel LiveData example

ViewModel and LiveData in Android Kotlin: A Practical Example In this article, we will explore a simple yet practical example of using ViewModel and LiveData in an Android Kotlin project. These two components, part of the Android Architecture Components, help developers manage UI-related data in a lifecycle-conscious way. The example consists of a MainActivity , a RandomNumberViewModel , and a simple XML layout with a button and a TextView . The goal of the example is to demonstrate how LiveData and ViewModel can be used together to generate and observe random numbers. Every time a user clicks the button, a new random number is generated and displayed in the TextView . The use of ViewModel ensures that the random number persists across configuration changes, such as device rotation, while LiveData makes sure the UI updates automatically whenever the data changes. MainActivity: Setting up ViewModel and Observing LiveData In the MainActivity , the onCreate function is responsible ...

Android Kotlin: WorkManager parameters example

Introduction WorkManager is an Android architecture component that provides a powerful and flexible API for scheduling background tasks. It allows developers to enqueue tasks that can be guaranteed to execute, even if the app is killed or the device is rebooted. In this article, we will explore how to use WorkManager in a Kotlin Android app, with an example that demonstrates how to download an image from the web and display it in the app. This example demonstrates the use of WorkManager's OneTimeWorkRequest to download images asynchronously, passing URL parameters to the worker class. The worker performs the download operation, saves the image to internal storage, and the app later retrieves and displays it. MainActivity and User Interaction In the MainActivity.kt file, the app's layout is set in the onCreate() method. Two buttons are used for interacting with the user: one for downloading an image ( btnDownload ) and another for displaying the downloaded image ( btnShow ). ...