This tutorial shows how to load graph data from XML and create diagram objects corresponding to the graph nodes and edges.
1. Follow the Eclipse or Android Studio guide to create a new Android project and add a DiagramView to its main Activity.
2. Right-click the project's assets folder and choose New -> File from the context menu (If using Android Studio, you might have to create assets folder first by selecting New -> Folder -> Assets Folder from app's context menu). Create a new XML file called SampleGraph.xml and add to it content in the following form:
XML
![]() |
---|
<?xml version="1.0" encoding="utf-8" ?> |
3. Open MainActivity.java and navigate to the onCreate override.
4. Add the following variables to the onCreate handler. "nodeMap" maps diagram nodes to their identifiers, and "bounds" contains the default node size.
Java
![]() |
---|
HashMap<String, DiagramNode> nodeMap = new HashMap<String, DiagramNode>(); |
5. Load SampleGraph.xml and its root element using the XML DOM API:
Java
![]() |
---|
// load the graph xml |
6. Load the graph node elements and create their corresponding ShapeNode objects by calling the createShapeNode method of the Factory class. createShapeNode is just a shortcut to creating a ShapeNode instance using the "new" operator and adding it to the Nodes collection of the diagram. Additionally, this code maps the new node to the "id" attribute of the XML element, and sets the node's Text to the value of the "name" attribute.
Java
![]() |
---|
// load node data |
7. Load the graph links and create corresponding DiagramLink objects. The Origin and Destination of the links are accessed by their ids through the "nodeMap" dictionary.
Java
![]() |
---|
// load link data |
8. Arrange the diagram using the LayeredLayout class.
Java
![]() |
---|
// arrange the graph |
9. Build and run the project. If everything is fine, you should see this representation of the graph: