Process involved in building a voice assistant for an Android app
You can create a voice assistant or a chatbot for an Android app written in Java or Kotlin using the Alan Android voice SDK. In this tutorial, let’s create a simple Android app, integrate a voice assistant using Alan, and then test in on a device. Once the process is completed, the app will allow users to tap on the interact with the voice assistant.
From this tutorial you will learn
- How to add a voice assistant to an Android app
- How to write simple voice commands for an Android app
Prerequisites before you get started
- Firstly, sign up for Alan Studio.
- Now, create a project in the Alan Studio.
- You need set up the Android environment and make sure that it is functioning properly. For details, see Android developers documentation.
- Ensure that the device is connected to the Internet so that the Android app will be able to communicate with the Alan Cloud to run the voice scripts.
Step 1: Create a basic Android app
Let’s create a basic Android app with a single screen in this tutorial.
- Open the IDE and select to start a new Android project.
- Select Empty activity as the project template. Then click Next.
- Enter a project name (for example, myapp) and select the language: Java or Kotlin. Then click Next.
- Click on Finish.
Step 2: Integrating the Android app with Alan
Now, let’s do the following to add the Alan button to the Android app.
- Open the
build.gradle
file at the module level. - In the
dependencies
block, add the dependency configuration for the Alan Android SDK.
//build.gradle file at the module level
dependencies {
...
//Add Alan SDK dependency
implementation 'app.alan:sdk:4.7.15'
}
3. Then sync the project with the gradle file. Below is how your build.gradle
file should look like:
4. Too add the XML layout for the Alan button to the main app activity, open the main_activity.xml
file and add the following to it:
<com.alan.alansdk.button.AlanButton
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:id="@+id/alan_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Below is how your main_activity.xml
file should look like:
5. Add the alanConfig
object to the app. This object describes the parameters that are provided for the Alan button. Now, open the MainActivity.java
or MainActivity.kt
file and add the following code to the MainActivity
class:
Below is the Java script:
//MainActivity.java file
public class MainActivity extends AppCompatActivity {
...
@Override protected void onCreate(Bundle savedInstanceState) {
...
// Alan button
AlanButton alanButton = findViewById(R.id.alan_button);
// Alan config object
AlanConfig alanConfig = AlanConfig.builder()
.setProjectId("")
.build();
alanButton.initWithConfig(alanConfig);
}
}
Below is the Kotlin script:
//MainActivity.kt file
class MainActivity : AppCompatActivity() {
...
override fun onCreate(savedInstanceState: Bundle?) {
...
// Alan config object
val config = AlanConfig.builder()
.setProjectId("")
.build()
alan_button.initWithConfig(config)
}
}
6. To setProjectId
, add the Alan SDK key for our project. In the Alan Studio, at the top of the code editor click Integrations and copy the key value from the Alan SDK Key field to get the key.
7. In the MainActivity.java
or MainActivity.kt
file, import the necessary classes. Below is how your main activity file should look like:
8. Now, run the app.
After the app is built, it will be launched on the connected device. Tap the Alan button and say: Hello world
.
Alan will not give an appropriate response if you try to ask: How are you doing?
. The voice script in the Alan Studio does not contain the necessary voice commands.
Step 3: Adding voice commands to interact with Alan
To interact with Alan let’s add some voice commands. Open the project in the Alan Studio and in the code editor, add the following intents:
intent (`What is your name?`, p => {
p.play(`It's Alan, and yours?`);
});intent (`How are you doing?`, p => {
p.play(`Good, thank you. What about you?`);
});
Now, tap the Alan button and ask: What is your name?
and How are you doing?
Alan will give respond with the intents we have provided.
Here’s what you can do next
Now, you can proceed with building a voice interface with Alan. Look into some helpful resources listed below:
- Here is our next tutorial that you can look into: Navigating between screens in an Android app.
- To learn about Alan concepts and functionality you can use to create a voice script go to Server API.
- In Alan Git, get the Android Java example app or Android Kotlin example app. Use these example apps to see how integration for Android apps can be implemented.
This content is originally taken from the Alan documentation at https://alan.app/docs/tutorials/android/integrating-java-kotlin.