Skip to main content

Posts

Showing posts from February, 2019

Android Kotlin: Room RecyclerView LiveData example

Introduction In modern Android app development, building efficient and responsive user interfaces that can handle large datasets is a crucial aspect. Combining components like RecyclerView for displaying data and Room for database management is a common practice, especially when you want to ensure smooth data flow and user experience. This example demonstrates how to use Room , RecyclerView , and LiveData in Kotlin to create an application where user inputs can be dynamically stored in a local database and displayed in real-time. This tutorial will walk through each part of the code in detail, from setting up the data model and database to integrating a view model and RecyclerView for efficient data handling. By the end of the description, you'll have a clear understanding of how these components work together to create a seamless Android application. MainActivity.kt In the MainActivity.kt , the app's primary logic is implemented. First, a StudentViewModel is retrieved using ...

Android Kotlin: Room LiveData ViewModel example

Introduction In Android development, integrating database operations with the user interface can be challenging, especially when aiming for clean and efficient code. Kotlin's support for Android Jetpack components, such as Room, ViewModel, and LiveData, provides an excellent solution for handling database-related tasks in a lifecycle-aware manner. This article demonstrates a practical example of how to use these components together in an Android application. Specifically, it shows how to implement a simple app where users can insert data into a Room database, and observe changes using LiveData, all managed by a ViewModel. The example covers the essential aspects of using Room for local storage, LiveData for observing data changes, and ViewModel for preserving UI-related data during configuration changes. The code includes database creation, a DAO interface for queries, and a ViewModel to manage UI data. MainActivity.kt Overview The MainActivity serves as the entry point of the app...

Android Kotlin: Room singleton example

Introduction In Android development, managing a local SQLite database can be challenging without a proper framework. Fortunately, the Room persistence library simplifies this process by providing an abstraction layer over SQLite, helping developers create robust and maintainable database systems. In Kotlin, combining Room with the Singleton design pattern offers a clean and efficient way to manage a single instance of the database throughout the app's lifecycle. This example demonstrates how to use a Singleton with Room to perform basic database operations, such as inserting and fetching data from a database table. In this tutorial, we walk through an Android Kotlin example that sets up a Room database using the Singleton pattern. The goal is to ensure only one instance of the database is used, which is crucial for avoiding resource leakage and ensuring thread safety. We'll also explain how the app interacts with this database using Data Access Objects (DAOs), Entities, and asy...

android kotlin - Volley UTF-8 encoding example

MainActivity.kt package com.cfsuman.kotlintutorials import android.app.Activity import android.os.Bundle import android.text.method.ScrollingMovementMethod import android.widget.* import com.android.volley.Request class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get the widgets reference from XML layout val button = findViewById<Button>(R.id.button) val textView = findViewById<TextView>(R.id.textView) // Make text view content scrollable textView.movementMethod = ScrollingMovementMethod() // Feed Url val url = "https://www.kalerkantho.com/rss.xml" // Run volley button.setOnClickListener { // Disable the button itself it.isEnabled = false val stringRequest = VolleyUTF8EncodingStringRequest( Reques...

android kotlin - Volley post request with parameters

MainActivity.kt package com.cfsuman.kotlintutorials import android.app.Activity import android.os.Bundle import android.text.method.ScrollingMovementMethod import android.widget.* import com.android.volley.DefaultRetryPolicy import com.android.volley.Request import com.android.volley.toolbox.JsonObjectRequest import org.json.JSONObject class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get the widgets reference from XML layout val button = findViewById<Button>(R.id.button) val textView = findViewById<TextView>(R.id.textView) // Make text view content scrollable textView.movementMethod = ScrollingMovementMethod() // Run volley button.setOnClickListener { // Disable the button itself it.isEnabled = false val url = "https://postman-echo....

android kotlin - Volley basic authentication example

MainActivity.kt package com.cfsuman.kotlintutorials import android.app.Activity import android.os.Bundle import android.widget.* import com.android.volley.Request class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get the widgets reference from XML layout val button = findViewById<Button>(R.id.button) val textView = findViewById<TextView>(R.id.textView) // Run volley button.setOnClickListener { // Disable the button itself it.isEnabled = false //val credentials = "username:$password" val credentials = "postman:password" val url = "https://postman-echo.com/basic-auth" textView.text = "" // Make a volley custom json object request // with basic authentication...

android kotlin - Volley example

MainActivity.kt package com.cfsuman.kotlintutorials import android.app.Activity import android.os.Bundle import android.text.method.ScrollingMovementMethod import android.util.Log import android.widget.* import com.android.volley.Request import com.android.volley.toolbox.JsonObjectRequest import org.json.JSONException class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get the widgets reference from XML layout val button = findViewById<Button>(R.id.button) val textView = findViewById<TextView>(R.id.textView) // Make text view text scrollable textView.movementMethod = ScrollingMovementMethod() // Data URL val url = "https://pastebin.com/raw/2bW31yqa" // Run volley button.setOnClickListener { // disable the button itself it.isEna...

Android Kotlin: WorkManager constraints example

Introduction In this article, we explore an Android Kotlin example using WorkManager to handle background tasks with specific constraints. WorkManager is part of Jetpack libraries and provides a simple yet flexible API for scheduling deferrable, guaranteed tasks that will run even if the app exits or the device restarts. In our example, we use WorkManager to download an image from a URL with certain constraints, such as the device being on charge and having a network connection. This breakdown will guide you through the key components of this example, highlighting the WorkManager’s constraints, handling background tasks with worker classes, and the integration of UI elements to display the download status and the downloaded image. MainActivity Overview The main activity ( MainActivity.kt ) is the entry point for the application and contains the logic for triggering the download process. When the user clicks the "Download Image" button, a WorkRequest is created with constrai...

Android Kotlin: WorkManager input output data example

Introduction WorkManager is a library in Android Jetpack that allows developers to schedule deferrable, asynchronous tasks that are guaranteed to execute even if the app exits or the device is restarted. This makes it highly suitable for background tasks that require guaranteed execution, such as downloading images, syncing data, or sending logs. In this article, we’ll explore an example of using WorkManager in Kotlin to download an image from the internet, manage input and output data, and display the result on the screen. This example showcases how WorkManager simplifies task scheduling and ensures task completion even in challenging circumstances. In addition to providing an overview of the WorkManager implementation, this breakdown will delve into key components such as input data, output data, observing task states, and handling background processes efficiently. By the end of this walkthrough, you'll have a good understanding of how to implement WorkManager for tasks requiring...