Activity Instructions & Code Snippets
Introduction to Android
Preparation work
- Create your Developer Profile
- Download Android Studio
- Any version is fine - stable one is Jellyfish
- Open Android Studio to the “Welcome to Android Studio” screen
Activity 1
- Create an Android App using the New Project wizard - “Empty Activity”
- Open the preview pane
- Alter the text to see the preview update
Activity 2
- Create an emulator Create and manage virtual devices
- Run your app Run apps on the Android Emulator
- (Optional) Run your app on a real device Run apps on a hardware device
Activity 3
- Add a
Column
- Create a
TextField
- Save the text input to a state
- Add a submit
Button
- Display the state as text on submit
Example Code
- (Extension) Add some logic to display an error message if the text field is empty on submit, disable the button if the input is not valid
Example Code
Adding AI with Gemini
Activity 4
- Create your API key: AI Studio
- (Optional) Add the Secrets Gradle plugin: secrets-gradle-plugin
Example Code
- (Optional) Enable
buildConfig
in the buildFeature config in appbuild.gradle.kts
Example Code
- (Optional) Add your API key to
local.properties
Example Code
- Add the Gemini API Client SDK
Example Code
Activity 5
- Send the user input to Gemini
- Display the output
- Run the app & try it out
Example Code
You will need to create a coroutine scope and wrap the
generateContent
call in a coroutine which is inside the button’sonClick
callval coroutineScope = rememberCoroutineScope() coroutineScope.launch { result = generativeModel.generateContent(input).text.orEmpty() }
If needed, add
verticalScroll(rememberScrollState())
to theColumn
modifier
to allow it to scroll.
Example prompt:
Recommend one pet for me that is not a usual domesticated animal and why
Activity 6
- Create 3 user inputs that are saved in variables:
- Where do you live? -
home
- What are your hobbies? -
hobbies
- What is your family like? -
family
- Where do you live? -
- Join the inputs into a prompt
- Display the result
- Run the app & try it out
Example Code
- Modify the model
generationConfig
&safetySettings
to produce different results. What produces the most interesting results?
You might like to use derivedStateOf to create the prompt so that it is updated every time the input text field state variables are changed
val input by remember { derivedStateOf { "prompt" } }
Example prompt:
Recommend one pet for me that is not a usual domesticated animal, I live in $home, I like to $hobbies and my family is $family.
Activity 7 (Extension)
- Update your prompt to request the output in a Json format
Example Code
- Parse the Json using Kotlin Serialization
Example Code
- Display the result in a formatted way
Example Code
Example prompt:
“Recommend one pet for me that is not a usual domesticated animal, I live in $home, I like to $hobbies and my family is $family. Return the result as a json object that fits the format of this data class: data class Pet( val name: String, val description: String, val photoUrl: String, )”
Example data class:
val name: String, val description: String, val photoUrl: String, )
Activity 8 (Extension)
- Integrate Coil to your app
- Use the url in your Json response to show the image of the animal
Example Code
- How can you modify your prompt to get better results?
Note: I have had a few issues here with getting images from Gemini that don’t result in 404s. Try different approaches to make your prompt and code more resiliant to this.