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()
Related
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.
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.
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().
The purpose of the XML layout is confusing for me, having only worked with UI in Visual Studio with the drag-drop xml layout and C# code-behind. Should I be creating a separate xml layout for every screen (load, main, data) in my application? How do views associate with layouts? How do I reference the xml and use it in my code? I only know of the setContentView(xml layout) call.
For example, for my loading screen I want to display a background image and a loading bar. I'm thinking there's a way for the layout to contain the picture and the widget so the user sees the loading screen immediately, and in the actual code I'm initializing all my necessary variables.
If you are confused on how layout works in Android, better try reading this topic on XML Layouts and also read this tutorial about user interface in Android for a head start.
Here's also a tutorial on how to set background image to progress bar.
You should load the layout resource(main.xml) from your application code, in onCreate method of your Activity.
setContentView(R.layout.main);
Yes, you need an XML file for every page of your application.
Write a XML file for that particular page mentioning the fields required and in your .java file use this command
setContentView(R.layout.main);
I am beginner in android development and my problem is that ı want to add some buttons in runtime. I mean, number of button will be change according to flow of program so i need to create different number of buttons at different situations. In code section i can handle it by using array but what about layout file? how can ı set the layout file according to flow of program. I hope ı could explain my problem. Thank you very much.
The xml files in res/layout are static descriptions of layouts. You can create different ones to be used in different contexts (different activities, dialogs, etc). You can actually even replace one layout with another one in the same activity. What you cannot do is to modify the xml files during runtime.
If your UI depends on runtime variables, then you will have to act accordingly. If it's just the number of buttons that will change, you can either
Add new buttons using addView(button);
Add a ListView to your xml file and use an ArrayList and an ArrayAdapter to determine how many buttons you will need.