'appwidget'에 해당되는 글 1건

  1. 2010.02.02 How to make a simple Android widget (5)
2010.02.02 09:21

How to make a simple Android widget

Yesterday I made my first Android widget. A very simple widget, just to try it out. It can show the time. No more, no less. I will here show you how to get started on widgets. I’ll start from scratch, so you actually don’t need any knowledge on Android development. Well, of course you need to have the SDK, and Eclipse installed and ready.

I used the 1.5 SDK, but it will probably work on newer SDK’s too.

I will show you how to do it in 6 easy steps.


1. Create a new Android project
Fire up Eclipse, and click File -> New -> Android project
Skærmbillede 2009-11-07 kl. 14.26.27
Type in a name for you project, eg. WatchWidget. Select the target, 1.5 in my case. I guess that there could be differences, so if you are not experienced in Android development, I suggest that you use 1.5 too.

Then we need an application name, which is the name that will show up on your phone. And last but not least, a package name.

Uncheck “Create activity”, as we don’t want any activities in this sample.

Click Finish to create the project. Now you have a structure that looks like this:

WatchWidget structure


2. Create the Java class
Right-click com.kasperholtze.watchwidget (or whatever you used for package name), and select New -> Class:

Create WatchWidget class

Give the class a name, i.e. WatchWidget. Then we need to extend the AppWidgetProvider, so type in android.appwidget.AppWidgetProvider as superclass, or browse for it.

Click Finish, and Eclipse will generate the following code:

package com.kasperholtze.watchwidget;

import android.appwidget.AppWidgetProvider;

public class WatchWidget extends AppWidgetProvider {



3. Create the Java code
Now it’s time to create our Java code, for updating the widget. Type in the following code:

package com.kasperholtze.watchwidget;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import com.kasperholtze.watchwidget.R;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.widget.RemoteViews;

public class WatchWidget extends AppWidgetProvider
    public void onUpdate( Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds )
        RemoteViews remoteViews;
        ComponentName watchWidget;
        DateFormat format = SimpleDateFormat.getTimeInstance( SimpleDateFormat.MEDIUM, Locale.getDefault() );

        remoteViews = new RemoteViews( context.getPackageName(), R.layout.main );
        watchWidget = new ComponentName( context, WatchWidget.class );
        remoteViews.setTextViewText( R.id.widget_textview, "Time = " + format.format( new Date()));
        appWidgetManager.updateAppWidget( watchWidget, remoteViews );

The only method we use in this class, is onUpdate( Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds ), which we override from the AppWidgetProvider class.

First we create three objects. remoteViews, watchWidget and format. remoteViews is a reference to the main view, which we will create in a moment. watchWidget is a reference to our class, and format is the date format used to show the time.

Then we initialize the remoteViews and watchWidget. You will get an error one the line remoteViews.setTextViewText(), which is where we actually update the text on our widget, stating that R.id is not found, but that’s all right for now. We will create that one in a moment too.

appWidgetManager.updateAppWidget sets the RemoteViews that should be used for our widget.


4. Edit the view for our widget, main.xml
Using the UI designer takes some trying, testing and failing. If you have not tried this yet, you should try playing around with it. It takes some time to get the feeling of it.

My main.xml looks like this:

<?xml version="1.0" encoding="utf-8"?>
        android:text="17:12:34 PM"

It contains a LinearLayout with a TextView inside. The important thing is the id of out TextView, which we used earlier in the WatchWidget class.
I used a standard widget background, which you can download at http://developer.android.com/guide/practices/ui_guidelines/widget_design.html. You can also grab it here:

WatchWidget background

Save it in the project, at res/drawable/background.png


5. Edit AndroidManifest.xml
The next thing is to edit the AndroidManifest.xml, which defines the application. We need to add an intent-filter, meta-data that defines which xml file we use for defining the widget provider, and name and other details of the application.

Here it goes…

<?xml version="1.0" encoding="utf-8"?>

    <application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true">

        <receiver android:name=".WatchWidget" android:label="@string/app_name">
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
                android:resource="@xml/watch_widget_provider" />

    <uses-sdk android:minSdkVersion="3" />



6. Create widget provider
The last thing we have to do, before the application is ready to run, is to create a XML file, defining the widget provider. Create a new folder under res, called xml, and save the file as res/xml/watch_widget_provider.xml.

<?xml version="1.0" encoding="utf-8" ?>

Here we set the minimum size that the widget needs to be added to a home screen. And then we set the updatePeriodMillis. updatePeriodMillis defines how often we wish to call the onUpdate method that we created earlier in the Java class.

That’s it! Connect your phone, or fire up a virtual device, and try it out

Trackback 2 Comment 5
  1. website logo design 2010.07.15 16:17 address edit & del reply

    The layout of your website is impressive and one of the greatest example of creativity.

  2. Lawyer Marketing 2011.08.18 07:57 address edit & del reply

    이봐, 하나는 새로운 twitting 법안이 무엇입니까? 나는 twitting가 가장 인기있는 문화 인터넷 사이트가 될 수 있기 때문에 일반적으로이 사람을 준수하려는거야. 지금 당신이 일반적으로 존재하지 경우에 당신이 아주 큰 의미에서 가족 고객과 통신할 수 있습니다 좋겠 이후 당신은 가능하게 그것을 통해 생각을 할것입니다.

  3. Canada Goose Jackets 2011.11.11 23:19 address edit & del reply

    Canada Goose Jackets sale is the feature product of Canada Goose,more and more consumers like this fashion Canada Jackets.

  4. Canada Goose Jackets 2011.11.11 23:19 address edit & del reply

    Canada Goose Jackets sale is the feature product of Canada Goose,more and more consumers like this fashion Canada Jackets.

  5. jumbo ugg 2011.12.15 16:12 address edit & del reply

    Nice classic short boots with the able adroitness and abundant affection are account of your purchasing. These ugg boots sale are able-bodied fabricated with the artistic design. With the heel bouncer apparent with http://www.ugg-snow-boot.org/, these ugg boots do attending actual timeless. The shoelaces are advanced and the eyelets are big. Thus, these Kids Ugg Boots attending altered from the accustomed winter boots in the appearance market. Featuring the lining abounding with sheepskin, the ugg boots can wick damp abroad for your feet. You can get pleasure the absolute warmth, abundance and benevolence of these absolute thigh-high style boots. It adds abundant absorption to the Appearance Sheepskin ugg boots. http://www.uggbootssale-au.net/