Difference between content_main.xml and activity_main.xml?

AndroidXml

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

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....

Solution 2 - Android

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.

Solution 3 - Android

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.

Solution 4 - Android

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.

Solution 5 - Android

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.

Solution 6 - Android

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChris DanielView Question on Stackoverflow
Solution 1 - AndroidChordin4tionView Answer on Stackoverflow
Solution 2 - AndroidSherlockSunView Answer on Stackoverflow
Solution 3 - AndroidAnkur1994aView Answer on Stackoverflow
Solution 4 - AndroidSaleem KalroView Answer on Stackoverflow
Solution 5 - AndroidAyukNayrView Answer on Stackoverflow
Solution 6 - AndroidpgiituView Answer on Stackoverflow