Prerequisites
Before you begin, ensure you have the following installed:- Android Studio Hedgehog (2023.1.1) or newer
- JDK 11 or higher
- Android SDK with API level 24 or higher
- An Android device or emulator running Android 7.0 (API 24) or higher
Installation
Open in Android Studio
Open Android Studio and select “Open an existing project”. Navigate to the cloned directory and click “OK”.
Sync Gradle
Wait for Gradle to sync all dependencies. The app uses standard AndroidX libraries:
dependencies
Configure device or emulator
Set up your target device:For physical device:
- Enable Developer Options and USB debugging
- Connect your device via USB
- Verify the device appears in Android Studio’s device dropdown
- Open AVD Manager in Android Studio
- Create a new virtual device with API 24 or higher
- Ensure the emulator has microphone access enabled in settings
Using the app
Once the app is running, you’ll see a simple interface with a text field and a “Speak” button.Grant microphone permission
Tap the “Speak” button. On first use, the app will request permission to record audio. Tap “Allow” to grant the permission.
MainActivity.kt:125-143
Speak your message
After granting permission, tap “Speak” again. A Google speech recognition dialog will appear with the prompt “Speak now…”Speak clearly into your device’s microphone. The recognized text will automatically populate the text field.
Project structure
Here’s an overview of the key files in the project:Understanding the core components
MainActivity
TheMainActivity class in MainActivity.kt:45-61 sets up the app with edge-to-edge display and Jetpack Compose:
MainActivity.kt
VoiceRecognitionScreen
This composable function (MainActivity.kt:75-152) contains all the UI and voice recognition logic:
- Text field: A
BasicTextFieldwith a custom decoration box for placeholder text - Voice button: Triggers the speech recognition flow
- Result handler: Processes the recognized speech and updates the UI state
RecognizerIntent configuration
The app configures the speech recognizer with these parameters inMainActivity.kt:130-136:
Troubleshooting
Permission denied
If you see a “Permission denied” error:- Check that the
RECORD_AUDIOpermission is declared inAndroidManifest.xml - Ensure you granted the permission when prompted
- Go to device Settings > Apps > VoiceTotext > Permissions and verify microphone access
Speech recognition not available
If you get a “Failed to recognize speech” error:- Ensure your device has an active internet connection (Google’s speech recognition requires network access)
- Verify that Google app is installed and up-to-date on your device
- Check that the device microphone is working properly
Emulator microphone issues
If the emulator isn’t detecting your voice:- Open emulator Settings > Privacy > Microphone
- Ensure “VoiceTotext” has microphone permission
- In Android Studio, go to Tools > AVD Manager > Edit > Show Advanced Settings
- Verify “Enable Device Frame” is checked (enables microphone passthrough)
Next steps
Now that you have the app running, explore these topics:Speech Recognition
Deep dive into how speech-to-text works
UI Components
Learn about the Jetpack Compose UI components
API Reference
Explore detailed API documentation
Troubleshooting
Solutions to common issues