I’ll be teaching you how to create your own Google Assistant app in less than five minutes in this article. The nice part is that this doesn’t require any coding knowledge. Therefore, you may use this guide to create a Google Assistant app that uses flash cards, trivia, or a personality quiz if you have an app idea but don’t know how to code. All you have to do is connect your Action on Google project with a corpus.
Step 1: Set Up Your Android Project
- Open Android Studio and create a new project.
- Choose “Empty Activity” and set the language to Kotlin.
- Set the minimum SDK to API 21 (Lollipop) or higher.
Step 2: Add Dependencies
Open your build.gradle (app)
file and add the following dependencies:
dependencies {
implementation "com.google.android.gms:play-services-assistant:18.0.0"
// Other dependencies...
}
Step 3: Update AndroidManifest.xml
Make sure your AndroidManifest.xml
has the necessary permissions and configurations. Add the following inside the <application>
tag:
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Add the Assistant integration -->
<meta-data
android:name="com.google.android.gms.assistant.API_KEY"
android:value="YOUR_API_KEY_HERE" />
Step 4: Create a Simple User Interface
In res/layout/activity_main.xml
, add a simple UI:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="@+id/welcomeText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Say something to Google Assistant!"
android:textSize="20sp"/>
<Button
android:id="@+id/buttonSpeak"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Speak"/>
</LinearLayout>
Step 5: Implement the MainActivity
Now, let’s implement the logic in MainActivity.kt
:
package com.example.assistantapp
import android.content.Intent
import android.os.Bundle
import android.speech.RecognizerIntent
import android.speech.SpeechRecognizer
import android.speech.SpeechRecognizer.RESULTS_RECOGNITION
import android.widget.Button
import android.widget.TextView
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
private lateinit var welcomeText: TextView
private lateinit var buttonSpeak: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
welcomeText = findViewById(R.id.welcomeText)
buttonSpeak = findViewById(R.id.buttonSpeak)
buttonSpeak.setOnClickListener {
startVoiceRecognition()
}
}
private fun startVoiceRecognition() {
val intent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM)
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak now...")
speechRecognizer.launch(intent)
}
private val speechRecognizer = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == RESULT_OK) {
val data = result.data?.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS)
val spokenText = data?.get(0) ?: "No speech recognized"
welcomeText.text = spokenText
respondToAssistant(spokenText)
}
}
private fun respondToAssistant(spokenText: String) {
// Simple response based on spoken text
if (spokenText.contains("hello", true)) {
welcomeText.text = "Hello! How can I assist you?"
} else {
welcomeText.text = "You said: $spokenText"
}
}
}
Step 6: Run Your App
- Connect your Android device or start an emulator.
- Build and run your app.
- Tap the “Speak” button and talk to your Google Assistant!
Conclusion
This is a basic example of configuring an Android app with Kotlin to communicate with Google Assistant. It can be expanded by including more intricate intent handling and functionalities.
Happy coding!
If you have any questions or need further clarification, feel free to ask!