Skip to main content

Posts

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...