Skip to main content

Posts

Coroutine- A complete tutorial

 What are coroutines? To know about coroutine, first you need to know about asynchronous programming, thread and multithreading concept. What is a thread? Thread describes in which context the function or sequence of instructions should be executed. So, every block of code or functions runs in a thread, right? Also, you can load multiple threads to perform different block of codes  How to start a thread? Thread thread = new Thread(){     public void run(){       System.out.println("This is a thread");     }   }     thread.start(); So when coming to android, before learning about coroutines we need to discuss some scenarios. Normally how an app works when user launch an application. When user launchers the application, a main thread is created. This thread is intended to do small operations like button clicks, UI interaction , small mathematical operations. We cant perform long running operation like file download, database queries, network operations and image

How to get the device information programmatically in android

You can use Build class to get the information of the device and the operating system details in which your app is running        val manufacturer = Build.MANUFACTURER val model = Build.MODEL val version = Build.VERSION.SDK_INT val versionRelease = Build.VERSION.RELEASE   Log.d( "Device Information", """manufacturer $manufacturer model $model version $version versionRelease $versionRelease""" )  

Live Data. Difference between livedata and mutable live data. Mediator Live Data. Live Data Encapsulation

What is Live Data?   Live Data is one of the first architectural components. Live Data is a data holder class. Not only a simple data holder class, but having lot of features. We can say that it is a simple, lifecycle aware, observable, data holder class. Before introducing live data , we need to manually handle the updates in the UI. We were using traditional approaches like interfaces, events buses to update the UI. These traditional approaches having problem like manual handling of lifecycle, memory leaks, and crashes.  Live Data It is wrapper which can be used with any data such as data and objects that implements collections like list. A Live Data usually declared and used inside a view model class. Live Data can be used to observe the change of a particular data and update the UI based on that. Due to life cycle aware property we don't need to bother about components life cycle. i.e. it will send the update only when the component (activity/fragment)  is in active state. If t

AlertBox Example in android

Alert Box in android  A dialogue is a small window that prompts the user to make decision or enter additional information.A dialogue does not fill the screen and is normally used for modal events that require users to take an action before they can proceed package com.example; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class AlertExampleActivity extends Activity {       /** Called when the activity is first created. */       @Override       public void onCreate(Bundle savedInstanceState) {             super .onCreate(savedInstanceState);             setContentView(R.layout. main );             Button button=(Button)findViewById(R.id. button1 );             button.setOnClickListener( new OnClickListener() {                   public void onClick(View

Progressbar example in android

Progress bar In android, progress bar can be used to tell the user the task takes longer time to finish. Progress  bar displays a bar which represent how far the operations has progressed. Progress bar can be made indeterminate. In indeterminate mode, progress bar shows a cyclic animation without indication progress. In this tutorial, I’ll show how to display a progress bar  package com.example; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.widget.ProgressBar; import android.widget.TextView; public class ProgressbarExamplActivity extends Activity {       private ProgressBar bar ;       private TextView txt ;       protected boolean isRunning ;       /** Called when the activity is first created. */       @Override       public void onCreate(Bundle savedInstanceState) {             super .onCreate(savedInstanceState);             setContentView(R.layout. main );             bar =(ProgressB