Skip to main content

Posts

ViewModel with Jetpack Compose

  Compose uses remember API to store object in memory. Stored value is returned during recomposition . remember helps us retain data across recompostion , but when configuration changes happen all stored values are lost . One way to overcome this is to use rememberSaveable . rememberSaveable saves any value that can be saved in a Bundle , so it will survive configuration changes.  But when we are using lot of data , for example a list we can cannot use a rememberSavble beacuse there is limit on amount of data that can be stored in Bundle . So we use ViweModel . ViewModel provide the ui state and access to the business logic located in other layers of the app. It also survives configuration changes. ViewModel handles events coming from the UI or other layers of the app and updates the state holder based on the events. We need to add the following dependency in our app level build.gradle to use ViewModel implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.4.1" F

Jetpack Compose with remember and mutableStateOf

  Jetpack Compose is a modern declarative UI Toolkit . In jetpack's declarative approach widgets are stateless and does not expose getter or setter function . So we cannot update UI with button.setText(String) or img.setImageBitmap()....  In Compose we build UI by defining set of Composable function. Composable function take in data and emit UI elements. So in Compose  only way to  update the UI is by calling the same Composable function with new Arguments. This Argument represents UI State. State in an app is any value that changes over time , this includes simple class variable to Room Database.  Any time a State is updated we need to call the Composable with new State to update the UI. This is called ReComposition .  This Demo Jetpack Compose app will help you understand the basics of Jetpack Compose. Screenshots of this App   This is our MainActivity package com.arun.androidtutsforu.democompose import android.os.Bundle import androidx.activity.ComponentActivity import androidx

Fragments In Android Kotlin

Fragments Represent Reusable portion of your apps UI  Fragments are used to define and manage UI of a single screen or portion of screen. Fragments has it's own layout , Life cycle and can handle input events Fragments can not exist on its own it must be hosted by an Activity or another Fragment Activities act as a frame that contains the fragment .Each fragment operates like a view in the activity but it has  has it's own layout , Life cycle and can handle input events. Activities are ideal place to put global elements around your app's UI like Navigation Drawer, Toolbar , Bottom Navigation and fragments are better suited to define UI of single screen or portion of screen. You can use multiple Fragments with single Activity ,  this gives more modularity and reusability to our code. Add the following dependency in your app level build.gradle file to use fragments implementation "androidx.fragment:fragment-ktx:1.3.6" Now create a new fragment  package com.arun.and

Simple Calculator With ViewModel and LIveData

This is a simple calculator with basic mathematical operations. You can download full source code of this project from Github https://github.com/arunkfedex/SimpleCalculator We are using ViewModel and LiveData so we need to add those dependencies in build.gradle file. build.gradle plugins { id 'com.android.application' id 'kotlin-android' } android { compileSdk 30 defaultConfig { applicationId "com.arun.androidtutsforu.simplecalculator" minSdk 21 targetSdk 30 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 ta

How to Open Android Emulator form Command Line and install Apk in Emulator

  You can also view this on my youtube channel How to Open Android Emulator from Commad Line 1.Open Command line 2.Change working directory to android sdk directory    cd  appdata/local/android/sdk/emulator 3.List all available Android virtual devices     emulator -list-avds 4. All your avds will be shown choose the avd_name you want to open    emulator -avd avd_name   5.Your Android virtual device will open up How to install APk file to emulator Drag the APK to Android emulator it will install automatically

DataBinding - ViewBinding in Android

ViewBinding is a feature that allow you to write code more easily.  First we will s ee an App without ViewBinding then we will enable ViewBinding in the App .Screenshot of our app is , it is asimple application when we  click the Button score Will Increase You can also see this tutorial in my youtube channel you can download source code of this project from GitHub https://github.com/arunkfedex/DemoNavGraphTest Layout file is 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" tools:context=".MainActivity"> <TextView android:id="@+id/text1

Navigation Android kotlin

  Navigation is the way user navigate between different fragments and activities . New navigation component helps us  visualize all the navigation in our application in a single navigation graph . Navigation graph is xml resource which contains all navigation related information in one centralized location. This include all individual content area(fragments) which are called destinations  and paths user can take in the applicatio known as actions. Navigation graph of our sample application is screenshots of our sample application implementing android navigation  Full source code of the sample application can be downloaded from the following github link https://github.com/arunkfedex/demoNavigation In this application we use navigation component to implement overflow menu and Navigation drawer. We also use Safe Args for sending data between fragments. 1.First we need to add following dependencies    For Navigati