Android Firebase Authentication using Kotlin, MVVM, LiveData, and View Binding (part III)

Alexandru Rotariu
3 min readFeb 25, 2023

--

Part III: User registration using Firebase Authentication

Welcome to the third post in our blog post series on Android Firebase Authentication using Kotlin, MVVM, LiveData and View Binding!

In this post, we will cover how to implement user registration using Firebase Authentication in your Android app. This involves creating the user registration layout using View Binding, implementing the registration functionality using Firebase Authentication, and displaying success or failure messages using LiveData.

This post is part of a longer series:

Let’s dive into how to implement user registration using Firebase Authentication in your Android app:

Creating the user registration layout using View Binding

The first step in implementing user registration using Firebase Authentication in your Android app is to create the user registration layout using View Binding.

To create the user registration layout:

1). Open the layout file for the user registration screen in your Android project.

2). Update the layout file to include the necessary views for the user registration form, such as EditTexts for the user’s email and password, and a button to submit the registration form.

<layout>
<LinearLayout
...>

<EditText
android:id="@+id/emailEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/email_hint"/>

<EditText
android:id="@+id/passwordEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/password_hint"/>

<Button
android:id="@+id/registerButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/register_button_text"/>

</LinearLayout>
</layout>

3). In the RegistrationActivity, create a binding object for the layout file using View Binding.

private lateinit var binding: ActivityRegistrationBinding

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

binding = ActivityRegistrationBinding.inflate(layoutInflater)
setContentView(binding.root)

// ...
}

Implementing the registration functionality using Firebase Authentication

Once the user registration layout is set up, the next step is to implement the registration functionality using Firebase Authentication.

1). Create an AuthViewModel

To handle authentication related functionality, we’ll create an AuthViewModel. This will be responsible for communicating with Firebase Authentication and handling any responses.

Create a new Kotlin class called AuthViewModel.kt and add the following code:

class AuthViewModel : ViewModel() {

private val _registerResult = MutableLiveData<AuthResult>()
val registerResult: LiveData<AuthResult> = _registerResult

fun registerUser(email: String, password: String) {
FirebaseAuth.getInstance().createUserWithEmailAndPassword(email, password)
.addOnCompleteListener { task ->
if (task.isSuccessful) {
_registerResult.value = AuthResult.Success
} else {
_registerResult.value = AuthResult.Error(task.exception?.message)
}
}
}
}

This creates an AuthViewModel that has a function called registerUser which takes an email and password as arguments. When called, this function will use the FirebaseAuth API to create a new user. If the operation is successful, a Success result is emitted on the registerResult LiveData. Otherwise, an Error result is emitted with the error message returned by Firebase.

2). Register the user on button click

To register the user when the register button is clicked, we’ll add an OnClickListener to the button in the RegistrationActivity.

registerButton.setOnClickListener {
val email = emailEditText.text.toString().trim()
val password = passwordEditText.text.toString().trim()
authViewModel.registerUser(email, password)
}

When the button is clicked, we retrieve the email and password entered by the user and call registerUser on the authViewModel.

3). Observe the result

Finally, we’ll observe the registerResult LiveData in the RegistrationActivity to display a success or error message to the user.

authViewModel.registerResult.observe(viewLifecycleOwner, { result ->
when (result) {
AuthResult.Success -> {
Toast.makeText(requireContext(), "Registration successful", Toast.LENGTH_SHORT).show()
}
is AuthResult.Error -> {
Toast.makeText(requireContext(), result.message, Toast.LENGTH_SHORT).show()
}
}
})

This observes the registerResult LiveData and displays a success message if the result is Success. If the result is an Error, it displays an error message.

And that’s it for implementing user registration!

In this post, we’ve covered setting up a Firebase project, integrating Firebase Authentication into an Android app, and implementing user registration with success and failure messages using LiveData. Stay tuned for the next post in the series where we’ll cover user login using Firebase Authentication.

Next post: User login using Firebase Authentication

--

--