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,
Start-tags
<TextView
End-tags
/> or ></TextView> ---End-Tag
- Comments can be used in XML programming as well
<!-- This is a comment! -->
- There is a wide variety of objects that can be created using XML.
- TextView's are similar to labels in Visual Basic.
- EditText's are similar to textboxes in Visual Basic. EditText's return the data type Edittable. You can convert Edittable to a String and from there to an int or double.
- Prior to programming objects, you need to declare a layout. The layout is how the screen will be formatted and how the objects will be arranged.
- LinearLayout will arrange the objects in a linear fashion. When using a LinearLayout, you need to declare if you want it to be a horizontal or vertical orientation; the default is horizontal. LinearLayout is the default layout for Android development.
- RelativeLayout will arrange the objects in a relationship with other objects or the parent layout. Relative layout is a useful layout, yet you need to ensure that the objects aren’t scattered.
- AbsoluteLayout arranges the objects based on their (X, Y) coordinates. This layout is less flexible and is harder to use than other layouts.
- TableLayout arranges the objects into rows and columns. This is a good layout for a game of Tic-Tac-Toe, but for other applications it is useless. Cells can be left empty or filled.
- FrameLayout allows the user to dedicate one screen to one item.
Objective #3: 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 IMPERATIVE that you give the object an Id which acts as the object's name.
android:id="@+id/new1"
Objective #4: Access the objects from the 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