How to Use Firebase ML Kit Smart Replies in Android?
We have seen using chatbots in Android for replying to the most common questions from the users. In this article, we will take a look at the implementation of Firebase ML Kit smart replies in Android. Firebase ML Kit smart replies are used to provide smart replies to the questions asked by the users using the Firebase ML Kit.
What we are going to build in this article?
We will be building a simple application in which we will be making a chatting-like interface in which the user will post his query in the chatbox and according to the user’s query we will get to see the message from Firebase ML Kit. A sample video is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language.
Step by Step Implementation
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Connect your app to Firebase
After creating a new project in Android Studio connect your app to Firebase. For connecting your app to firebase. Navigate to Tools on the top bar. After that click on Firebase. A new window will open on the right side. Inside that window click on Firebase ML and then click on Use Firebase ML kit in Android. You can see the option below screenshot.
After clicking on this option on the next screen click on Connect to Firebase option to connect your app to Firebase.
Step 3: Adding dependency for language translation to build.gradle file
Navigate to the Gradle Scripts > build.gradle(Module:app) and add the below dependency in the dependencies section.
// dependency for firebase core.
// Firebase ML dependency
// dependency for smart reply
Inside the same Gradle file. Add the below code in the android section.
Now sync your project and let’s move towards the implementation of Firebase ML Kit smart replies.
Step 4: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.
Step 5: Creating a modal class for storing our data
As we are displaying all our data in our RecyclerView. So we have to store this data in a modal class. For creating a modal class, Navigate to the app > java > your app’s package name > Right-click on it > New > Java class and name it as ChatMsgModal and add the below code to it. Comments are added inside the code to understand the code in more detail.
Step 6: Creating a layout file for each item of the message
Navigate to the app > res > layout > Right-click on it > New > layout resource file and name it as msg_rv_item and add the below code to it.
Step 7: Creating an adapter class for displaying this data
Navigate to the app > java > your app’s package name > Right-click on it > New > Java class and name it as ChatRVAdapter and add the below code to it.
Step 8: Working with the MainActivity.java file
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
Now run your app and see the output of the app.
Note: You will get a certain delay in reply from Firebase for the first time. Also, the response from Firebase will not be accurate as this response are send from the Firebase ML kit model.
Please Login to comment...