Skip to content

Kotlin Codes

  • Home
  • Flutter
  • Kotlin
  • SwiftUI
  • About Me
  • Home
    • Blog
    • Privacy Policy
  • Flutter
    • Widgets In Flutter
      • Cupertino Widgets
      • iOS Styling Flutter
    • Database & Storage
    • State Management in Flutter
    • Performance Optimization
    • Networking & APIs
    • Testing & Debugging
  • Kotlin
    • Kotlin XML Development(Traditional View-based UI)
      • Introduction to XML UI Development
      • State Management and Architecture
      • Advanced Topics
      • Firebase and Cloud Integration
      • UI Components and Customization
      • Media and Visual Enhancements
      • Navigation and Data Handling
      • UI Performance Optimization
      • Networking and Data Management
    • Jetpack Compose
      • UI Elements
      • Kotlin Multiplatform
      • Accessibility
      • Animation
      • Core Concepts
      • Custom Drawing
      • Interoperability
      • Layouts
      • State Management
      • Modifiers
      • Navigation
      • Testing
      • Theming
      • Performance
    • Kotin-CodeChallenge
  • SwiftUI
  • About Me

Firebase Authentication Integration with XML UI

March 12, 2025May 15, 2025 Sayandh

Firebase Authentication provides a robust, secure, and easy-to-use authentication system for your mobile and web applications. While many modern Android projects leverage Jetpack Compose for UI development, there are still numerous projects that use traditional XML layouts. Integrating Firebase Authentication with XML UI involves setting up Firebase, designing the XML layouts, and handling authentication logic in your activities or fragments.

What is Firebase Authentication?

Firebase Authentication simplifies the process of implementing user authentication. It supports various authentication methods, including email/password, social media logins (Google, Facebook, Twitter, etc.), and phone number authentication. This service allows developers to focus on building app features rather than managing authentication infrastructure.

Why Use Firebase Authentication?

  • Easy Setup: Straightforward integration with Android, iOS, and web projects.
  • Multiple Providers: Supports various authentication methods, providing flexibility.
  • Secure: Offers secure authentication processes, managed by Google’s infrastructure.
  • Scalable: Built to handle a large number of users and authentication requests.

How to Integrate Firebase Authentication with XML UI

Here’s a step-by-step guide to integrating Firebase Authentication with an Android app using XML layouts:

Step 1: Set Up Firebase Project

  1. Create a Firebase Project: Go to the Firebase Console and create a new project.
  2. Register Your App: Add your Android app to the Firebase project by providing the package name, SHA-1 certificate, and other required details.
  3. Download google-services.json: Download the google-services.json file and add it to the app/ directory of your Android project.

Step 2: Add Firebase Dependencies

In your project’s build.gradle (Project level), add the Google Services plugin:

buildscript {
    dependencies {
        classpath 'com.google.gms:google-services:4.3.15' // Check for latest version
    }
}

In your app’s build.gradle (Module level), apply the plugin and add the Firebase Authentication dependency:

plugins {
    id 'com.android.application'
    id 'com.google.gms.google-services'
}

dependencies {
    implementation 'com.google.firebase:firebase-auth:22.0.0' // Check for latest version
    // Other dependencies
}

Sync your Gradle files after making these changes.

Step 3: Design XML Layouts

Create XML layouts for user registration and login. Here’s an example of a registration layout (activity_register.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp">

    <EditText
        android:id="@+id/editTextEmail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Email"
        android:inputType="textEmailAddress"
        android:layout_marginBottom="8dp"/>

    <EditText
        android:id="@+id/editTextPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Password"
        android:inputType="textPassword"
        android:layout_marginBottom="8dp"/>

    <Button
        android:id="@+id/buttonRegister"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Register"/>

</LinearLayout>

Similarly, create a login layout (activity_login.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp">

    <EditText
        android:id="@+id/editTextEmail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Email"
        android:inputType="textEmailAddress"
        android:layout_marginBottom="8dp"/>

    <EditText
        android:id="@+id/editTextPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Password"
        android:inputType="textPassword"
        android:layout_marginBottom="8dp"/>

    <Button
        android:id="@+id/buttonLogin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Login"/>

</LinearLayout>

Step 4: Implement Registration Activity

Create a RegisterActivity to handle user registration:

import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.firebase.auth.FirebaseAuth

class RegisterActivity : AppCompatActivity() {

    private lateinit var auth: FirebaseAuth
    private lateinit var editTextEmail: EditText
    private lateinit var editTextPassword: EditText
    private lateinit var buttonRegister: Button

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_register)

        auth = FirebaseAuth.getInstance()
        editTextEmail = findViewById(R.id.editTextEmail)
        editTextPassword = findViewById(R.id.editTextPassword)
        buttonRegister = findViewById(R.id.buttonRegister)

        buttonRegister.setOnClickListener {
            registerUser()
        }
    }

    private fun registerUser() {
        val email = editTextEmail.text.toString()
        val password = editTextPassword.text.toString()

        if (email.isEmpty() || password.isEmpty()) {
            Toast.makeText(this, "Please enter email and password", Toast.LENGTH_SHORT).show()
            return
        }

        auth.createUserWithEmailAndPassword(email, password)
            .addOnCompleteListener(this) { task ->
                if (task.isSuccessful) {
                    Toast.makeText(this, "Registration successful", Toast.LENGTH_SHORT).show()
                    finish() // Go back to login screen or main activity
                } else {
                    Toast.makeText(this, "Registration failed: ${task.exception?.message}", Toast.LENGTH_SHORT).show()
                }
            }
    }
}

Step 5: Implement Login Activity

Create a LoginActivity to handle user login:

import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.firebase.auth.FirebaseAuth

class LoginActivity : AppCompatActivity() {

    private lateinit var auth: FirebaseAuth
    private lateinit var editTextEmail: EditText
    private lateinit var editTextPassword: EditText
    private lateinit var buttonLogin: Button

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)

        auth = FirebaseAuth.getInstance()
        editTextEmail = findViewById(R.id.editTextEmail)
        editTextPassword = findViewById(R.id.editTextPassword)
        buttonLogin = findViewById(R.id.buttonLogin)

        buttonLogin.setOnClickListener {
            loginUser()
        }
    }

    private fun loginUser() {
        val email = editTextEmail.text.toString()
        val password = editTextPassword.text.toString()

        if (email.isEmpty() || password.isEmpty()) {
            Toast.makeText(this, "Please enter email and password", Toast.LENGTH_SHORT).show()
            return
        }

        auth.signInWithEmailAndPassword(email, password)
            .addOnCompleteListener(this) { task ->
                if (task.isSuccessful) {
                    Toast.makeText(this, "Login successful", Toast.LENGTH_SHORT).show()
                    // Start your main activity
                    val intent = Intent(this, MainActivity::class.java)
                    startActivity(intent)
                    finish()
                } else {
                    Toast.makeText(this, "Login failed: ${task.exception?.message}", Toast.LENGTH_SHORT).show()
                }
            }
    }
}

Step 6: Update AndroidManifest.xml

Declare the activities in your AndroidManifest.xml:

<activity android:name=".LoginActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<activity android:name=".RegisterActivity"/>
<activity android:name=".MainActivity"/>

Step 7: Create MainActivity (Optional)

Implement a MainActivity that displays some content after successful login. The contents of this activity is entirely dependant on your use-case.

import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.google.firebase.auth.FirebaseAuth

class MainActivity : AppCompatActivity() {

    private lateinit var auth: FirebaseAuth
    private lateinit var textViewCurrentUser: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        auth = FirebaseAuth.getInstance()
        textViewCurrentUser = findViewById(R.id.textViewCurrentUser)

        val currentUser = auth.currentUser
        if (currentUser != null) {
            textViewCurrentUser.text = "Logged in as: ${currentUser.email}"
        } else {
            textViewCurrentUser.text = "Not logged in"
        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp">

    <TextView
        android:id="@+id/textViewCurrentUser"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Not logged in"/>

</LinearLayout>

Best Practices

  • Input Validation: Always validate user inputs on both the client and server sides.
  • Password Security: Encourage users to use strong passwords and consider implementing additional security measures like multi-factor authentication.
  • Error Handling: Provide meaningful error messages to users when authentication fails.
  • UI Responsiveness: Use progress dialogs or loaders to provide feedback during authentication processes.

Conclusion

Integrating Firebase Authentication with XML UI in Android apps offers a straightforward way to implement user authentication. By setting up Firebase, designing XML layouts, and implementing the authentication logic in your activities, you can provide a secure and seamless authentication experience for your users. Adhering to best practices ensures a robust and user-friendly implementation.

Beyond This Article: Your Next Discovery Awaits

Android Accessibility (a11y) in Kotlin XML: A Developer’s Guide
Kotlin Extensions: Simplify View Manipulation in Android XML Development
Sharing UI Code: Jetpack Compose Across Platforms Guide
Best Practices for Navigation Graph in XML Layouts
Testing XML Layouts and UI Logic (Espresso Basics) in Kotlin Android
RecyclerView Optimization: setHasFixedSize(true) in Kotlin XML
Tagged with Android Authentication, Android Development, Android XML Layouts, Compose Firebase Authentication, Firebase Android Guide, Firebase Login, Firebase Registration, Flutter Mobile Security, User Authentication Android, XML UI Android
  • Firebase and Cloud Integration

Post navigation

Previous Post

Integrating Firebase Features with XML UI

Next Post

Integrating Google Maps in XML Layouts

Recents

  • Working with Firestore, Firebase’s Scalable NoSQL Cloud Database, for Storing and Retrieving Application Data in Flutter
  • Performing Integration Testing to Validate the Interaction Between Different Parts of Your Flutter Application
  • Using Packages Like upgrader to Facilitate the In-App Update Process in Flutter
  • Implementing In-App Updates to Allow Users to Update Your App Without Leaving It in Flutter
  • Analyzing Analytics Data to Gain Insights into User Engagement and Identify Areas for Improvement in Flutter
  • Dart
  • Flutter
    • Advanced Concepts
    • Animations & UI Enhancements
    • Data Handling (JSON, REST APIs, Databases)
    • Database & Storage
    • Input Widgets
    • iOS Styling Flutter
    • Layout Widgets
    • Navigation and Routing
    • Networking & APIs
    • Performance Optimization
    • Platform Integration (Native Features)
    • State Management (Provider, BLoC, Riverpod)
    • State Management in Flutter
    • Testing (Unit, Widget, Integration)
    • Testing & Debugging
    • UI Basics
    • Widgets In Flutter
      • Cupertino Widgets
  • Kotlin
    • Jetpack Compose
      • Accessibility
      • Animation
      • Core Concepts
      • Custom Drawing
      • Interoperability
      • Kotlin Multiplatform
      • Layouts
      • Modifiers
      • Navigation
      • Performance
      • State Management
      • Testing
      • Theming
      • UI Elements
    • Kotin-CodeChallenge
    • Kotlin XML Development(Traditional View-based UI)
      • Accessibility
      • Advanced Topics
      • Advanced Topics & Custom Views
      • Animation
      • Data Binding
      • Drawables
      • Firebase and Cloud Integration
      • Introduction to XML UI Development
      • Kotlin Integration & Patterns
      • Layouts
      • Media and Visual Enhancements
      • Navigation and Data Handling
      • Networking and Data Management
      • RecyclerView
      • State Management and Architecture
      • Styles & Themes
      • UI Components and Customization
      • UI Performance Optimization
      • View Binding
      • Views
      • XML Techniques
  • SwiftUI

© KotlinCodes. Explore the latest Kotlin tutorials, Flutter guides, and Dart programming tips. | Learn Kotlin | Flutter Development | Dart Programming | Flutter Widgets