Of course, after developing a gadget you will want to deploy it. For example, to run the "hello, world" gadget you need to follow these steps:
Upload the gadget to your web server.
Go to http://www.google.com/ig.
To add a gadget, you must have a personalized homepage. If you don't already have one, you must create it by clicking the "Get started" link. Then sign in with an existing Google Account, or create a new one.
After you have a personalized homepage, you can add gadgets to it by clicking the "Add stuff" link in the upper right corner. This takes you to the content directory. You can use the content directory to search for gadgets and add them to your homepage.
Click the "Add by URL" link (next to the "Search Homepage Content" button).
In the "Add by URL" field, enter the URL (your server or mine) for the hello-world.xml gadget, and then click the Add button.
Click the "Back to homepage" link, to see the new gadget on your homepage.
|Author's Note: I uploaded the examples for this article in my web server so you can try them even if you don't have your own. You can find all the examples under the following path: http://www.alessandrolacava.com/google/gadgets/. For example, the URL related to the "hello, world" gadget will be: http://www.alessandrolacava.com/google/gadgets/hello-world.xml.
You can also publish your gadgets to the Google Content Directory
. That link discusses how to publish your gadget to other targets too, such as your own web site through syndication.
Now that you've seen how to write and deploy a simple gadget you're ready to move on to more interesting stuff.
Some gadgets need to give users a way of supplying user-specific information. For example, an RSS-feed-reader gadget might require users to provide the number of items to retrieve or the URL of a feed the user is interested in. The user preferences section in the XML file describes the user input fields that are turned into user interface controls when the gadget runs. To include a user preference in your gadget you need to include a <UserPref></UserPref>
section into your XML file. Going back to the HelloWorld example, you could let users specify the gadget's title through a UserPref element:
<?xml version="1.0" encoding="UTF-8"?>
<ModulePrefs title="__UP_title__" />
<UserPref name="title" display_name="Gadget Title"
default_value="Hello world example"
<div id="content" style="color: red;">
|Figure 2. User Preference: At runtime, the gadget renders the "title" user preference as a simple textbox.|
shows how the preceding user preference gets renderedas a simple textbox.
The attributes for the UserPref
elements in the preceding example have the following meaning:
- name is the preference's name. It's through its name that you refer to the preference within your code. This is the only required attribute.
- display_name is the label displayed beside the rendered objecttextbox, checkbox, and so on.
- default_value is the value selected by default.
- datatype is the datatype of this user preference. The options are string, bool, enum, hidden, list or location. If you don't specify a datatype value it defaults to string. The preference rendering depends on this attribute. For example, a preference defined with a datatype of bool renders as a checkbox. You'll see more about datatypes later on.
These attributes are the most common, but there are others you might need in your gadget. Refer to the official documentation
to find out more on this and other gadget API topics.
Notice how the previous code points to the user preference. It uses a double underscore, followed by "UP," which signifies "User Preference," another underscore, and the preference name, closing with another double underscore:
The only thing that changes is prefName
which must match the name you provided in the name
attribute when you defined the preference. Such a variable is called a "substitution variable." For example, you can refer to the title