Build a UI with Layout Editor

 

n the Layout Editor, you can quickly build layouts by dragging UI elements into a visual design editor instead of writing the layout XML by hand. The design editor can preview your layout on 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. To learn more about layout fundamentals, see Layouts. To learn more about how to build a layout with ConstraintLayout, see Build a Responsive UI with ConstraintLayout.

 

Introduction to the Layout 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:

  1. Palette: List of views and view groups that you can drag into your layout.
  2. Component Tree: View hierarchy for your layout.
  3. Toolbar: Buttons to configure your layout appearance in the editor and to change some layout attributes.
  4. Design editor: Layout in Design or Blueprint view, or both.
  5. Attributes: 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 edit the layout 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. The Attributes window is not available from the text editor.

 

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.

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.

  1. 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 contains commands for creating a new layout variant.
  2. Device type and size: Select the device type (phone/tablet, Android TV, or Wear OS) 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.
  3. API version: Select the version of Android on which to preview your layout.
  4. App theme: Select which UI theme to apply to the preview. (This works only for supported layout styles; thus many themes in this list result in an error.)
  5. 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).

 

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:

 

 

  1. In the Project window, click the module (such as app) in which you want to add a layout.
  2. In the main menu, select File > New > XML > Layout XML File.
  3. 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:

  1. Open your original layout file and be sure you’re viewing the design editor (click the Design tab at the bottom of the window).
  2. Click Orientation for Preview  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.
  3. 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 
  4. 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

Convert a view or layout

 

You can convert a view to another kind of view, and you can convert a layout (view group) to another kind of layout.

  1. Click the Design tab at the bottom of the editor window.
  2. In the Component Tree, right-click the view or layout, and then click Convert view.
  3. In the dialog that appears, choose the new type of view or layout, and then click Apply.

 

 

Convert a layout to Constraint Layout

 

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 any nested view groups.

For improved layout performance, you should convert older layouts to ConstraintLayout.

To convert an existing layout to ConstraintLayout, do the following:

  1. Open your existing layout in Android Studio and click the Design tab at the bottom of the editor window.
  2. In the Component Tree window, right-click the layout and then click Convert layout to ConstraintLayout.

The command to specifically convert a layout to ConstraintLayout is more intelligent about inferring constraints and preserving layout than the simple Convert view command described in the previous section.

Find items in the Palette

To search for a view or view group by name in the Palette, click the Search  button at the top of the palette, or just start typing the name of the item when the Palette window is active.

You can find frequently used items in the Common category in the Palette. To add an item to this category, right-click on a view or view group in the Palette and click Favorite in the context menu.

Open documentation from the Palette

To open the Android Developers reference documentation for a view or view group, select the UI element in the Palette and press Shift+F1.

To open the Material Guidelines documentation for a view or view group, right-click the UI element in the Palette and select Material Guidelines from the context menu. If no specific entry exists for the item, 

Add views to your layout

To start building your layout, simply drag views and view groups from the Palette into the design editor. As you place a view in the layout, the editor displays information about the view’s relationship with the rest of the layout.

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—whether by clicking it in the Component Tree or in the design editor—the Attributes window shows the following, as indicated in figure 3:

  1. The Declared Attributes section, which lists attributes specified in the layout file. To add an attribute, click Add  at the top right of the section.
  2. View inspector with controls for width/height style. For views in a ConstraintLayout, this section also shows constraint bias and lists the constraints that the view uses. 
  3. A list of common attributes for the selected view. To see all available attributes, expand the All Attributes section at the bottom of the window.
  4. The Search button. Click this to search for a specific view attribute.
  5. The indicator to the right of each attribute value is solid  when the value is a resource reference, and empty  when it is not. This allows you to recognize hard-coded values at a glance. Clicking this indicator in either state opens the Resources dialog window, where you can select a resource reference for the corresponding attribute.
  6. Attributes with errors or warnings are highlighted, with red highlights for errors and orange highlights for warnings. One example of an error is an invalid entry in a layout-defining attribute (as pictured). One example of a warning is use of a hard-coded value when a resource reference is expected (as pictured).

In a TextView, you can choose between different sample text categories. When using sample text, Android Studio populates the text attribute of the TextView with your chosen sample data. Note that you can choose sample text via the Design-time View Attributes window only if the text attribute is empty.