Developing widgets for the Android platform involves a slightly different set of tasks than standard app development. In this series of tutorials, we will work through the process of developing a customizable analog clock widget. The clock will be based on the Android AnalogClock class and customized with your own graphics.
Tutorial Teaser
Step 1: Handle Widget Clicks
First, let’s add some code to the widget class to detect user clicks. In the “ClockWidget” class, inside the “if” statement in the “onReceive” method, after the line in which we retrieved the Remote Views object, add the following code to create an Intent for the chooser Activity we are going to use:
Intent choiceIntent = new Intent(context, ClockChoice.class);
Don’t worry about the Eclipse errors for now, they will disappear when we create the new Activity class in the next step. After this line, create a Pending Intent as follows:
PendingIntent clickPendIntent = PendingIntent.getActivity (context, 0, choiceIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Launching Activities on widget clicks is a little different, as you can see. Notice that we pass the Context object and a reference to the new Intent. Now add the following code specifying that the Pending Intent should be launched when the widget is clicked:
views.setOnClickPendingIntent(R.id.custom_clock_widget, clickPendIntent);
We specify the widget by referring to the ID of the parent layout in the “clock_widget_layout” XML file. We need to use the Remote Views to refer to the user interface items as we are in a widget class rather than an Activity class. We will add more code to this class later.
Step 2: Create a Chooser Activity
Now for the Activity in which we let users choose a design. Create a new class in your project by right-clicking or selecting the source package folder and choosing “File” – then select “New,” “Class” and enter “ClockChoice” as the class name. Eclipse will open the new class when you click Finish. Remember that we included this Activity in the project Manifest file in Part 1.
Make your new class an Activity and one that will handle user clicks by extending its opening line as follows:
public class ClockChoice extends Activity implements OnClickListener {
Again, just ignore any error messages, they will appear until we provide the “onClick” method. You will need the following import statements:
import android.app.Activity; import android.view.View.OnClickListener;
Provide the Activity “onCreate” method inside the class as follows:
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.clock_choice); }
We will create the layout file in the next step. You will need another import:
import android.os.Bundle;
We will be adding more code to this class later.
Step 3: Chooser Activity Design
Let’s create the layout we specified in the Activity class above. Create a new layout file by right-clicking or selecting the “res/layout” folder and choosing “File” then clicking “New,” “File” and entering “clock_choice.xml” to match the reference in the above code.
Get the Full Series!
This tutorial series is available to Tuts+ Premium members only. Read a preview of this tutorial on the Tuts+ Premium web site or login to Tuts+ Premium to access the full content.
Joining Tuts+ Premium. . .
For those unfamiliar, the family of Tuts+ sites runs a premium membership service called Tuts+ Premium. For $19 per month, you gain access to exclusive premium tutorials, screencasts, and freebies from Mobiletuts+, Nettuts+, Aetuts+, Audiotuts+, Vectortuts+, and CgTuts+. You’ll learn from some of the best minds in the business. Become a premium member to access this tutorial, as well as hundreds of other advanced tutorials and screencasts.