I am new to android.I want to know what is the main difference between android Manifest file and android XML file.Both are written in the same xml. But,What is the main difference between them can any one tell me with explanation please.Thanks in advance
Read the docs about the manifest
It says
The manifest file presents essential information about your app to the
Android system, information the system must have before it can run any
of the app's code.
The file type of this manifest file should be .xml
And, regarding other XML files, almost all of the other data, apart from the code, everything is specified in an XML format.
according to the docs, the reason why XML is used is as follows
The advantage to declaring your UI in XML is that it enables you to
better separate the presentation of your application from the code
that controls its behavior. Your UI descriptions are external to your
application code, which means that you can modify or adapt it without
having to modify your source code and recompile. For example, you can
create XML layouts for different screen orientations, different device
screen sizes, and different languages. Additionally, declaring the
layout in XML makes it easier to visualize the structure of your UI,
so it's easier to debug problems. As such, this document focuses on
teaching you how to declare your layout in XML. If you're interested
in instantiating View objects at runtime, refer to the ViewGroup and
View class references.
Related
In android, whats the difference between these 2? I started trying to make apps a few days ago and i can seem to wrap my head around it?
From what i have heard from the tutorial i am following, MaiActivity.java uses Java and Activity_main uses xml language?
Also is activity_main used to code the look of the app and MainActivity is used to code what the things do?
And what are ID's for? Is it just to reference certain buttons between the 2 files?
So basically from what i understand if what i have said above is correct, activity_main codes how the buttons look and gives them ID's, and MainActivity code what the buttons do and use the ID's to code the right button.
IS this correct?
From what i have heard from the tutorial i am following, MaiActivity.java uses Java and Activity_main uses xml language?
Also is activity_main used to code the look of the app and MainActivity is used to code what the things do?
Yes. Android uses xml to declare layouts and java to provide logic.
Note that while both activity_main and MainActivity follow common naming conventions, there is no need for them to be called this way.
And what are ID's for? Is it just to reference certain buttons between the 2 files?
IDs are used to identify views in all situations. The most common use case is in the respective java class.
When you create a android project 2 files get generated MainActivity(java) and activity_main(xml) , the xml file is used to create the views which you will be setting in the java file in the setContentView . The android build system created R.java file which contains your xml ids and other xml declaration . the java file can access the views in the xml by referring to R.id,R.string etc . basically its like a address of the xml view which you can refer from java . However I would recommend you to go through the android developer site - http://developer.android.com/guide/index.html
XML, it's an intermediate language between all programming languages and databases, used to pass values from language to another. All tags are user-defined as well as the properties inside such tags. The user can determine the name of the tag, and determine the properties in it, then the name of the tag and its properties with same names will be used in both languages, the first one sets the values to the properties while the other gets them. And so, it works as an intermediate language.
To be specific on how it works, for example, let's assume that we want to pass values from database to java class. There will be three files as follow:
- Java file (.class).
- XML file (.xml).
- Database file (.sql) for example.
In the XML file there is a tag:
<Student>
<name>the name of the student</name>
<age>number</age>
<collage>name</collage>
</Student>
Now each student's data will be in such tag, set from the database file (by an algorithm that writes inside a file when facing a specific text which is the property name), and the java file will get the values (by an algorithm that reads from a file when facing a specific text which is the property name). In this way the values are transformed from language to another.
In Android, the XML file contains all the elements of the activity such as buttons, text views, menus and so on. Each element has an XML tag with its name like Button tag, and each tag has properties. The java file will go to the XML file and look for element tag (Button tag) by the ID of that element (tag), and then the java file (class) takes the values of the properties and sets them to the variables (attributes) of the Button class, and then the Button class draws the Button in the activity. Furthermore, Android studio provides virtual mobile phone screen and displays on it the elements to tell the developer the primary appearance of the activity, in addition, to inform the developer what is the appropriate position, dimensions, or the color of the element, this will generate the XML code to make it easier while coding (it's called visual programming), but in fact the java file did not read the XML file yet, until the Gradle is building the APK (execution phase).
In Android basically we use two languages JAVA and XML.
XML
For layout, how your screen looks? What are the elements(Textview, Buttons, Listview, etc) on screen? What are the attributes of these elements (e.g What is the textcolour, background colour, visibility, font, width, height and much more?)?
The answer of all above question is inside layout subdirectory of res directory i.e a xml file.
Manifest.xml
You will find this xml in app directory of your project.
As in novel or any other book we have content/index page, which gives us the information about all chapters/topic included in that book. In a similar way APK have Manifest.xml which includes all information about Activities, User permissions, receiver, App name, App icon etc.
With the help of xml you can create animation (e.g how textview or any other element will be animated? fade in, fade out, zoom in zoom out etc). Also you can create shapes like circle(oval), rectangle etc and use them as a background or as a icon.
You can string.xml, color.xml etc
JAVA
Used for coding. This page control all the elements of xml with time. You can give default attributes values for different elements in xml, which will be used(for that particular element) in Activity(app) until you change that attribute in corresponding JAVA file for that particular element. To change attributes one must first define an id to the element and use that id in JAVA file to change its attributes.
So I was doing a google search on xml, and most of the links were about xml in web-terms. One of the websites state:
XML does not DO anything. XML was created to structure, store, and transport information.
In android ( I'm a newbie ), there are these .xml files that hold the UI design of an app.
So, what xml exactly is? Is it a language for UI designing, information transport/storage, or both?
XML in Android does not do anything either. But the Android SDK and Android OS would interpret those XML files and, based on their contents, build an UI, define animations etc.
An example from the Android world would be the layout file. It contains information such as what elements will be displayed on the screen, what would be their position, colors etc. But those documents won't "show the UI" on the screen. Instead, Android runtime will read them, interpret their contents and finally use that information to create interface elements.
So you can see that the meaning XML documents have depends on the application. To help with that, often the XSD (XML Schema Defintion) or DTD (Document Type Defintion) is created. Those special documents describe the structure the XML document can have for the given application. It is not necessary to have them, though.
Is it possible for an activity to create xml files and write inside?, or just modify an existing xml files in the res folder?
i am trying to achieve an activity which the user can design a live wallpaper inside it, than save it into an xml file of sort, or modify an existing one (which could be better), and the wall paper service will read that xml file to apply it to the screen.
It a bad practice to modify your code at runtime, moreover I don't think is even possible. a possible solution is to create a default layout, and then modify it using java methods, in case there are not method you should extend the View class of the object (button,imageview...).the following question might be helpful for y
Android Runtime Layout Tutorial
Perhaps this would help
I haven't worked with XML but I doubt it is not possible.
I am a complete newb to Android and have seen tutorials that show widgets being added via XML and also via Java. I want to know what industry experts prefer and why. The author/narrator seems to prefer XML saying it gives more control but I wanted to get feedback from veterans.
With XML Tutorial
Without XML Tutorial
Thank You and again I apologize for a complete newb question.
XML layouts are very easy compared to java code. The coding also very less than java.
You can prefer any of these two, but all authors prefer XML because its easy.
Please read information available at this link, you will get an idea.
XML Layouts
Declare UI elements in XML (most
common and preferred)
Android provides a straightforward XML vocabulary
that corresponds to the View classes and subclasses,
such as those for UI controls called widgets
(TextView, Button, etc.) and layouts.
Instantiate layout elements at
runtime (programmatically in Java code)
Your application can create View and ViewGroup
objects (and manipulate their properties)
programmatically (in Java code).
Advantages of : Declaring UI in XML
• Separation of the presentation from the code
that controls its behavior
You can modify UI without having to modify your
source code and recompile
For example, you can create XML layouts for
different screen orientations, different device screen
sizes, and different languages
• Easier to visualize the structure of your UI
(without writing any code)
Easier to design/debug UI
Visualizer tool (like the one in Eclipse IDE)
Advantages of : Instantiate layout elements at runtime
• While showing dynamic data
When your application need to show dynamically some information for example loading title from web then you need as many text-view per title and you are not sure at design time about this at that time this can be preffered
You can make your application design both ways and both will help you the same,It will depened on your requirement whether you require to create it at runtime or not,Anyways I think while you can make separate XML file why need to do it in your Activity java file and get more complexity in code unnecessarily.
XML layout method is very easy, fast and easy to visualize. You can do a paper-pencil work first and simply do it in XML.
XML layouts are easy to manage - you can change the caption, position, look & feel, size, colors etc. in the XML layout, without altering a single line of the Java Code. The App will generate the Java Code for the layout from your XML file.
XML layout does not compell that you should write the whole XML codes. Instead, you can use a GUI editor and you arrange the controls on screen, and it will generate the XML for you. There are lots of GUI design tools. Eg.: http://www.droiddraw.org
Now, you can go for Java Code UI layout only if you cannot determine the layout at the time of writing the App. Say, you are designing an Android App for Web Designers - An app to create HTML Forms and create the HTML code (a simple HTML editor tool), then you are unable to know which type of layout the user is going to do. At this point, the better option is choosing Dynamic Layout (or Java Code layout)
Almost every Android programmer chooses to do layout in XML. That's the beast and easiest way to do it.
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