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 change some layout attributes.
- Design Editor: Displays your layout in a combination of the Design and Blueprint views.
- Attributes: Provides controls for the selected view's attributes.
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 by clicking Preview on the right side of the window. However, the Attributes window is not available from the text editor.
Tip: Switch between design and text editors by pressing Alt + Shift + Left/Right Arrow (Control + Shift + Left/Right Arrow on Mac).
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; select either the Design view (a real-world
preview of your layout), the Blueprint view (only outlines for each
view), or Design + Blueprint for both side by side.
Tip: You can toggle between these views by pressing B.
- Screen orientation and layout variants: Select between landscape and portrait screen orientation, or other screen modes for which your app provides alternative layouts, such as "night mode." This menu also helps you create a new layout variant (see Create a layout variant below). .
- 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).
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 layout variations for specific device
configurations (such as for xlarge screens).
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 Orientation in Editor 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 Attributes 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 attributes available for that layout.
If any issues are detected in your layout, they are indicated in the Component Tree with an exclamation icon next to the corresponding view. To view the error details, click the icon. You may also click Show Warnings and Errors in the toolbar to see all known issues in a window below the editor. From this window you can also enable Show issues on the preview, which adds a warning or error icon to each corresponding view in the preview (in the design view only, not the blueprint view).
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 view attributes
Instead of editing your view attributes in XML, you can do so from the Attributes 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.
When you select a view, the Attributes window shows the following, as indicated in figure 5:
- View inspector with controls for width/height style, margins, and bias
(available only for views in a
ConstraintLayout). For more information, see Build a Responsive UI with ConstraintLayout.
- A list of common attributes for the selected view. To see all available attributes, click View all attributes at the top of the window.
- Favorite attributes you've selected. To add attributes, click View all attributes and then click the star that appears when you hover your mouse over the left side of a attribute name.
To search for a specific view attribute, click View all attributes and then click Search at the top of the window.
Download and apply fonts to text
When using the Android Support Library v26.0.0 or higher (or when your
minSdkVersion is 26 or higher), you can select from hundreds of fonts by
following these steps:
- In the Layout Editor, click the Design tab to view your layout in the design editor.
- Click a text view.
- In the Attributes window, expand textAppearance and then click to expand the fontFamily box.
Scroll to the bottom of the list and click More Fonts to open the Resources dialog.
In the Resources dialog, select a font by browsing the list or typing into the search bar at the top. If you select one listed under Downloadable, then you can either click Create downloadable font to load the font at runtime (as a downloadable font), or click Add font to project to package the TTF font file in your APK. (The fonts listed under Android are provided in the Android system so do not need to be downloaded or bundled in your APK.)
- Click OK.