I am new to 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 have been following Bucky's tutorial and he just has the activity_main.xml. Which one should I use?
Unlike previous version, the new API 23 comes with the coordinatorLayout and stuff and just to make them simple to use android has distinguished activity's layout into two different layout i.e. content_main.xml & activity_main.xml.
I'll explain what they are for:
activity_main.xml
this xml file is used to display coordinatorLayout, tabLayout, floatingActionButton, viewPager etc..
content_main.xml
This xml file is use to display your stuff i.e. what you want to display to users.
So, if you are just asking in which xml you have to put your stuff, content_main.xml is the one....
I think the content_main.xml is a part of activity_main.xml.
Because there is include layout="#layout/content_main" in the activity_main.xml.
Android Studio 1.4 gives a default new option of content_main.xml in the activity. actually activity load the xml file of activity_main.xml for your layout. content_main.xml is the part of main.xml.
you can use both for layout,but main.xml is necessary for your Activity. its a option to choose the content_main.xml for design of layout.
Hope you get it.
The activity_main.xml is the "outer" part of the activity layout (toolbar, action button, etc.) and content_main.xml is the inner part where you put your own layout (the blank space).
content_main.xml file will be generated when you choose "blank activity". This activity will be having floating button by default at the bottom of the screen.
But when you choose "empty activity" then content_main.xml won't be generated and it won't be having floating button, but just simple Activity.
activity_main and content_main are linked to each other.
You can use either both. But to be specific, you should use activity_main.
let me explain this further.
activity_main, this is where the content_main is placed. This is a mother and child case, where activity_main is the mother and content_main is the child.
content_main, this is where you put your controls / content e.g button. This represents as the body of your design.
These are just 2 xml layout files which basically represent 2 different layouts. You should use the file based on the layout you want to use in your android app.
Based on the tutorial it is simple that you should use activity_main.xml as it is the layout for the MainActivity.
You can also see the preview of the layout file in android studio as to how it will look in the app.
Related
How can I show a layout.xml design on an activity? Say If I have a layout.xml file on external storage on android. If I want to show the full design of layout.xml file on some specific view group (say linear or relative layout) inside my activity of android app at runtime. If anyone can provide example to show any kind of layout.xml file's design on an activity.
While creating any activity(other than empty) in android studio it is creating only activity xml, using Android Studio 3.1.4, Kindly help.
Empty activity default contains only a single layout file
Read this
Activity template
This template creates an empty activity and a single layout file with sample text content. It allows you to start from scratch when building your app module or activity.
But you can create the content layout separately then include into the main activity.
As my understanding, the activity_main xml file created by wizard is the content xml file when create Basic Activity.
Then you could modify the activity_main xml to fulfil your requirment.
According to the documentation of Android Studio, activity_main.xml will affect how global UI of Activity is designed and works. But content_main.xml will affect the contents in the activity_main.xml.
Since a Basic Activity,Empty Activity doesn't have much content, thus the content_main.xml is not created for those. If you create a Navigation Drawer Activity, Studio will create both app_bar_main.xml , content_main.xml and activity_main.xml
I am learning android framework. I see these three get added when I add a new Blah.java basic activity. I would like to know what is the purpose of each one of it?
activity.xml
This xml file is design view of your activity. Its for designing purpose and also thats front end of screen view.you can design xml by using android layout and widgets.
fragment.xml
This xml file is design view of your fragments.
content.xml
Thats also part of activity.xml design file .we can access/use it from others xml file by using include
<include layout="#layout/content"/>
This are the layout xml for your android app, activity refers to your class, if you are using any fragment in your app then you should have one fragment.xml.
content is something which is optional, you can create anything inside content and then you can include that in both fragment and activity xml.
I am presuming you are using an IDE (Android Studio), which has this great advantage of generating layout files while creating Activities in android.
Now layout files (.xml) files are used for generating views, while creating an activity you are given a option (in IDE) whether you want to create a fragment for the activity.
For each of them the activities and fragments there will be a layout file and based on your activity/fragment name the layout files name will be generated (which can be edited)
In your case:
activity_blah.xml and content_blah.xml are the layout files generated for the blah.java activity file, you can add view components in the layout files for user interface of the activity
fragment_blah.xml is generated for fragment.
When creating new project, sometimes Android Studio creates activity_main.xml and content_main.xml, but sometimes it creates just activity_main.xml and not content_main.xml. I can't find the logic, under which condition it chooses which files to create. Can anyone explain it, please?
When you create a new activities, you can choose different options, two of them are Blank Activity (if I recall properly) and Empty Activity. The difference between them is that one is based in the modern Android approach, based on Fragments.
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.
Answer based on https://stackoverflow.com/a/33415349/6166978
I am new to Android Studio. Following the tutorial given by developer.android I have created a new project. They asked to create a blank activity and as there is no one such, I have used Empty Activity as the template. Then the tutorial said that there will be a content_main.xml inside res/layout folder, but I didn't find one. Did I build the correct Application? What probably may be the wrong? Thanks in advance.
content_main.xml file will be generated when you choose "blank activity" . This activity will be having floating button by default at the bottom of the screen.
But when you choose "empty activty" then content_main.xml won't be generated and it won't be having floating button , but just simple activity.
So for your tutorial choose "blank activity" and then your tutorial will run fine
You need to select basic activity when creating a project. that will give you the 2 files content_main.xml and activity_main.xml
content_main.xml is used to handle the contents of the activity(like buttons, textView etc). So when we choose an empty activity, there are no components and hence the layout content file i.e. content_main.xml is not created. If you choose a blank activity there will be a floatingactionbutton component and hence there will be a content_main.xml created. So hopefully this will solve your problem. You can delete the floatingactionbutton later if you want your activity to be blank.
Try to close and reopen the Android Studio. I hope this works!
On the top right corner of the android studio code there is a search icon.
Click that and type: content_main.xml
You'll get the file over there open it.