Contents Menu Expand
Logo
| Docs
Logo
| Docs
Logo

Introduction

  • Get started
  • How Alan works
    • Alan basics
    • Alan infrastructure
    • Deployment options
    • Voice processing
  • Alan Studio
    • Alan projects
    • Voice scripts
    • Versions and environments
    • Testing and debugging
      • Debugging Chat
      • Tools to simulate in-app behavior
      • Test View
      • Alan Studio logs
      • In-app testing
    • Alan Playground
    • Voice analytics
      • Projects dashboard
      • Analytics View
    • Popup editor
    • Billing and subscriptions
  • Alan button
    • Customizing the voice assistant
    • Setting up cohorts
    • Starting and stopping dialog sessions
  • Supported platforms
  • Recent Alan Platform updates

Server API

  • Script concepts
    • Commands and responses
    • Patterns
    • Slots
      • User-defined slots (static)
      • Dynamic slots
      • Regex slots
      • Predefined slots
    • Contexts
    • Built-in JavaScript libraries
    • Predefined script objects
    • Voice assistant lifecycle
    • Error handling and re-prompts
    • Alan button popups
  • Sending data from the app
    • authData
    • Visual state
    • Project API
  • User data
    • User events
    • Client object
  • API reference

Alan SDKs

  • Integrating with client apps
    • Web frameworks
      • React
      • Angular
      • Vue
      • Ember
      • JavaScript
      • Electron
      • Cross-platform frameworks
      • Server-side rendering
    • iOS
    • Android
    • Ionic
    • Apache Cordova
    • Flutter
    • React Native
  • Client API methods
  • Alan handlers
    • onCommand handler
    • onButtonState handler
    • onEvent handler

Samples & Tutorials

  • How-tos
  • Tutorials
    • Web
      • Building a voice assistant for web
      • Creating a voice-enabled food delivery app: complete tutorial
        • Step 1: Create a web app
        • Step 2: Create a voice assistant
        • Step 3: Integrate Alan with the app
        • Step 4: Add a voice command
        • Step 5: Specify options and alternatives
        • Step 6: Pick out important data with slots
        • Step 7: Add several items
        • Step 8: Send a command to display items in the cart
        • Step 9: Use slot labels to display the order
        • Step 10: Remove items with voice
        • Step 11: Highlight named items
        • Step 12: Add the checkout context
        • Step 13: Get the delivery time
        • Step 14: Capture a comment
        • Step 15: Populate fields with the delivery details
        • Step 16: Make an API call to get ingredients
        • Step 17: Get the app visual context
        • Step 18: Get the balance
        • Step 19: Let Alan start the dialog
        • Step 20: Show a popup next to the Alan button
      • Using dynamic slots in patterns
      • Making a Web API call from the voice script
    • Web frameworks
      • Building a voice assistant for a React app
      • Building a voice assistant for an Angular app
      • Building a voice assistant for a Vue app
      • Building a voice assistant for an Ember app
      • Building a voice assistant for an Electron app
    • iOS
      • Building a voice assistant for an iOS app
      • Navigating between views
      • Passing the app state to the voice script
      • Highlighing items with voice
      • Triggering voice script actions without commands
      • Playing a greeting in an app
    • Android
      • Building a voice assistant for an Android Java or Kotlin app
      • Navigating in an Android app with voice (Kotlin)
      • Passing the app state to the voice script (Kotlin)
      • Sending data from the app to the voice script (Kotlin)
    • Flutter
      • Building a voice assistant for a Flutter app
      • Navigating between screens
      • Passing the app state to the voice script
      • Sending data to the voice script
    • Ionic
      • Building a voice assistant for an Ionic Angular app
      • Navigating between tabs (Ionic Angular)
      • Passing the app state to the voice script (Ionic Angular)
      • Building a voice assistant for an Ionic React app
      • Navigating between tabs (Ionic React)
    • React Native
      • Building a voice assistant for a React Native app
      • Sending commands to the app
      • Passing the app state to the voice script
      • Triggering activities without voice commands
      • Navigating between screens with voice
  • App showcase
    • Appointment scheduling app (React)
    • Shrine e-commerce app (Flutter)
    • Food delivery app (Ionic)
    • Order drinks app (Angular)
  • FAQ
  • Alan examples
    • Voice script examples
    • Integration code examples
  • Videos
    • Alan Studio videos
    • Alan Platform videos
    • Integration videos
    • Voice-enabled apps
    • Alan AI Udemy course (web app)
  • Alan cheat sheet

Navigating in an Android app with voice (Kotlin)¶

While interacting with the voice assistant built with the Alan Platform, users can perform actions in the app with voice. For example, they can give commands to navigate to another screen, select an item in the list, enable or disable options. To achieve this, you need to send commands from the voice script to the app and handle them in the app.

In this tutorial, we will add voice commands that will allow us to navigate between tabs in the app.

YouTube

If you are a visual learner, watch this tutorial on Alan AI YouTube Channel.

What you will learn¶

  • How to complete tasks in the Android app with voice

  • How to navigate in the Android app with voice

  • How to send commands from the voice script and handle them in the Android app

What you will need¶

For this tutorial, we will use the app created in the Building a voice assistant for an Android Java or Kotlin app tutorial.

Step 1: Add voice commands to navigate between tabs¶

First, we need to add new voice commands to navigate between tabs with voice. In Alan Studio, open the project and in the code editor, add the following intents:

Voice script¶
intent('Open the second tab', p => {
    p.play({command: "openTab", tab: 1});
    p.play('Opening the second tab');
});

intent('Go back', p => {
    p.play({command: "openTab", tab: 0});
    p.play('Going back to the first tab');
});

When we say one of these phrases, two things will happen:

  • Alan will send the command provided in the intent to the Android app. To send the command, we need to specify a JSON object in the p.play function. Here the object contains the command name — openTab, and the index of the tab to be opened in the app.

  • Alan will play back the action confirmation to us.

Step 2: Handle commands on the app side¶

When we say Open the second tab or Go back, Alan now sends a command to the Android app. We need to handle this command in the app and make sure an appropriate action is performed. To do this, we will use the onCommand handler.

In the IDE, open the MainActivity.kt file and update the code for handling commands in the Alan button block to the following:

MainActivity.kt¶
class MainActivity : AppCompatActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    val alanCallback: AlanCallback = object : AlanCallback() {
      /// Handling commands from Alan Studio
      override fun onCommand(eventCommand: EventCommand) {
        try {
          val command = eventCommand.data
          val commandName = command.getJSONObject("data").getString("command")
          when (commandName) {
            /// Navigating between tabs when the openTab command is received
            "openTab" -> {
              val tab = eventCommand.data.getJSONObject("data").getString("tab").toInt()
              tabs.getTabAt(tab)?.select()
            }
          }
        } catch (e: JSONException) {
          e.message?.let { Log.e("AlanButton", it) }
        }
      }
    };

    /// Register callbacks
    alanButton?.registerCallback(alanCallback);
  }
}

Here is how it works: when the Android app receives the openTab command from the voice script, the tab index is saved to the tab variable and used to open the necessary tab.

You can test it: run the app, tap the Alan button and say: Open the second tab. The second tab in the app will be open. Then say Go back, and you will get back to the first tab.

What’s next?¶

Have a look at the next tutorial: Passing the app state to the voice script.

Next
Passing the app state to the voice script (Kotlin)
Previous
Building a voice assistant for an Android Java or Kotlin app
Alan® is a trademark of Alan AI, Inc. Handcrafted in Sunnyvale, California
Download Alan Playground to see how Conversational Voice Experiences can add value to your application or test your Alan Studio Projects.
On this page
  • Navigating in an Android app with voice (Kotlin)
    • What you will learn
    • What you will need
    • Step 1: Add voice commands to navigate between tabs
    • Step 2: Handle commands on the app side
    • What’s next?