5

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_main.xml). I use online tutorials to learn but they have only activity_main.xml in them. So what i want to know is what are the functions that should be used in these respective files. can i just use activity_main.xml and just let the other be ? and vice versa.

Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79
  • 2
    Have a look at the following answer: [http://stackoverflow.com/a/32880945/5352802](http://stackoverflow.com/a/32880945/5352802) – camelCaseCoder Oct 29 '15 at 12:44

5 Answers5

11

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 :)

Kelevandos
  • 7,024
  • 2
  • 29
  • 46
  • So, should we use this method for each activity that we create? i.e. Create two XML files for each activity? Or is this one preferred only for MainActivity? – Mukesh Ghatiya Oct 27 '19 at 08:16
6

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.xml 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);
Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79
Jaimin Modi
  • 1,530
  • 4
  • 20
  • 72
3

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);
    }
}
Community
  • 1
  • 1
dabo248
  • 3,367
  • 4
  • 27
  • 37
1

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);
}
Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79
aldux
  • 2,774
  • 2
  • 25
  • 36
0

use that one which is set in Activity class. i mean set to setContentView(). or please provide your code if you want more description.

ibad ur rahman
  • 1,381
  • 4
  • 18
  • 40