You are on page 1of 10

Android Developers

Add Code from a Template


Android Studio provides code templates
that follow the Android design and
development best practices to get you on
the right track to creating beautiful,
functional apps. You can use templates to
create new app modules, individual
activities, or other specic Android project
components.

In this document
Add a project component
Select an activity template
Basic Activity
Empty Activity
Fullscreen Activity

Some templates provide starter code for

Login Activity

common usage contexts, such as navigation

Master/Detail Flow

drawers or login screens. You can choose

Navigation Drawer Activity

from these app module and activity templates

Scrolling Activity

when you rst create your project

Settings Activity

(https://developer.android.com/studio/projects/create-

Tabbed Activity

project.html),

when you add a new app module

within an existing project


(https://developer.android.com/studio/projects/add-appmodule.html),

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 les.
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
(https://developer.android.com/tools/support-library/features.html)

to include user interface

principles based on material design (https://developer.android.com/design/material/index.html).

Add a project component

Figure 1. The templates menu,


accessible through the File > New
menu or by right-clicking in the Project
window.

The list of templates provided in Android Studio is constantly growing. Android


Studio groups templates by the type of component that they add, such as an
Activity or an XML le, as shown in gure 1.
To add an Android project component using a template, use the Project

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 gure 1.
When you select the template you want to add, a corresponding wizard window
appears and asks for the component's conguration information, such as its name.
After you enter the conguration information, Android Studio creates and opens the
les 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.

Select an activity template


One of the most common uses of templates is adding new activities to an existing
app module. For example, to create a login screen for your apps users, add an
activity with the Login Activity template.

This section covers the commonly used activity templates for phone and tablet
apps. Android Studio also provides templates for a variety of different app module
types, including Android Wear, Android TV, and Cloud App Engine. You can view
templates for these different module types when creating an app module
(#CreateAppModule).

Templates also exist for more API-specic modules and activities,

such as Google AdMobs Ads and Google Maps.


The following phone and tablet templates provide you with the code components
for specic usage contexts, such as logging into an account, presenting a list of
items with details, or scrolling through a long block of text. Each can serve as either
an entire app module or an individual activity.

Basic Activity

This template creates a simple app with an app bar and a oating action button. It
acts as a starting point for your project by providing commonly used UI
components.
This template includes:
AppBar

(https://developer.android.com/training/appbar/index.html)

FloatingActionButton

(https://developer.android.com/reference/android

/support/design/widget/FloatingActionButton.html)

Two layout les: one for the activity and one to separate out text content

Empty Activity

This template creates an empty activity and a single layout le with sample text
content. It allows you to start from scratch when building your app module or
activity.
This template includes:
Single layout le with text content

Fullscreen Activity

This template creates an app that alternates between a primary fullscreen view and
a view with standard user interface (UI) controls. The fullscreen view is the default
and a user can activate the standard view by touching the device screen.
This template includes:

Touch listener implementation for hiding the standard view elements


Button that appears in the standard view but does not do anything
AppBar

(https://developer.android.com/training/appbar/index.html)

for the

standard view
Single layout le with both the fullscreen view and a frame layout for standard
view elements

Login Activity

This template creates a standard login screen. The user interface includes email
and password elds and a sign-in button. It is more commonly used as an activity
template than as an app module template.
This template includes:
AsyncTask

(https://developer.android.com/reference/android/os/AsyncTask.html)

implementation for handling network operations separately from the main user
interface thread
Progress indicator during network operations
Single layout le with the recommended login UI:
Email and password input elds
Sign-in button

Master/Detail Flow

This template creates an app that has both an item list display and a display for the
details of an individual item. Clicking on an item on the list screen opens a screen
with the items details. The layout of the two displays depends on the device that is
running the app.
This template includes:
Activity representing the list of items
Activity and fragment options for displaying an individual items details
FloatingActionButton

(https://developer.android.com/reference/android

/support/design/widget/FloatingActionButton.html)

on each screen

Collapsing toolbar (https://developer.android.com/reference/android/support/design/widget


/CollapsingToolbarLayout.html)

for the item detail screen

Alternative resource (https://developer.android.com/guide/topics/resources/providingresources.html#AlternativeResources)

layout les for different device congurations

Navigation Drawer Activity

This template creates a Basic Activity with a navigation drawer menu. The
navigation bar expands from the left or right side of your app and appears in
addition to the regular app bar.
This template includes:
Navigation drawer implementation with a DrawerLayout
(https://developer.android.com/reference/android/support/v4/widget
/DrawerLayout.html),

AppBar

corresponding event handlers, and example menu options

(https://developer.android.com/training/appbar/index.html)

FloatingActionButton

(https://developer.android.com/reference/android

/support/design/widget/FloatingActionButton.html)

Layout les for the navigation drawer and the navigation drawer header, in
addition to those from the Basic Activity template

Scrolling Activity

This template creates an app with a collapsing toolbar and a scrolling view for long
text content. As you scroll down the page, the toolbar, which can serve as a header,
automatically condenses, and the oating action button disappears.
This template includes:
Collapsing toolbar (https://developer.android.com/reference/android/support/design/widget
/CollapsingToolbarLayout.html)

in place of the regular AppBar

(https://developer.android.com/training/appbar/index.html)

FloatingActionButton

(https://developer.android.com/reference/android

/support/design/widget/FloatingActionButton.html)

Two layout les: one for the activity and one to separate out the text content into
a NestedScrollView

(https://developer.android.com/reference/android/support

/v4/widget/NestedScrollView.html)

Settings Activity

This template creates an activity that displays user preferences or settings


(https://developer.android.com/guide/topics/ui/settings.html)

PreferenceActivity

for an app. It extends the

(https://developer.android.com/reference/android/preference

/PreferenceActivity.html)

class and is more commonly used as an activity template

than as an app module template.


This template includes:
Activity that extends PreferenceActivity

(https://developer.android.com

/reference/android/preference/PreferenceActivity.html)

XML les (in the res/xml/ directory of your project) to dene the displayed
settings

Tabbed Activity

This template creates an app with multiple sections, swiping navigation


(https://developer.android.com/training/implementing-navigation/lateral.html),

and an app bar. The

sections are dened as fragments between which you can swipe left and right to
navigate.
This template includes:
AppBar

(https://developer.android.com/training/appbar/index.html)

Adapter that extends FragmentPagerAdapter

(https://developer.android.com

/reference/android/support/v4/app/FragmentPagerAdapter.html)

and creates a

fragment for each section


ViewPager

(https://developer.android.com/reference/android/support/v4/view

/ViewPager.html)

instance, a layout manager for swiping between sections

Two layout les: one for the activity and one for individual fragments

You might also like