Skip to main content

Posts

Showing posts from January, 2019

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

Android Kotlin: WorkManager example

Introduction In this article, we explore how to use Android's WorkManager with Kotlin to perform background tasks such as downloading images. WorkManager is part of Android Jetpack and is designed for tasks that require guaranteed execution, whether or not the app is in the foreground. It is particularly useful for scenarios like periodic data syncing, large file downloads, and applying long-running operations that need to be preserved even after app termination. We'll walk through an example that demonstrates how to download an image using WorkManager, store it in internal storage, and display it in an Android app. This guide covers the key components such as setting up the WorkManager request, managing background worker tasks, and handling image storage with Kotlin extensions. Breakdown of MainActivity The MainActivity is where the core functionality of the app begins. In the onCreate method, the layout is initialized, which includes two buttons: one for downloading an imag...

android - MaterialCardView example

MainActivity.kt package com.cfsuman.kotlintutorials import android.os.Bundle import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) } } activity_main.xml <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#DCDCDC" android:id="@+id/rootLayout" android:padding="24dp"> <com.google.android.material.card.MaterialCardView android:layout_width="matc...

android kotlin - ChipGroup multi selection example

MainActivity.kt package com.cfsuman.jetpack import android.content.Context import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.widget.Toast import kotlinx.android.synthetic.main.activity_main.* import com.google.android.material.chip.Chip class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Create an empty list val list = mutableListOf<String>() // Loop through the chips for (index in 0 until chipGroup.childCount) { val chip:Chip = chipGroup.getChildAt(index) as Chip // Set the chip checked change listener chip.setOnCheckedChangeListener{view, isChecked -> if (isChecked){ list.add(view.text.toString()) }else{ list.remove(view.text.toString()) ...

android - Change chip text size, color and font

activity_main.xml <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/rootLayout" tools:context=".MainActivity" android:background="#fdfdfc"> <com.google.android.material.chip.ChipGroup android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/chipGroup" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTo...

android - How to change chip selected color

activity_main.xml <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/rootLayout" tools:context=".MainActivity" android:background="#fdfdfc"> <com.google.android.material.chip.ChipGroup android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/chipGroup" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toT...

android kotlin - Create chip programmatically

MainActivity.kt package com.cfsuman.jetpack import android.content.Context import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.transition.TransitionManager import android.widget.Toast import androidx.core.content.ContextCompat import kotlinx.android.synthetic.main.activity_main.* import com.google.android.material.chip.Chip import kotlin.random.Random class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // set the button click listener button.setOnClickListener{ // Initialize a new chip instance val chip = Chip(this) chip.text = "Random ${Random.nextInt(100)}" // Set the chip icon chip.chipIcon = ContextCompat.getDrawable(this,R.drawable.ic_action_android) //chip.setChipIconTintResource(R.color.abc_search_url...

android kotlin - Chip group example

MainActivity.kt package com.cfsuman.jetpack import android.content.Context import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.widget.Toast import kotlinx.android.synthetic.main.activity_main.* import com.google.android.material.chip.Chip class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Set chip group checked change listener chipGroup.setOnCheckedChangeListener{group,checkedId:Int -> // Get the checked chip instance from chip group val chip:Chip? = findViewById(checkedId) chip?.let { // Show the checked chip text on toast message toast("${it.text} checked") } } } } // Toast extension method fun Context.toast(message:String)= Toast.makeText(this,message,Toast.LENGTH_SHORT).s...

android kotlin - Material chip example

MainActivity.kt package com.cfsuman.jetpack import android.content.Context import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.widget.Toast import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Set chip click listener chip1.setOnClickListener{ toast("chip clicked") } // Set chip checked change listener chip2.setOnCheckedChangeListener { view, isChecked -> // Handle the toggle. if (isChecked){ toast("chip checked") }else{ toast("chip unchecked") } } // Chip close icon click listener chip2.setOnCloseIconClickListener{ toast("chip close icon clicked...