Days to Xmas widget tutorial


SDK Version: 
M3

My second article based on Norbert Möhring's HelloWidget tutorial, but i think it's easier. :) It will look like this:


xmas widget


The first thing you need is a cool xmas tree for background and an icon.


background
icon

Let’s start from the begining.
Open Eclipse (with android sdk)

File > new Project > other > Android Project

new project
Project name: DaysToXmas
Application name: DaysToXmas
Create Activity: DaysTo Xmas (may be useful)
Min SDK Version: 4

Then click Finish.

Your project structure will look like this: project structure

Now copy/move your christmas tree image to res/drawable/ directory, and the icon to res/drawable-hdpi, to res/drawable-mdpi, and to res/drawable-ldpi.

After that you should refresh your project directories.
Now open the strings.xml (values/string.xml) and modify it like this:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3.     <string name="hello">loading widget...</string>
  4.     <string name="app_name">DaysToXmas</string>
  5. </resources>

Now we have to tell Android that we are going to have a widget.
Open AndroidManifest.xml and go to the source view and copy that:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.       package="com.helloandroid.daystoxmas"
  4.       android:versionCode="1"
  5.       android:versionName="1.0">
  6.     <application android:icon="@drawable/icon" android:label="@string/app_name">
  7.         <!-- Broadcast Receiver that will process AppWidget updates -->
  8. <receiver android:name=".DaysToXmas" android:label="@string/app_name">
  9.    <intent-filter>
  10.       <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
  11.    </intent-filter>
  12.    <meta-data android:name="android.appwidget.provider"
  13.                         android:resource="@xml/hello_widget_provider" />
  14. </receiver>
  15.        
  16.  
  17.     </application>
  18.     <uses-sdk android:minSdkVersion="4" />
  19.  
  20. </manifest>

After that open the main.xml(layout/main.xml) and modify it like this:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="vertical"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent"
  6.     android:background="@drawable/background&quot;>
  7. <TextView  
  8.         android:id="@+id/xmas"
  9.     android:layout_width="fill_parent"
  10.     android:layout_height="wrap_content"
  11.     android:text="@string/hello"
  12.    android:paddingTop="35dip" android:paddingLeft="29dip" android:textColor="#000000"/>
  13.  
  14. </LinearLayout>

Create directory /res/xml, and copy this to hello_widget_provider.xml.

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
  3.    android:minWidth="50dip"
  4.    android:minHeight="50dip"
  5.    android:updatePeriodMillis="10000"
  6.    android:initialLayout="@layout/main"
  7. />

Open the DaysToXmas.java file, located inside DaysToXmas> src > com.helloandroid.daystoxmas. Modify it like this:
  1. package com.helloandroid.daystoxmas;
  2.  
  3. import java.util.Calendar;
  4. import java.util.Date;
  5. import java.util.GregorianCalendar;
  6. import java.util.Timer;
  7. import java.util.TimerTask;
  8.  
  9. import android.appwidget.AppWidgetManager;
  10. import android.appwidget.AppWidgetProvider;
  11. import android.content.ComponentName;
  12. import android.content.Context;
  13. import android.widget.RemoteViews;
  14.  
  15. public class DaysToXmas extends AppWidgetProvider {
  16.         @Override
  17.         public void onUpdate(Context context, AppWidgetManager appWidgetManager,
  18.                                                                    int[] appWidgetIds) {
  19.                Timer timer = new Timer();
  20.                timer.scheduleAtFixedRate(new MyTime(context, appWidgetManager), 1, 60000);
  21.           }
  22.           private class MyTime extends TimerTask {
  23.                  RemoteViews remoteViews;
  24.                  AppWidgetManager appWidgetManager;
  25.                  ComponentName thisWidget;
  26.                  public MyTime(Context context, AppWidgetManager appWidgetManager) {
  27.                  this.appWidgetManager = appWidgetManager;
  28.                  remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
  29.                  thisWidget = new ComponentName(context, DaysToXmas.class);
  30.                  }
  31.             @Override
  32.             public void run() {
  33.                         Date date1 = new Date();
  34.                         Calendar calendar = new GregorianCalendar(2010, 11,25);
  35.                         long days = (((calendar.getTimeInMillis()- date1.getTime())/1000))/86400;
  36.                         remoteViews.setTextViewText(R.id.xmas,""+ days);
  37.                         appWidgetManager.updateAppWidget(thisWidget, remoteViews);
  38.              }
  39.           }
  40.         }

We create a timer that will run our MyTime-Thread every 60 seconds.

  1. timer.scheduleAtFixedRate(new MyTime(context, appWidgetManager), 1, 60000);

To start your application click Run > Run (Ctrl+F11). This will start the Android Virtual Device (AVD) and install your widget on this device.
Now press and hold your left mouse button on the AVD Desktop screen until the menu pops up.
Choose Widget > DaysToXmas



Feel free to download the complete source of the project from this link