Firebase & Android Logo | Collected & Edited by Author ©

Building a Firebase Login App with Voice Input in Android Studio

Habibur Rahaman Fahim
3 min readJul 12, 2023

--

Introduction:

This tutorial walks through the process of creating a Firebase login app with voice input using Android Studio. The app allows users to log in by entering their email and password manually or using voice recognition. The tutorial covers the necessary steps to set up Firebase authentication, handle voice input, and navigate between activities.

Setting Up the Project:

Android Studio is a powerful development environment for building Android applications. With its extensive set of tools and resources, developers can create feature-rich apps for a variety of purposes. This tutorial focuses on building a Firebase login app that incorporates voice input for a seamless user experience.

To get started, open Android Studio and create a new project. Set the project name as “FirebaseLogin” and choose the desired package name and location. Select the “Empty Activity” template for the main activity and name it “MainActivity.”

Before diving into the code, ensure that Firebase is set up in your project. Create a new Firebase project in the Firebase console and follow the instructions to add Firebase to your Android app. After downloading the google-services.json file and adding the required dependencies in your project-level and app-level build.gradle files, proceed to the next steps.

Building the Main Activity:

Let’s begin with the MainActivity.java file. This file is responsible for handling the login functionality and integrating voice input. Start by importing the necessary classes and setting up the Firebase authentication and UI elements.

// Import necessary classes

public class MainActivity extends AppCompatActivity {

private static final int SPEECH_REQUEST_CODE_EMAIL = 1;
private static final int SPEECH_REQUEST_CODE_PASSWORD = 2;

private EditText emailEditText, passwordEditText;
private Button voiceEmailInputButton, voicePasswordInputButton, loginButton;
private FirebaseAuth firebaseAuth;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

firebaseAuth = FirebaseAuth.getInstance();

emailEditText = findViewById(R.id.emailEditText);
passwordEditText = findViewById(R.id.passwordEditText);
voiceEmailInputButton = findViewById(R.id.voiceEmailInputButton);
voicePasswordInputButton = findViewById(R.id.voicePasswordInputButton);
loginButton = findViewById(R.id.loginButton);

voiceEmailInputButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startSpeechRecognition(SPEECH_REQUEST_CODE_EMAIL);
}
});

voicePasswordInputButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startSpeechRecognition(SPEECH_REQUEST_CODE_PASSWORD);
}
});

loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email = emailEditText.getText().toString().trim();
String password = passwordEditText.getText().toString().trim();
signInWithEmailAndPassword(email, password);
}
});
}

private void startSpeechRecognition(int requestCode) {
// Implement speech recognition intent setup
// ...
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
// Handle speech recognition result
// ...
}

private void parseSpokenText(int requestCode, String spokenText) {
// Parse spoken text and update input field
// ...
}

private void signInWithEmailAndPassword(String email, String password) {
firebaseAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(this, task -> {
if (task.isSuccessful()) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
openNewActivity();
Toast.makeText(this, "Authentication succeeded. Welcome, " + user.getEmail() + "!", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(this, "Authentication failed.", Toast.LENGTH_SHORT).show();
}
});
}

private void openNewActivity() {
Intent intent = new Intent(MainActivity.this, SuccessActivity.class);
startActivity(intent);
finish();
}
}

Creating the Success Activity:

Now, create the SuccessActivity.java file, which represents the success activity. This activity will be displayed upon successful login.

// Import necessary classes

public class SuccessActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_success);
}
}

Designing the Layout Files:

Next, create the layout files for the main activity and success activity. In the res/layout directory, create two XML files named activity_main.xml and activity_success.xml.

<!-- activity_main.xml -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingTop="16dp"
android:paddingRight="16dp"
android:paddingBottom="16dp"
tools:context=".MainActivity">

<!-- UI elements for email input, password input, voice input buttons, and login button -->
<!-- ... -->

</RelativeLayout>
<!-- activity_success.xml -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<!-- Success message text view -->
<!-- ... -->

</RelativeLayout>

Conclusion:

In this tutorial, covered the process of building a Firebase login app with voice input using Android Studio. By following the step-by-step guide, developers can integrate Firebase authentication, handle voice input, and navigate between activities in their Android app.

Git Repository:

Check here for full code: Firebase Login App with Voice Input.

--

--