Quantcast
Channel: Envato Tuts+ Code - Mobile Development
Viewing all articles
Browse latest Browse all 1836

Build a Custom Clock Widget: Launching & Receiving Updates

$
0
0

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: Implement the Launcher Activity

You may remember that in the first tutorial, we decided to include a launcher Activity within the widget app. This is not a necessity, but offers a couple of advantages. Users unaware of how to launch a widget app will see instructions on launching it. Also, devices running Android 4.0 sometimes fail to include new widget apps within the menu, but providing a launch Activity seems to circumvent this issue.

When you created your project in Eclipse, if you specified an initial Activity for the app, Eclipse should have created a Java class for it. Using the default steps outlined in Part 1, you should have a Java class in your project package named “CustomClockWidgetActivity” or an alternative name if you changed it. Open this in Eclipse – it should contain the following outline:

//package name

import android.app.Activity;
import android.os.Bundle;

public class CustomClockWidgetActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

If Eclipse did not create the class, create it now and add the code, altering the class name if necessary. We can actually use the default behavior of this main Activity class and leave the code as it is. Notice that the code specifies the main XML layout for its content view. We will alter the main XML layout file next to suit the details of our widget app.

The Activity should already be included in your project Manifest file as follows:

<activity
	android:name=".CustomClockWidgetActivity"
	android:label="@string/app_name" >
	<intent-filter>
	<action android:name="android.intent.action.MAIN" />
	<category android:name="android.intent.category.LAUNCHER" />
	</intent-filter>
</activity>

Here we indicate that this Activity should start when the app is launched. This is the way a launcher Activity is listed within the Manifest for a standard (non-widget) app. Although our widget will generally run by being added to the user’s homescreen, we are providing a launcher Activity for the reasons mentioned above.


Step 2: Alter the Main XML Layout

Open the main XML layout file: “res/layout/main.xml”. Eclipse should have created this file when you created the project. Most of this file can be left with the default code, but we want to add the following attribute to the TextView element for improved readability:

android:padding="10dp"

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.



Viewing all articles
Browse latest Browse all 1836

Trending Articles