Week 5 Update: Navigation & Authentication - Codeman239/TruckMate GitHub Wiki
## Overview
In Week 5, I established in‑app navigation and created a simple login flow. This lays the groundwork for secured areas of TruckMate and a modular screen structure.
## 1. Project Rename
- Updated `settings.gradle.kts`:
```kotlin
rootProject.name = "TruckMate"
-
Switched to version‑catalog
alias(...)
for core plugins:plugins { alias(libs.plugins.android.application) alias(libs.plugins.kotlin.android) alias(libs.plugins.kotlin.compose) id("kotlin-kapt") // no catalog alias available }
-
Added Navigation dependencies:
implementation("androidx.navigation:navigation-fragment-ktx:2.7.0") implementation("androidx.navigation:navigation-ui-ktx:2.7.0")
Created app/src/main/res/navigation/nav_graph.xml
:
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/nav_graph"
app:startDestination="@id/loginFragment">
<fragment
android:id="@+id/loginFragment"
android:name="com.codyhassey.truckmate.LoginFragment"
android:label="Login">
<action
android:id="@+id/action_login_to_home"
app:destination="@id/homeFragment" />
</fragment>
<fragment
android:id="@+id/homeFragment"
android:name="com.codyhassey.truckmate.HomeFragment"
android:label="Home" />
</navigation>
All UI XML lives in app/src/main/res/layout/
:
-
activity_main.xml Hosts the
NavHostFragment
:<?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"> <fragment android:id="@+id/nav_host_fragment" android:name="androidx.navigation.fragment.NavHostFragment" android:layout_width="0dp" android:layout_height="0dp" app:defaultNavHost="true" app:navGraph="@navigation/nav_graph" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
-
fragment_login.xml Two
EditText
(username, password) +Button
:<?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" android:layout_width="match_parent" android:layout_height="match_parent"> <EditText android:id="@+id/etUsername" android:layout_width="0dp" android:layout_height="wrap_content" android:hint="Username" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" android:layout_margin="16dp" /> <EditText android:id="@+id/etPassword" android:layout_width="0dp" android:layout_height="wrap_content" android:hint="Password" android:inputType="textPassword" app:layout_constraintTop_toBottomOf="@id/etUsername" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" android:layout_margin="16dp" /> <Button android:id="@+id/btnLogin" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Log In" app:layout_constraintTop_toBottomOf="@id/etPassword" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" android:layout_marginTop="24dp" /> </androidx.constraintlayout.widget.ConstraintLayout>
-
fragment_home.xml Simple
TextView
welcome message:<?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" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/tvWelcome" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Welcome to TruckMate!" android:textSize="24sp" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" android:layout_marginTop="32dp" /> </androidx.constraintlayout.widget.ConstraintLayout>
-
MainActivity.kt
package com.codyhassey.truckmate import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import androidx.navigation.findNavController import androidx.navigation.ui.setupActionBarWithNavController class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val navController = findNavController(R.id.nav_host_fragment) setupActionBarWithNavController(navController) } override fun onSupportNavigateUp() = findNavController(R.id.nav_host_fragment).navigateUp() || super.onSupportNavigateUp() }
-
LoginFragment.kt & HomeFragment.kt See full implementations in the
app/src/main/java/com/codyhassey/truckmate/
directory.