Week 3 Class 2

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 -> Done

4. Declarative vs Imperative Programming? -> done

5. XML vs Compose? -> Done

6. Lamda in Kotlin

Steps to take

1. Designing the App: Figma

2. Why Declarative vs Imperative Approach?

3. Importing Libraries -> not reinvent the wheel

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

Rs. 1000 Bounty

Compile-Time vs Run-Time Error

Which is Better?

Optionals/ Nullables

Object -> let's say employee

Accessing Object is like opening a box if box is empty (null) you get error be careful

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.

Downloading image vs locally

Project -> 2 Image

10 Images -> in App

More images -> update app

push app -> play store

Images -> Server

Images -> 50-100kb

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));
    }
}

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

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 { ... }

Week 3 Class 2: First Android App (RestAPI + Glide) (Continued)

By Harnoor Singh

Week 3 Class 2: First Android App (RestAPI + Glide) (Continued)

  • 246