📱

Android Crash Course

Jul 11, 2024

Android Crash Course

Introduction

  • Presenter: Not specified
  • Platform: Brad's Channel
  • Prerequisites: Knowledge of Kotlin (link provided in video description)
  • Purpose: Build a to-do list app
  • Features:
    • Add to-do items
    • Check/uncheck items
    • Delete checked items
    • Note: No database included (data will be lost on app restart)
    • Detailed tutorials available on the presenter's channel

Why Kotlin?

  • Trending Alternatives: Flutter, React Native (cross-platform frameworks)
  • Recommendation: Start with native Android in Kotlin
    • More features in Kotlin than Java
    • Google is pushing Kotlin for Android development
    • Java is becoming outdated for Android

Installing Android Studio

  1. Download:
  • Search for "Android Studio download" on Google
    • First link: developer.android.com/studio
    • Accept license agreements
  1. Installation:
  • Run the downloaded file
    • Install Android Studio and Android Virtual Device
    • Choose standard installation
    • Select dark theme
    • Download required files, SDK tools

Setting Up the Project

  • Open Android Studio
  • Start a new project
    • Select "Empty Activity"
    • Set application name and package name
    • Ensure language is set to Kotlin
    • Minimum SDK: API 21 (Android Lollipop)
    • Click "Finish"
  • Project Structure:
    • MainActivity.kt: The main Kotlin file
    • activity_main.xml: Layout file associated with MainActivity
    • AndroidManifest.xml: Declares app settings and permissions
    • res (Resources) Folder: Contains drawable, layout, and values files
    • build.gradle (Module: app): Define application dependencies, minimum SDK, etc.

Building the Layout

  • Edit activity_main.xml:
    • Remove the default "Hello World" TextView
    • Add a RecyclerView for the to-do list
    • Use ConstraintLayout for responsive design
    • Add EditText for input
    • Add buttons for adding and deleting to-do items

Writing Kotlin Code

  • Create Model Class:
    • data class ToDo(val title: String, var isChecked: Boolean = false)
  • Create Adapter Class:
    • ToDoAdapter: Handles the logic for displaying the to-do list
    • Functions to Override:
      • onCreateViewHolder: Creates the ViewHolder for the RecyclerView
      • getItemCount: Returns the number of items in the list
      • onBindViewHolder: Binds the data to the views
    • ViewHolder Class: Holds the layout of a specific list item
  • Define Additional Functions:
    • addToDo: Adds a new to-do item to the list
    • deleteDoneToDos: Deletes all checked to-do items
  • Update MainActivity.kt:
    • Set up RecyclerView and Adapter
    • Define onClickListeners for add and delete buttons

Running the App

  • Set Up Emulator:
    • Use AVD Manager in Android Studio
    • Create a new virtual device (e.g., Pixel 3a with API 29)
    • Configure and start the virtual device
  • Build and Run:
    • Select the virtual device from the dropdown
    • Click the Run button
    • Test adding, checking, and deleting to-do items

Note on Rotations

  • Issue: List is cleared on device rotation
  • Reason: Activity is recreated on rotation, onCreate function resets the list
  • Solution: Advanced topic outside the scope of the crash course

Closing Remarks

  • Further Learning: Advanced android topics, databases, networking, media, etc.
  • Resources: GitHub link provided for source code
  • Thank You: Acknowledgment of Brad's platform and viewers