Add Code From A Template - Android Studio - Android Developers
Add Code From A Template - Android Studio - Android Developers
Some templates provide starter code for common usage contexts, such as navigation drawers or login screens. You can
choose from these app module and activity templates when you first create your project (/studio/projects/create-project), when
you add a new app module within an existing project (/studio/projects/add-app-module), or when you add a new activity within an
app module.
In addition to activities, you can also add other Android project components to an existing app using templates. These
templates include both code components, such as services and fragments, and non-code components, such as folders and
XML files.
This page discusses how to add Android project components like activities to your project and describes the commonly used
activity templates available in Android Studio. Note that most templates depend on the Android Support Library
(/tools/support-library/features) to include user interface principles based on material design (/design/material).
https://developer.android.com/studio/projects/templates 1/14
10/24/24, 3:26 PM Add code from a template | Android Studio | Android Developers
window. Right-click on the folder in which you want to add the new component, and select
New. Based on what components can be added to the folder you clicked on, you then see a
list of template types like those shown in figure 1.
When you select the template you want to add, a corresponding wizard window appears and
asks for the component's configuration information, such as its name. After you enter the
configuration information, Android Studio creates and opens the files for your new
component. It also runs a Gradle build to sync your project.
Although you can also use the File > New menu of Android Studio to create a new Android
project component, navigating to your desired folder in the Project window ensures that you
create the component in the correct place.
One of the most common uses of templates is adding new activities to an existing app accessible through the File > New
module. There are templates for creating screens for logging into an account, presenting a list menu or by right-clicking in the
of items with details, or scrolling through a long block of text. Project window.
Android Studio also provides templates for a variety of different app module types, including
Wear OS, Android TV, and Cloud App Engine. You can view templates for these different module types when adding a project
component (#FindTemplates). Templates also exist for more API-specific modules and activities, such as Google AdMobs Ads
and Google Maps.
One of the most commonly used templates is the Empty Compose Activity template, which creates an empty activity with a
sample composable and a preview of the composable. It lets you to start from scratch when building your app module or
activity.
https://developer.android.com/studio/projects/templates 2/14
10/24/24, 3:26 PM Add code from a template | Android Studio | Android Developers
https://developer.android.com/studio/projects/templates 3/14
10/24/24, 3:26 PM Add code from a template | Android Studio | Android Developers
https://developer.android.com/studio/projects/templates 4/14
10/24/24, 3:26 PM Add code from a template | Android Studio | Android Developers
https://developer.android.com/studio/projects/templates 5/14
10/24/24, 3:26 PM Add code from a template | Android Studio | Android Developers
https://developer.android.com/studio/projects/templates 6/14
10/24/24, 3:26 PM Add code from a template | Android Studio | Android Developers
https://developer.android.com/studio/projects/templates 7/14
10/24/24, 3:26 PM Add code from a template | Android Studio | Android Developers
https://developer.android.com/studio/projects/templates 8/14
10/24/24, 3:26 PM Add code from a template | Android Studio | Android Developers
Copy and paste the API key into the New Project wizard.
https://developer.android.com/studio/projects/templates 9/14
10/24/24, 3:26 PM Add code from a template | Android Studio | Android Developers
Important: Remember, your API key is a secret! Don't share it with anyone, and it is strongly recommended that you don't check in an API key
into your version control. The Gemini API Starter template stores the API key in a local.properties file (which is located in your project's root
https://developer.android.com/studio/projects/templates 10/14
10/24/24, 3:26 PM Add code from a template | Android Studio | Android Developers
directory, but excluded from version control) and uses the Secrets Gradle plugin (https://github.com/google/secrets-gradle-plugin) for Android
to read the API key as a build configuration variable.
Learn more
Learn more about the Google AI SDK for Android by reviewing the Google AI SDK for Android quick start guide
(http://ai.google.dev/tutorials/android_quickstart?utm_source=android&utm_medium=referral). And for even more code samples, you can
import the Generative AI code sample into Android Studio through File > New > Import Sample and searching for
Generative AI Sample.
https://developer.android.com/studio/projects/templates 11/14
10/24/24, 3:26 PM Add code from a template | Android Studio | Android Developers
https://developer.android.com/studio/projects/templates 12/14
10/24/24, 3:26 PM Add code from a template | Android Studio | Android Developers
Try out Gemini's chat, text, and multi-modal capabilities in the sample app.
Content and code samples on this page are subject to the licenses described in the Content License (/license). Java and OpenJDK are trademarks or
registered trademarks of Oracle and/or its affiliates.
https://developer.android.com/studio/projects/templates 13/14
10/24/24, 3:26 PM Add code from a template | Android Studio | Android Developers
https://developer.android.com/studio/projects/templates 14/14