Android - XML
Objective #1: Explain the purpose of programming in XML format.
- Extensible Markup Language, or XML, is a set of rules that specifies how to make documents machine-readable. Among other uses it can be used to design and format information for computer screens especially with regard to being viewed on the Internet.
- The origins are from the World Wide Web Consortium in the 1990’s. XML is a spinoff of SGML.
- The programming of XML has to be very accurate because Eclipse may not always acknowledge an error, yet the emulator will.
Objective #2: Understand the basics of XML programming.
- Designing an Android interface in XML is an easy way to make an interface the way you want it to look. By understanding the face-value of XML programming, the programmer can create a high caliber interface.
- When you start a new project this XML Declaration is usually already typed out but ensure that this code is included:
<?xml version="1.0" encoding="utf-8"?>
- Tags are used to show when an objects code begins and ends. There are three different types of tags,
An example of a start-tag is:
<TextView
Examples of end-tags are
/> and ></TextView>
- Comments can be used in XML programming as well
<!-- This is a comment! -->
Objective #3: Edit the user interface (UI) for a simple project by editing XML files.
- Double-click the activity_main.xml file and go to the Layout tab to be able to drag and drop objects onto the main interface.
- Or, click the main.xml tab to edit XML tags in the source code of the XML file.
- XML is a markup language similar to HTML that is used to create web pages. XML files are not executed. Rather, they merely store data that is used by executable files when an application is opened or executed.
- An XML tag is structured like this
<Tag property settings />
Notice the opening angle bracket < and the closing > angle bracket. Also notice the forward slash that is typed just before the closing angle bracket.
- The settings inside each XML tag are similar to properties of a class. Here's a website that can help you with RGB values for interesting colors.
- Here's a screencast that demonstrates how to change the interface of a view using XML.
Objective #4: Learn the properties of XML objects.
- When you create a TextView, you need to set its length and height. By accessing its length and height properties, you can set them to a numeric value or to a set value (such as: fill_parent or wrap_content).
- You can also change the text color, text, and text size.
- When setting the text color, make sure you use the hexadecimal value for that color. If you want the text color to be red, by typing “RED” will result in an error.
- If you plan on accessing the object in the source code, it is important that you give the object an id which acts as the object's name.
android:id="@+id/new1"
Objective #5: Change colors in XML.
- When you want to change the color of an object in your project you can do it in both xml and in the normal code window.
- To change the color of an xml object type this code:
android:background="#hexadecimal code"
android:textColor="#000"
Objective #6: Access XML objects from the Java source code.
- You can easily access the XML objects through the source code. From the source code, you can still change the properties BUT you need to have an id on the object that you are accessing.
- Use the findViewById method to access the object that you want. You need to have the object types match up, i.e. Buttons with Buttons and TextViews with TextViews. As a parameter inside of the parenthesis of the findViewById method, type (R.id.widget01) to access the object with the id of widget01