Posts

Showing posts from December, 2021

Fragment in Android

Image
Overview Fragment is also called sub-activity. It is an application component which is dependent on Fragment. Following is the Fragment lifecycle. Activity with Fragment rotated Steps to follow Enable view binding in build.gradle (Module) file to use ActivityMainBinding. buildFeatures { viewBinding true } Create blank fragments inside an Android project. It will create a Java/Kotlin file and the associated xml file.  Extend the Java/Kotlin file with Fragment class. (When you create a new Fragment on Android Studio, the studio automatically creates basic boilerplate codes for you). class FragmentA : Fragment() { private lateinit var communicator : Communicator override fun onCreateView ( inflater : LayoutInflater , container : ViewGroup? , savedInstanceState : Bundle? ) : View? { communicator = activity as Communicator val view = inflater.inflate(R.layout. fragment_a , container , false ) view.btnSend.setOnClickListener {

Get user location in Android

Image
Overview Steps to follow Add the following permissions in AndroidManifest.xml file <uses-permission android :name ="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android :name ="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android :name ="android.permission.ACCESS_BACKGROUND_LOCATION" /> Add the following dependencies in gradle.build (Module) file implementation 'com.google.android.gms:play-services-location:19.0.0' Create a layout Write the logic private lateinit var fusedLocationProviderClient : FusedLocationProviderClient private val PERMISSION_ID = 44 override fun onCreate (savedInstanceState : Bundle?) { super .onCreate(savedInstanceState) setContentView(R.layout. activity_main ) fusedLocationProviderClient = FusedLocationProviderClient( this ) getLastLocation() } @SuppressLint ( "MissingPermission" ) private fun getLastLocation () { if (checkPermissio

SQLite in Android

Image
Overview SQLite is an opensource SQL database that is used to store data into a text file on a device. SQLite only supports NULL INTEGER REAL TEXT BLOB SQLite with MVVM Add the required dependency in build.gradle (Module) file. Create a data class data class User( val name : String , val dob : String , val address : String , val pin : String , val username : String , val password : String , val isLoggedIn : Int ) Create a class name DBHelper and extend it with SQLiteOpenHelper Create the repositories Some Syntax of SQL commands Do's and Dont's You cannot store a BOOLEAN value in SQLite, you have to use INTEGER for this purpose. Code to Open Database from external file public class DatabaseHelper extends SQLiteOpenHelper { public static final String DATABASE_NAME = "appDatabase.db" ; private static final int DATABASE_VERSION = 1 ; private static String DATABASE_PATH ; private final Context context ; private SQLiteDat