I have just begun learning Android App development. I have Android Studio 1.4. In my layout folder I have two XML files (content_main.xml
and activity_mai
According to new design pattern in android studio activity_main.xml will determine how the look of the main 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 the content_main.xml 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 the contents.
What is the role of content_main.xml in android studio 1.4?
So it seems the content_main.xml is a part of a new design pattern introduced in Android Studio 1.4. For the moment, to get along with the tutorials you can find pick the 'empty activity' when creating a new project. It will not contain the content_main.xml.
As mentioned before, the layout file used for your activity is set with setContentView(R.layout.activity_main);
in the onCreate
function of the activity.
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
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 :)
As I know, there must be include statement in your activity_main.xml
file as follows :
<include layout="@layout/content_main" />
that means it is calling the content_main.xml
which has actual elements to be hold.
There will be no problem if you cut and paste all the content of content_main.xml
file and paste it in activity_main.xm
l file in place of include statement(tag).
You can delete your content_main.xml
after doing as above.
In your activity setContentView()
statement should be look like as below :
setContentView(R.layout.activity_main);
use that one which is set in Activity class. i mean set to setContentView(). or please provide your code if you want more description.
I have not seen Android Studio creating two layout files for one activity. Perhaps the content_main.xml
was generated for a previous activity, wasn't it?
Anyway, it doesn't matter what is the name the layout file. Choose one and go for it. Just remember to set the right one in your Activity:
@Override
protected void onCreate(Bundle savedInstanceState){
setContentView(R.layout.your_layout_here);
}