If you are new to android development world, you may have doubts about the word “layout”.

So let’s start here,

what is layout in android - www.androidride.com

In Android, layout describes the appearance of the app. Layouts are just resourced files. The instructions or content in layout decides to position your widget and make your user interface.

The layout contains view, viewgroup objects and <merge> tag. Just like HTML, It must have a root element. It can contain more GUI widgets like Button, TextView etc. Nested elements are also supported.

An app can have more than one screens based on developer needs. So you can have many layouts as you want.

Layout resource names must contain only lowercase a-z, 0-9, or underscore otherwise, you might end with an error.

How to create layout in android?

  • Using XML

You can use XML straightforward vocabulary to specify your UI. For beginners, you can check it out drag and drop feature. It helps us to drag a view which you want and drop it wherever you want in layout design. The XML code will automatically complete by android studio.

For localization (translating strings into different languages), XML layout would be a better idea.

Note: You must put xmlns:android attribute with android namespace inside root element.

For an example: assume LinearLayout as your root element.

  • Programmatically at runtime

It’s not generally recommended. There may be occasions to make your layout at runtime. But it mixes up UI code and code that controlling app behavior. So it may become difficult to tweak and refine user interface.

Where is layout files located at?

layout subdirectory in android studio

Android studio automatically creates a directory to hold your layout files – layout subdirectory.

In your project structure

app->res->layout .

all resource files stored in res (resources folder).

How to create a layout file in android studio?

Right Click on your default layout subdirectory

New -> Layout resource file -> Enter layout Name and click OK.

Also give details of the root element, source set, Directory name and other available qualifiers.

How to rename layout file in android studio?

Right click on your layout file which want to rename

Refactor -> Rename -> give new file name -> Refactor. You can use shift + F6 key also.



  • I think creating layouts with code is absolutely legitimate, too, as you have nicely shown in another of your articles. It is interesting, that neither Android Studio, nor Xamarin generates multiple layout skeletons for portrait phone, landscape phone, portrait tablet, landscape tablet format. Why is that? I guess, the reason is that because a phone or tablet can be rotated and the same layout (axml) is used when the phone/tablet is rotated to landscape. The other reason is that at which DPI resolution we regard a device as a tablet or a phone? Localization is implemented with completely different techniques, no need to create multiple screen designs (AXML). So, if we take all these into consideration, a fully dynamic layout creation gives far the greatest flexibility. The only legitimate reason to use AXML is when we have a team of a graphics/UI designer and a programmer/software engineer; the AXML parts are done by the designer, the application/business logic is written by the programmer.

    • admin

      creating layouts for different criteria (orientation,screen density and so on) is up to you. Best criteria matching layout doesn’t found, Android uses the default one. XML has it’s on importance, for reducing complexities in resource selection (localization etc), backward compatibility, theme selection and so on.