67

I am new to Android App development. I have Android Studio 1.4. In my layout folder I have two XML files (content_main.xml and activity_main.xml). I have been following Bucky's tutorial and he just has the activity_main.xml. Which one should I use?

Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79
Chris Daniel
  • 1,141
  • 2
  • 11
  • 11

6 Answers6

73

Unlike previous version, the new API 23 comes with the coordinatorLayout and stuff and just to make them simple to use android has distinguished activity's layout into two different layout i.e. content_main.xml & activity_main.xml.

I'll explain what they are for:

  1. activity_main.xml

this xml file is used to display coordinatorLayout, tabLayout, floatingActionButton, viewPager etc..

  1. content_main.xml

This xml file is use to display your stuff i.e. what you want to display to users.

So, if you are just asking in which xml you have to put your stuff, content_main.xml is the one....

Jad Chahine
  • 6,849
  • 8
  • 37
  • 59
Chordin4tion
  • 984
  • 6
  • 15
  • 10
    Just to elaborate - and see @SherlockSun's answer - he's the only one who explicitly points out that activity_main.xml contains an "include" for content_mail.xml. These two layout files are related, they describe different aspects of the overall layout. – RenniePet Aug 28 '16 at 00:20
  • That's right...But for those who are new to the android environment explaining things this way is much better...and the question was about which XML to use. – Chordin4tion Aug 30 '16 at 07:30
  • @Chordin4tion - What if I dont want to use coordinatorLayout, tabLayout, floatingActionButton or viewPager and want a simple layout? Do I still use content_main.xml and leave activity_main.xml unused? – Shubs Dec 04 '17 at 08:44
  • 2
    @Shubs That's totally up to you...If you don't want to use two different layout you can use `activity_main.xml` and remove the include tag from `activity_main.xml`. You can remove coordinator layout, tab layout and other stuff and replace parent layout with whatever layout you are planning to use. – Chordin4tion Dec 05 '17 at 10:30
  • 1
    "stuff i.e. what you want to display to users" –Floating action button is not something you want to display to users? – Julius Feb 15 '19 at 10:41
15

I think the content_main.xml is a part of activity_main.xml. Because there is include layout="@layout/content_main" in the activity_main.xml.

Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79
SherlockSun
  • 151
  • 1
  • 3
6

Android Studio 1.4 gives a default new option of content_main.xml in the activity. actually activity load the xml file of activity_main.xml for your layout. content_main.xml is the part of main.xml. you can use both for layout,but main.xml is necessary for your Activity. its a option to choose the content_main.xml for design of layout. Hope you get it.

Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79
Ankur1994a
  • 2,112
  • 2
  • 13
  • 18
2

The activity_main.xml is the "outer" part of the activity layout (toolbar, action button, etc.) and content_main.xml is the inner part where you put your own layout (the blank space).

content_main.xml file will be generated when you choose "blank activity". This activity will be having floating button by default at the bottom of the screen.

But when you choose "empty activity" then content_main.xml won't be generated and it won't be having floating button, but just simple Activity.

Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79
Saleem Kalro
  • 1,046
  • 9
  • 12
2

activity_main and content_main are linked to each other.

You can use either both. But to be specific, you should use activity_main.

let me explain this further.

activity_main, this is where the content_main is placed. This is a mother and child case, where activity_main is the mother and content_main is the child.

content_main, this is where you put your controls / content e.g button. This represents as the body of your design.

AyukNayr
  • 386
  • 2
  • 21
-2

These are just 2 xml layout files which basically represent 2 different layouts. You should use the file based on the layout you want to use in your android app.

Based on the tutorial it is simple that you should use activity_main.xml as it is the layout for the MainActivity.

You can also see the preview of the layout file in android studio as to how it will look in the app.

pgiitu
  • 1,671
  • 1
  • 15
  • 23
  • 1
    rellay? Are you sure? – B001ᛦ Dec 08 '15 at 11:17
  • use content_main.xml for adding all the elements – nikoo28 Feb 10 '16 at 20:49
  • 2
    This is not correct - they are not two different layouts. See @SherlockSun's answer - he's the only one who explicitly points out that activity_main.xml contains an "include" for content_mail.xml. So the two layout files are related, they describe different aspects of the overall layout. – RenniePet Aug 28 '16 at 00:12
  • Let's put it in another way, when you embed some html code to the html code, it does not mean that those html's are different pages. There is only one page but consisted of two pieces... – Nusrat Nuriyev Mar 21 '21 at 12:47