Whenever I make an activity, Android studio creates two xml files. If I name it main, I get activity_main.xml and content_main.xml. What is the advantage of doing this? And what should I put in activity_main vs content_main?
The content.xml file gets included into the activity's xml file, so for one its reusable in multiple activities or fragments also you can manage your concerns better in smaller units which is also a good thing.
Related
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
Im developing an application with quite a number of activities.
The activities are content wise identical, containing 2 buttons and 2 textfields.
The problem I have is that I want every activity to look like every other in placement, since their content will be different.
Im using the Eclipse IDE
How do I approach this problem?
You should share one layout with different activities. Create a layout (xml file) and then use it for all the activities that you want to look identical. Just change setContentView(R.layout.yourlayout); in onCreate().
I'm creating an android app that will have multiple pages with the same layout, but the only thing that changes will be a string that is displayed on the top (using setText). Can I use a different xml file in the same activity class.., or does Android not allow that?
No problem with that. You can use in the same activity as many XML layouts as you want. Simply switch between them using setContentView()