Add a voice assistant to an Android App(Java and Kotlin)

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.

  1. Open the IDE and select to start a new Android project.
  2. Select Empty activity as the project template. Then click Next.
  3. Enter a project name (for example, myapp) and select the language: Java or Kotlin. Then click Next.
  4. 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.

  1. Open the build.gradle file at the module level.
  2. 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:

This content is originally taken from the Alan documentation at https://alan.app/docs/tutorials/android/integrating-java-kotlin.

--

--

--

Alan is a B2B Voice AI platform for developers to deploy and manage Voice Interfaces for Enterprise Apps.

Love podcasts or audiobooks? Learn on the go with our new app.

VBCS Excel Plugin — Manage Common Lookups

Building a Prometheus Exporter

How to enable IntelliJ Idea Global Menu on Arch Linux

Connecting to Socket using Swift 5

Connecting to Socket using Swift 5

A real control center

CS50第三課 — notes part4

Use case: Smart HVAC for office

How cloud and open source technology help APAC businesses thrive

Two penguins dance hand in hand illustrating how cloud and open source technology help APAC businesses thrive.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alan AI

Alan AI

Alan is a B2B Voice AI platform for developers to deploy and manage Voice Interfaces for Enterprise Apps.

More from Medium

Cross Platform(IOS & Android)

Flutter coding in system

Android Runtime Permissions — Asking for multiple permissions

KMM Tutorial And Building a Sample App

Anime App with Kotlin