1

I got the new android studio, and I noticed one thing when creating a new blank activity.

enter image description here

The layout that corresponds with the java for the activity is made twice, once as a content:

enter image description here

This new layout is an exact replica of the regular activity_menu.xml except this one is called content_menu.xml. I also noticed this piece of code in the layout code of activity_menu.xml:

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

What is the point of this extra layout? Can I delete it? I found this on the official documentation, but I still don't understand why it is necessary. This wasn't there in the first update...

user229044
  • 232,980
  • 40
  • 330
  • 338
Ruchir Baronia
  • 7,406
  • 5
  • 48
  • 83

2 Answers2

1

Here content_menu is just a name for your xml layout content. Don't get confused by this. As you see in your Menu Activity you'll see setContentView(R.layout.activity_menu)

Here the activity is told that its layout is the content inside activity_menu.xml but in this(activity_menu.xml) file you see an include tag. the include tag means that the content inside another xml file named content_menu.xml must be included inside the current layout at the place where include tag is used. so if want to change the internal content you can leave everything in activity_menu.xml as it is and make your changes in content_menu.xml and it'll all be visible in your design.

and if you don't want the content_menu.xml file you can just copy everything from this file to activity_menu.xml in place of <include > tag and delete the content_menu.xml file.

Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96
Akash Raghav
  • 1,073
  • 9
  • 19
  • Ohhh, that makes more sense. What is the point of this anyway? I will mark you best answer once I get it! – Ruchir Baronia Dec 04 '15 at 06:35
  • there are many reasons 1. to seperate the common design part such as 'Toolbar' or 'FAB(Floating action button)' etc from the your main content. 2. to make the code reusable if multiple activity has same content or similar layout, this reason is the most important one. 3. To increase the robustness and readability of code. – Akash Raghav Dec 04 '15 at 07:21
  • Alright, and in number two you mention reusability. How can we reuse the layout, and why not just use a fragment? Also, is it okay to remove the `content`? – Ruchir Baronia Dec 04 '15 at 18:43
  • No, a 'fragment' is a class with its own full fledged life-cycle which uses system resources same as an 'activity' and does the job of dynamic inclusion of ui component, so it has its own purpose, whereas here 'content.xml' is just a static xml file which could be added anywhere statically and won't necessarily be modified at the runtime. And yes you can remove the content file if you want to just place your ui code into the 'activity_yourfilename.xml'. – Akash Raghav Dec 05 '15 at 12:45
0

Say for example you want to reuse content_menu.xml content somewhere else, in that case in other activity you will do the same include statement in xml file. The point is that you are reusing the same xml and reducing number of content.

Iliiaz Akhmedov
  • 867
  • 7
  • 17
  • 1
    Why not just inflate menu again? – Ruchir Baronia Dec 04 '15 at 01:07
  • @RuchirBaronia - You wouldn't inflate `content_menu`. Say you include `content_menu` within `activity_menu.xml` and `fragment_menu.xml`. You would inflate the later instead. – OneCricketeer Dec 04 '15 at 01:20
  • ...I am really lost right now. What is the point of the `content` and why do we need to reuse it? Is the reason for performance or something? I have no idea what `content` even is, let alone how to inflate it...Thanks – Ruchir Baronia Dec 04 '15 at 01:23
  • @RuchirBaronia - basically you *can* delete `content` as long as you replace the `include` line in the XML of `activity` with the full contents of `content` – OneCricketeer Dec 04 '15 at 01:25
  • @RuchirBaronia - The purpose of having `content` at all and `include`-ing it, is so it can be reused elsewhere in other menus. Say you had a "Quit App" menu item in `content`, you could then `include` that menu item in all your other menus without retyping it in all the other ones. – OneCricketeer Dec 04 '15 at 01:27
  • Isn't that just a fragment...? – Ruchir Baronia Dec 04 '15 at 01:29