I know Java programming well. I am working on an Android project and have learned about how to create an application for android. However, I have a point that I do not understand about interfaces of android. When we are working on Java we have used in code screen panels and some imports like Swing, AWT, SWT, but now we are creating interfaces on XML files and I do not understand how do XML files create an interface. I want to know its logic. Could you please enlighten me about XML files.
Share
Think of XML sort of like HTML – you are basically using markup to define UI components and giving them basic style attributes like position, size, colors, etc, but not really any behavior properties. XML tags, like HTML, have names, and attributes that define properties. They can be nested as well, in many cases. When a tag has other tags nested in it, we call those children. Tags with children have the opening tag () and a closing tag (). Children are defined in between. Tags without children may omit the closing tag by placing a / (slash) in front of the enclosing >
The attributes have the android: label on them so that the xml parser can properly recognize they are properties defined by android, to put it simply. The build tools included in the eclipse ADT (or in the sdk tools if you are not using eclipse) generate java objects behind the scenes with the corresponding properties appropriately set, so that you don’t have to do that work. It also generates an R.java file, which contains pointers to the java objects it generates for your layout elements, so that you can easily reference them in java code:
If you want, you CAN do your layout/UI development entirely with java code, but it becomes much more verbose and tedious, as you will have to explicitly set many values which are implied/omitted in the corresponding XML code. XML saves a lot of time and the visual editors make it a lot easier/faster to get the look how you want it.
With XML, you can also define things like Strings which you want to easily internationalize, and simple drawables which, if done correctly, will easily scale up n down screen sizes and resolution, and simple animations. Using the “qualified path names” on your resource directories, you can provide alternative versions of any value/resource defined in XML (such as layouts, drawables, or strings) which will be appopriately swapped out at run-time based on the users device & settings.
For instance, you can define a layout, say main.xml, in res/layout-land for landscape, and an alternate version in res/layout-port for for portrait. Then the app will load the layout which corresponds to the users orientation automatically. Similarly, you can define a string, say button_label, in res/values-en/strings.xml which has a value of “Yes”, and define a string with the same name, button_label, in res/values-es/strings.xml with a value of “Si”. If the users locale is set to english, ‘Yes’ will be used. If their local is Spanish, “Si” will be used.
http://developer.android.com/training/basics/firstapp/building-ui.html
http://developer.android.com/guide/topics/ui/declaring-layout.html
http://developer.android.com/guide/topics/resources/overview.html
http://developer.android.com/guide/topics/graphics/view-animation.html