• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps content.xml vs activity_main.xml

I have just gotten started into making android apps coming from a half decent knowledge of Java. I am trying to follow this tutorial by the new boston as it was published earlier this year.

My problem is that for the life of me I cannot figure out what the content.xml file does. I'm not sure if I am just not far enough in or what, but every time he says edit the activity_main file he has solely that in his layout folder while I have that and the content.xml file in there as well. When I check the preview screen, it seems to be that the content.xml file is the file that I want to edit to add widgets (from the component tree and the general xml file code), but the preview phone thing looks exactly the same on both files even though I add the widgets to the content_main.xml file. TBH I don't even know where the came from as it did not appear in the new project creation process.

Can I get an explanation for what the difference is between the two and what I am missing?
Thanks.
 
What layout name do you use to set the content view in your activity code? Have you got that far yet?
Could be that you accidentally copied the activity_main.xml file?
Normally there is only one layout XML file associated with an activity.

The system does auto-generate certain files during the compilation process, but not in the layouts folder (AFAIK). You manually create new layout files there, to support your activity code.
 
think it is last update to android studio makes those tutorials a bit outdated.
it adds next novelty to "blank activity" template, so activity_main.xml that goes to setContentView looks like:
Code:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:fitsSystemWindows="true"
tools:context=".MainActivity">

<android.support.design.widget.AppBarLayout android:layout_height="wrap_content"
android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar android:id="@+id/toolbar"
android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main" />

<android.support.design.widget.FloatingActionButton android:id="@+id/fab"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>
and second layout, named content_main.xml included/wrapped in activity_main.xml:
Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main" tools:context=".MainActivity">

<TextView android:text="Hello World!" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
seems google didn't publish some guide yet on how it should be used, but seems like activity_main.xml should work like some kind of "controller/container" for app content, that will go to "content_main.xml" layout...

I suggest to start with "no activity" template and add your activity class and layout by hands for simplicity if you want just follow tutorial.
 
Last edited:
think it is last update to android studio makes those tutorials a bit outdated.
it adds next novelty to "blank activity" template, so activity_main.xml that goes to setContentView looks like:
Code:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:fitsSystemWindows="true"
tools:context=".MainActivity">

<android.support.design.widget.AppBarLayout android:layout_height="wrap_content"
android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar android:id="@+id/toolbar"
android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main" />

<android.support.design.widget.FloatingActionButton android:id="@+id/fab"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>
and second layout, named content_main.xml included/wrapped in activity_main.xml:
Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main" tools:context=".MainActivity">

<TextView android:text="Hello World!" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
seems google didn't publish some guide yet on how it should be used, but seems like activity_main.xml should work like some kind of "controller/container" for app content, that will go to "content_main.xml" layout...

I suggest to start with "no activity" template and add your activity class and layout by hands for simplicity if you want just follow tutorial.
Thanks so much! I wasn't sure what was so weird and found no explanations in Google.
 
Back
Top Bottom