Estimated time: 30 minutes
Welcome to the lab on leveraging Gemini AI in Android Studio. In this lab, you will create a simple app with a linear layout and basic widgets, guided by Gemini AI. The app will take user input and output the number of words in that input. The goal is for you not to write any code, but ask Gemini to generate the logic for you. The final result would look something like this.
After completing this lab, you will be able to:
This is an instructional lab that does not require a Skills Network lab environment. You will follow this lab on your local machine using Android Studio.
Gemini
and choose Java as the
language.com.example.gemini
. This is
important later on in the project.Open the activity_main.xml
file in the
res/layout
directory. This is the main view of the project
that opens when the app is launched. Switch to the "Design" view by
clicking on "Design" on top of the window.
Select and delete the existing text box that says "hello world" in the middle of the screen.
Convert the main
constraint layout to a linear
layout.
Android Studio might complain about missing orientation. Use the
quick fix to set the orientation to vertical
.
TextView
from the Palette into the
LinearLayout
.id
to
app_title
.text
to "Gemini AI Word Counter."textSize
to 32sp
.gravity
to center
.margin
to 24dp
.layout_gravity
to
center_horizontal
.EditText
from the Palette into the
LinearLayout
.id
to
user_input
.Delete the text
and set the
hint
to "Enter some text."inputType
is text
.layout_margin
to 44dp
.padding
to 16dp
.textSize
to 30sp
.minHeight
to 60dp
.Button
below the EditText
:Button
from the Palette into the
LinearLayout
.id
to
action_button
.text
to "Submit."layout_margin
to 24dp
.padding
to 16dp
.textSize
to 40sp
.minHeight
to 60dp
.TextView
from the Palette into the
LinearLayout
.id
to
result_text
.text
to "Results will be displayed here".textSize
to 30sp
.layout_margin
to 24dp
.padding
to 16dp
.The design should look like the following at this time.
In Android Studio, navigate to
File > Settings > Plugins
and search for "Gemini AI."
Accept the license and Install the Gemini AI plugin. This might
restart Android Studio.
Access the Gemini AI by clicking on the star-like button on the
right-hand panel in Android Studio.
You will notice a dialog to authorize Gemini.
At the end of the process, you will see a successful message. Go ahead and follow the wizard to sign into your Google account and authorize Gemini AI.
You can close this and go back to Android Studio. You will see a screen informing you that Gemini was authorized.
You should now see the AI panel populated with a dialog box with a question prompt.
Great! We are now ready to ask Gemini for help. The goal is to
add code that will take the user input and display the count of words in
the output view when the action button is clicked. Let's ask Gemini how
to do this by asking
I want to count the number of words in the user input (user_input) when the action_button is clicked. The results should be displayed in "result_text" widget. Use Java to write this code for me and tell me where to add it in the package com.example.gemini in this project. Thank you!
.
You can rephrase it as you like. Notice that some key pieces of
information were provided, such as using Java
and the
package com.example.gemini
. Make sure the package name
matches the package your MainActivity.java
file is in.
Remember AI response is not deterministic and you might see a different
answer with a different prompt.
Gemini should produce code for the MainActivity.java
file! How cool is this!
Not only that, it also explains the code below and gives you references to read more.
You can now copy and paste this code into
MainActivity.java
file.
That's it! We can run the application in the emulator now.
When first generating this code, an issue cropped up where Gemini
produced what was obviously an incorrect java code. The onclick listener
on the button was generated as publicvoid onClick
instead
of public void onClick
. This resulted in an error in the
java file.
After letting Gemini know that it had made a mistake, it quickly apologized and corrected the function!
Note: You may encounter different error messages in your code based on the AI's response. Please adjust your code accordingly.
Ensure your AVD is set up.
Select the "Run" button in Android Studio.
Select your AVD and wait for the application to launch.
Verify that the app appears as designed, the button click updates the result TextView with the word count of the entered text.
You have now successfully created an app with a linear layout, basic widgets, and hooked up the button click, leveraging Gemini AI to generate the application code. This foundational skill will be crucial as you continue to develop more complex AI-powered apps in Android Studio.
UL