why is there content_main.xml and activity_main.xml by default instead of just activity_main.xml?
The modern Android approach is based on Fragments
, which are, in a way, "small Activities", which you can put in Activities
, gaining lots of flexibility.
Therefore, activity_main.xml
is simply the Activity layout containing a container (FrameLayout
most probably) and content_main.xml
is the layout for a Fragment put into this container somewhere within MainActivity.java
. You should study the code in there to understand it better :)
What is the role of content_main.xml in android studio 1.4?
According to new design pattern in android studio activity_main.xml
will determine how the global UI of the Activity should be. And on the other hand content_main.xml
will determine the contents in the activity_main.xml
.
That is content_main.xml
will contain the textview, edittext, button etc component. And it will be included by the activity_main.xml
.
So we can think of content_main.xml
just like partial in HTML. activity_main.xml
will contain your activity global design, and content_main.xml
will contain the contents.
From view of what they contain:
activity_main
: Co-ordinator layout, ViewPager etc
content_main
: Developer choosen elements. textview, edittext, button etc.
What the android developer website says about them is
activity_main:
This XML layout file is for the activity you added when you created
the project with Android Studio. Following the New Project workflow,
Android Studio presents this file with both a text view and a preview
of the screen UI. The file contains some default interface elements
from the material design library, including the app bar and a floating
action button. It also includes a separate layout file with the main
contentcontent_main:
This XML layout file resides in
activity_my.xml
(activity_main) ,
and contains some settings and Textview(other) element.
What has to be used, content_main or Activity_main?
While setting the parameters for setContentView, which XML has to be
typed ? Content_main or Activity_main ?
You should use content_main.xml
to put your views but pass activity_main.xml
as the parameter to you setContentView()
method.
activity_main.xml
is used to display coordinatorLayout, tabLayout, floatingActionButton, viewPager etc.
content_main.xml
is used to display your stuff i.e. what you want to display to users.
reference
And if I want to add an Layout file (XML) do I have to create 2 files
with the same name ?
Totally depends on the nature of the activity you are creating the layout for. The difference above will help you decide whether you need two or one.
Related Topics
Failed to Connect to Camera Service
Font Size Too Large to Fit in Cache
Error String Types Not Allowed at Android:Configchanges in Manifest File
Android; Geocoder, Why Do I Get "The Service Is Not Available"
Java.Lang.Numberformatexception: Invalid Int: "" in Android
Gridview VS Gridlayout in Android Apps
Android - How to Check Proguard Obfuscation Has Worked
How to Change the Text of a Checkbox in Listview
How to Create Always-Top Fullscreen Overlay Activity in Android
How to Add Shadow to the Fab Provided with the Android Support Design Library
Yet Another Getview Called Multiple Times
Android: How to Tell If the Soft Keyboard Is Showing or Not
Android Internet Connectivity Check Problem
How to Show Dialog in Oncreate Method
Trying to Add Adb to Path Variable Osx