0% found this document useful (0 votes)
63 views4 pages

Kotlin Firebase Guide Final

Uploaded by

emmawat556son
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views4 pages

Kotlin Firebase Guide Final

Uploaded by

emmawat556son
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Kotlin & Firebase Android App Guide

Kotlin Basics

Kotlin Basics

Variables

- var name = "Chatu" // mutable

- val age = 20 // immutable

Data Types: String, Int, Double, Boolean, Float, Char

Functions

fun sayHi(name: String): String {

return "Hi $name!"

Conditionals

if (age > 18) { ... } else { ... }

Loops

for (i in 1..5) { println(i) }

Classes

class Person(val name: String, val age: Int)


Kotlin & Firebase Android App Guide

Null Safety

var name: String? = null

How to Build an App Using Kotlin

How to Build an App Using Kotlin

1. Setup

- Install Android Studio

- Create New Project -> Empty Activity -> Language: Kotlin

2. Files

- MainActivity.kt: Code

- activity_main.xml: UI layout

3. UI Example (XML)

<Button android:id="@+id/btnClick" android:text="Click Me!" />

4. Connect UI in Kotlin

val btn = findViewById<Button>(R.id.btnClick)

btn.setOnClickListener {

Toast.makeText(this, "Clicked!", Toast.LENGTH_SHORT).show()

}
Kotlin & Firebase Android App Guide

5. Run on Emulator or Device

Firebase Integration

Firebase Integration Guide

Step 1: Firebase Project

- Go to console.firebase.google.com

- Add project -> App (Android) -> Enter package name

- Download google-services.json and place it in the app/ folder

Step 2: Dependencies

- project-level build.gradle: classpath 'com.google.gms:google-services:4.3.15'

- app-level build.gradle:

apply plugin: 'com.google.gms.google-services'

dependencies {

implementation 'com.google.firebase:firebase-database-ktx'

implementation 'com.google.firebase:firebase-auth-ktx'

Step 3: Initialize Firebase

FirebaseApp.initializeApp(this)

Step 4: Realtime Database


Kotlin & Firebase Android App Guide

- Add data:

val db = FirebaseDatabase.getInstance().reference

db.child("users").push().setValue(User("Alice", 25))

- Read data:

db.child("users").addValueEventListener(object : ValueEventListener {

override fun onDataChange(snapshot: DataSnapshot) {

for (data in snapshot.children) {

val user = data.getValue(User::class.java)

override fun onCancelled(error: DatabaseError) {}

})

Step 5: Firebase Auth (Optional)

FirebaseAuth.getInstance().createUserWithEmailAndPassword(email, password)

Data Class

data class User(val name: String? = null, val age: Int? = null)

You might also like