In Android Studio's Layout Editor, you can quickly build layouts by dragging widgets into a
visual design editor instead of writing the layout XML by hand. The editor can preview your layout
in different Android devices and versions, and you can dynamically resize the layout to be sure
it works well on different screen sizes. The Layout Editor is especially powerful when building a
new layout with
ConstraintLayout—a layout manager provided in a support
library that's compatible with Android 2.3 (API level 9) and higher.
This page provides an overview of the Layout Editor's interface and features. To learn more
about how to build a layout with
ConstraintLayout, see Build a Responsive UI with ConstraintLayout.
Introduction to the Editor
The Layout Editor appears when you open an XML layout file. Corresponding to the numbers in figure 1, the regions of the editor are as follows:
- Palette: Provides a list of widgets and layouts that you can drag into your layout in the editor.
- Component Tree: Shows the view hierarchy for your layout. Click an item here to see it selected in the editor.
- Toolbar: Provides buttons to configure your layout appearance in the editor and to edit the layout properties.
- Design Editor: Displays your layout in a combination of the Design and Blueprint views.
- Properties: Provides property controls for the currently selected view.
When you open an XML layout file, the Design editor opens by default (as shown in figure 1). To instead edit the XML in the Text editor, click the Text tab at the bottom of the window. While in the Text editor, you can also view the Palette, Component Tree, and Design editor (as shown in figure 2) by clicking Preview on the right side of the window. However, the Properties window is not available from the Text editor.
Tip: You can switch between Design and Text editors by pressing Control+Shift+Right/Left arrow.
Change the preview appearance
The buttons in the top row of the Design editor allow you to configure the appearance of your layout in the editor. This toolbar is also available in the Text editor's Preview window.
Corresponding to the numbers in figure 3, the buttons available are as follows:
- Design and blueprint: Select how you'd like to view your layout in the
editor. The Design
view displays a color preview of your
layout, while the Blueprint
view shows only outlines for each view. Or you can
view both Design + Blueprint
side by side.
Tip: You can toggle between these views by pressing B.
- Screen orientation: Rotate the device between landscape and portrait.
- Device type and size: Select the device type (phone/tablet, Android TV, or
Android Wear) and screen configuration (size and density). You can select from several
pre-configured device types and your own AVD definitions, or start a new AVD by selecting
Add Device Definition from the list.
Tip: You can resize the device size by dragging the bottom-right corner of the layout.
- API version: Select the version of Android on which to preview your layout.
- App theme: Select which UI theme to apply to the preview. Note: This works only for supported layout styles; thus many themes in this list result in an error.
- Language: Select the language to show for your UI strings. This list displays only the languages available in your string resources. If you'd like to edit your translations, click Edit Translations from the drop-down menu (see Localize the UI with Translations Editor).
- Layout Variants: Switch to one of your alternative layouts for this file, or create a new one (see Create a layout variant below).
Note: These configurations have no effect on your app's code or manifest (unless you chose to add a new layout file from Layout Variants); they affect only the layout preview.
Create a New Layout
When adding a new layout for your app, begin by creating a layout file in your project's default
layout/ directory so that it applies to all device configurations. Once you have a
default layout, you can create variations of that layout for specific device configurations (such as
for xlarge screens)—if that's what you want to do, skip to create a layout variant.
There are a few different ways to create a new layout, depending on your Project window view, but the following procedure is accessible from any view:
- In the Project window, click the module (such as app) in which you want to add a layout.
- In the main menu, select File > New > XML > Layout XML File.
- In the dialog that appears, enter a name for the file, the root layout tag, and the source set in which the layout belongs. Then click Finish.
A couple other ways to start a new layout file (although the dialogs that appear are different) are the following:
- If you've selected the Project view in the Project window: open the res directory for your app module, right-click the layout directory where you'd like to add the layout and then click New > Layout resource file.
- If you've selected the Android view in the Project window: right-click the layout folder and then select New > Layout resource file.
Create a layout variant
If you already have a layout and want to create an alternative version to optimize the layout for different screen sizes or orientations, follow these steps:
- Open your original layout file and be sure you're viewing the Design editor (click the Design tab at the bottom of the window).
- Click Layout Variants in the toolbar. In the dropdown list, either click a suggested variant such as Create Landscape Variant and you're done, or click Create Other and continue to the next step.
- In the dialog that appears, you simply need to define the resource qualifiers for the directory name. You can type it in Directory name or select from the Available qualifiers list, one at a time, and click Add .
- Once you've added all your qualifiers, click OK.
When you have multiple variations of the same layout, you can easily switch between them from the list that appears when you click Layout Variants .
For more information about how to create layouts for different screens, see Supporting Different Screen Sizes.
Convert a layout to ConstraintLayout
ConstraintLayout is a view group available in the Constraint Layout library, which
is included with Android Studio 2.2 and higher. It was built from the ground up along with the
Layout Editor, so everything is accessible from the Design editor and you never need to edit the XML
by hand. Best of all, its constraint-based layout system allows you to build most layouts without
nested any view groups.
For improved layout performance, you should convert older layouts to
ConstraintLayout. Android Studio has a built-in converter to help you do this:
- Open your existing layout in Android Studio and click the Design tab at the bottom of the editor window.
- In the Component Tree window, right-click the layout and then click Convert layout to ConstraintLayout.
To learn more about how to build a layout with
Build a Responsive UI with ConstraintLayout.
Add Views to Your Layout
Building a layout for your app requires that you understand the layout fundamentals, but Android Studio takes away many of the complexities of working directly in the XML files. The Layout Editor helps you accomplish a lot of work by dragging widgets into the Design editor and refining layout attributes in the Properties window.
To start building your layout, simply drag views from the Palette pane into the Design editor. As you place a view in the layout, the editor indicates the view's relationship with the rest of the layout in a manner appropriate to the type of layout in which you place it.
For example, video 1 shows how dragging a
TextView into a
ConstraintLayout creates constraints below and
aligned left to the above
TextView (this is with
When dragging views into a layout other than
ConstraintLayout, the Layout Editor responds
differently, as appropriate for the layout properties available for that layout.
Any errors detected in your layout are counted in the toolbar. To view them, click Show Warnings and Errors .
The appearance in the Design editor is for preview only. Although editing your layout in the Design editor can get you far with accurate appearances, you should run your app on an emulator or real device to verify the results.
Edit the view properties
Instead of editing your view properties in XML, you can do so from the Properties window (on the right side of the Layout Editor). This window is available only when the Design editor is open, so be sure you've selected the Design tab at the bottom of the window.
Select a view in the editor to see and edit common properties for that view. If you need to access more properties for the view, click View all properties .
When the selected view is a child of
ConstraintLayout, the Properties window
includes a view inspector at the top with several controls, as shown in figure 7. For more
information about the property controls for views in a
ConstraintLayout, see Build a
Responsive UI with Constraint Layout.