Week 3 Class 1
First Android App
Goal of Today
Concepts for Today
1. Optionals in Kotlin -> Explained (Run time vs compile error))
2. Using Libraries in Android (Library vs Framework)
3. Android App UI -> Image set with a button
4. Declarative vs Imperative Programming?
5. XML vs Compose?
6. Lamda in Kotlin
Why this batch is most important?
Learn how to learn: XML + Compose -> 2022
Errors will come but we will fix together LIVE
Spots -> error
Rs. 1000
Steps to take
1. Designing the App: Figma
2. Why Declarative vs Imperative Approach?
3. Importing Libraries
4. Build and run
COding means handling errors, even when I recreate an app you will see errors and I will fix with you all
Homework Discussion
Read about Retrofit
Library vs Framework?
Android Revision (week2)
Constraint Layout
ImageView
Button Click
Declarative Programming vs Imperative Progamming
Imperative Programming
code: + how it is done?
// how to print list from list container? 4 lines of code
list = [1,2,3,4,5]
for x in list {
print(x)
}
Declarative
Answer using what -> / 1 line of code
getList()
12345
SQL -> Declarative
Select * from Employee
Why Declarative + Imperative
Why Compose + XML?
90%+ of Industry
Compose vs XML?
Compile-Time vs Run-Time Error
Which is Better?
lateinit var vs null object
Initialization | Property is uninitialized until explicitly initialized using lateinit. | Property is initialized to null by default. |
Nullable Property | No need for a ? after the property type, as it's not nullable. | Property is implicitly nullable (requires ? after the property type). |
Null Safety | Risky if not properly initialized; accessing an uninitialized lateinit property will result in a runtime exception. | Safe by default, as properties are initialized to null. Null checks or safe calls (?.) are required when accessing. |
Initialization Responsibility | Developer must ensure that the property is initialized before use. | No obligation to explicitly initialize the property; it can remain null. |
Use Cases | - Used when you can guarantee that the property will be initialized before being accessed. - To avoid nullable types and null checks, especially for properties that are expected to always have a value. | - Used when there's a possibility that the property might not be initialized immediately or can be left as null intentionally. - When you need to handle null values explicitly. |
Revision: 3 types of Functions
Simple Lamda Example
typealias Callback = (String) -> Unit
fun performTask(callback: Callback) {
// Simulating some task
val result = "Task completed successfully"
callback(result)
}
fun main() {
performTask { result ->
println("Callback received: $result")
}
}
import java.util.function.Consumer;
interface Callback {
void onComplete(String result);
}
class Task {
void performTask(Callback callback) {
// Simulating some task
String result = "Task completed successfully";
callback.onComplete(result);
}
}
public class Main {
public static void main(String[] args) {
Task task = new Task();
task.performTask(result -> System.out.println("Callback received: " + result));
}
}
What are interfaces
Interface vs Class
Feature | Classes | Interfaces |
---|---|---|
Method Implementation | Can have method implementations | Only method signatures, no implementations |
Inheritance | Single inheritance (extends one class) | Multiple inheritance (implements multiple interfaces) |
Usage | Defines objects, properties, and behavior | Defines contracts for behavior |
Abstraction | Abstraction of data and behavior | Higher level of abstraction, contract for behavior |
Multiple Inheritance | Not supported | Supported (through implementing multiple interfaces) |
Example | class MyClass { ... } | interface MyInterface { ... } |
without Lamda
public class Main {
public static void main(String[] args) {
Adder adder = new Adder();
int result = adder.add(5, 7);
System.out.println("Result of adding 5 and 7 is: " + result);
}
}
class Adder {
public int add(int a, int b) {
return a + b;
}
}
fun main() {
val add: (Int, Int) -> Int = { a, b -> a + b }
val result = add(5, 7)
println("Result of adding 5 and 7 is: $result")
}
with Lamda in Kotlin
ImageView and set Image in Android
1. Which is faster to share Image: Base64 or Downloading?
2. Why use Glide?
Using Library: Glide
1. Never reinvent wheel
2. Use Documentations
3. Gradle import
https://github.com/bumptech/glide
Add Permission
Add internet permisson
I Root cause (1 of 1)
java.lang.SecurityException: Permission denied (missing INTERNET permission?)
at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:150)
at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:103)
at java.net.InetAddress.getAllByName(InetAddress.java:1152)
at com.android.okhttp.Dns$1.lookup(Dns.java:41)
at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:178)
at com.android.okhttp.internal.http.RouteSelector.nextProxy(Ro
Homework
Show new screen on button click
Input from Screen 1 to Screen 2 (Data Passing)
Let's POLL
First Compose or Project?
Trick of the Day
Week 3 Class 1: First Android App (RestAPI + Glide)
By Harnoor Singh
Week 3 Class 1: First Android App (RestAPI + Glide)
- 309