This tutorial assumes you have installed the Android SDK and Eclipse, and have unarchived the DroidDiagram distribution archive.
Let's create an Android application project and add the diagram library to it:
XML
![]() |
---|
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" |
If you run the application now, you will be able to draw diagram nodes on the touch screen if the finger touches an empty area of the view, or draw links between the nodes when the finger initially touches a node. You can scroll the view by dragging it with two fingers.
As of version 1.5.1, this point is obsolete and state is now saved automatically. You could still use similar approach if you prefer rebuilding the diagram from already loaded model data. In that case, set android:saveEnabled="false" for the DiagramView in order to disable built-in state management.
If you change the tablet orientation (and screen rotation is not locked), you will notice that all content of the Diagram is lost. This happens because Android recreates the current Activity when the screen is rotated. Since in most cases a diagram will represent your own data, the DiagramView control does not save and restore the diagram content automatically, but assumes you will rebuild it from the restored data. However, in this example we will show how to save and restore the diagram using built-in serialization methods. First, add a DiagramView field to the Activity, and initialize it from onCreate:
Java
![]() |
---|
DiagramView diagView; |
Override the onSaveInstanceState method to save the current diagram in the state Bundle. Call the saveToString method of DiagramView to save both the diagram content and the view state, such as scroll position and zoom level:
Java
![]() |
---|
@Override |
Override onRestoreInstanceState to load the previously saved diagram:
Java
![]() |
---|
@Override |
If you run the application now, rotating the tablet should no longer erase the diagram content.
In this section we'll show how to handle diagram events, defined in the DiagramListener interface, and how to change properties of diagram elements. Lets add an event listener to the diagram via anonymous class derived from DiagramAdapter, which provides empty implementations for all events from DiagramListener:
Java
![]() |
---|
@Override |
Add an image to the project's assets folder. From the nodeCreated event handler we'll show that image when the user draws a new node, and will also set the node's text label:
Java
![]() |
---|
diagram.addDiagramListener(new DiagramAdapter() @Override |
If you run the application now and draw several nodes and links, you should see something similar to this image:
Here are some additional features of the component you could experiment with:
Browse the next sections of this help file to get conceptual overview of the library's programming interface.