How to use Android XML Layout? - android

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

Related

How to dump the xml of the android layout of android activity on running app

Problem Description:
I am debuggin an android app that is written by another developer. The layout of the screen is constructed via xml and several stuff are added dynamically. The code is a prototype code with no comments or naming conventions to understand what's happening, or what variable does what.
Question
Is there a way to, when running the app, dump the xml layout as it is shown on the screen? That is retreive the xml layout that is dynamically constructed, in order to check variable and view's names?
Thanks
I found it inside the DDMS, there is a button called "DUMP view hierarchy for UI Automator" the button is next to the screenshot. And it will dump the xml to be used for automation testing.

How to set up a layout using code and not XML

I'm learning to develop android apps and in the process I realized that there two ways to get a job done. Using xml or normal code. Suppose I want to change the position of a button, I'll be doing it in xml using align left/align centre etc., This will be done in the XML file. If I want to achieve the same through code, where should I place the code ? Inside which class ?
There are two aspects to your question that I understand.
1. Creating a whole layout file dynamically (without XML).
2. Creating a layout through XML and changing the components positions and properties dynamically through your activity file.
Now, it's upto the developer what he wishes to choose.
To help you further, please view this video link posted by the Android team.
It's all about layouts and includes how to layout apps using Java, not XML. However, you are warned that the android team wants you to use XML.
The code will be placed in the same class as the class where you reference your xml code. Do a read up in your android docs for insight.

Can i have same activity using different xml file

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

Convert layout to class

I currently have a layout that has scrollviewer as main layout, and a relative one (with a lot of views) as my app layout, but now I need to make tabs, the android tutorial instructs to make the actual layout of each tab programatically, how do I convert my current layout (xml) to a class? Is there any guideline to follow? how do I set the relative layout inside the scrollviewer and how do I specify the "android:layout_below="SOMETHING"" and all that properties?
I din't understand your question very well but, when creating the TabLayout programaticaly you define the activities that are used for each of the tabs. Each of them should have setContentView( 'activity layout')
In order to access XML elements in Android you need to do something like the following:
Button submitButton = (Button) myActivity.getViewById(R.id.btnSubmit);
Then you can call things like submitButton.setVisibility(View.HIDDEN) to hide it, or whatever else you need to do with it.
This should work for any layout elements.
Make sure you do this in your onActivityStart method, or else you will throw runtime exceptions.
There isn't any way to automatically "convert" an XML layout file to a class, unless you're using an XML parsing algorithm of some sort, which sounds like way more than you're trying to do.
Hope this helped!

XML parser for dynamic layout (dynamically loaded skins)

I want to write an app where (at least for now) the content is always the same but the layout is loaded dynamically at run time based on a user preference. Essentially I want the app to apply a "skin" which may look completely different to other skins.
I found some tutorials using SAXparser:
http://www.androidpeople.com/android-xml-parsing-tutorial-using-saxparser/
http://twigstechtips.blogspot.com/2010/12/android-how-to-parse-xml-string.html
and can imagine writing something from scratch that recognizes all the standard xml layout tags and then dynamically loads each part of the layout. But that's a lot of work to do from scratch! Surely this functionality is available in android, or surely someone has written some open source code which can be run at the start of your activity's onCreate method, which takes in an xml file and sets your layout?
I found a similar but unsatisfactorily answered question here:
How to create a layout file programmatically
which makes me think that since setContentView must take an integer resourceID as its argument, the fact that these are pre-baked at compile time might be a problem. (setContentView may also take a View object as its argument, but I don't want a ton of if statements and to pass it each View object one by one, I want some code that inputs an xml file or xml string and sets the content view.)
Maybe I'm way off track. Is there another way to do this? I would think that the ability to have an app with dynamically loaded skins is important.
Thanks!
I had similar requirements and tried the same approach - it does not work.
Documentation clearly states this: http://developer.android.com/reference/android/view/LayoutInflater.html
Update:
Since OP needs to load XML layouts created at runtime:
Possibly this could be done, by creating XML layout files, copying them to dummy project, create .apk and then load apk on to device.
DexClassLoader can be then used to load classes inside apk.
well, android makes the hard work for you, but no all the the work....
first that all you have to forget about parsing xml layouts... instead you can make skeletons layout, that manages his inner childs position, size, etc... and later inflate that 'skeleton' xml with LayoutInflater and obtain a View instance...
When you have that View instance then you can do what you want with it, applying the users preferences like backgrouds, foregrounds colors, position, sizes, etc...
maybe i dont understand your question but you can get any view inflated from a xml resource at compile-time and later apply other style or set another propertys
It seems it is impossible to load the layout & change the skin dynamically according to the doc :
Therefore, it is not currently possible to use LayoutInflater with an XmlPullParser over a plain XML file at runtime; it only works with an XmlPullParser returned from a compiled resource (R.something file.)
http://developer.android.com/reference/android/view/LayoutInflater.html

Categories

Resources