Activity Instructions & Code Snippets

Introduction to Android

Preparation work

  1. Create your Developer Profile
  2. Download Android Studio
    • Any version is fine - stable one is Jellyfish
  3. Open Android Studio to the “Welcome to Android Studio” screen

Activity 1

  1. Create an Android App using the New Project wizard - “Empty Activity”
  2. Open the preview pane
  3. Alter the text to see the preview update

Activity 2

  1. Create an emulator Create and manage virtual devices
  2. Run your app Run apps on the Android Emulator
  3. (Optional) Run your app on a real device Run apps on a hardware device

Activity 3

  1. Add a Column
  2. Create a TextField
  3. Save the text input to a state
  4. Add a submit Button
  5. Display the state as text on submit
    • Example Code

  6. (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

  1. Create your API key: AI Studio
  2. (Optional) Add the Secrets Gradle plugin: secrets-gradle-plugin
    • Example Code

  3. (Optional) Enable buildConfig in the buildFeature config in app build.gradle.kts
    • Example Code

  4. (Optional) Add your API key to local.properties
    • Example Code

  5. Add the Gemini API Client SDK
    • Example Code


Activity 5

  1. Send the user input to Gemini
  2. Display the output
  3. 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’s onClick call

val coroutineScope = rememberCoroutineScope()
coroutineScope.launch {
   result = generativeModel.generateContent(input).text.orEmpty()
}

If needed, add verticalScroll(rememberScrollState()) to the Column modifier to allow it to scroll.

Example prompt:

Recommend one pet for me that is not a usual domesticated animal and why


Activity 6

  1. 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
  2. Join the inputs into a prompt
  3. Display the result
  4. Run the app & try it out
  • Example Code

  1. 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)

  1. Update your prompt to request the output in a Json format
    • Example Code

  2. Parse the Json using Kotlin Serialization
    • Example Code

  3. 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)

  1. Integrate Coil to your app
  2. Use the url in your Json response to show the image of the animal
    • Example Code

  3. 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.