Having  this addictive fascination of customizing gadgets…..?
Here’s howto ….”Pimp” your Android Home Screen!

In this tutorial, we will be customizing the Android Home screen with a Timer widget (which can be easilly skinned).
Download: http://www.filefactory.com/file/b3dgedg/n/WidgetTest.tar.gz


Modify your AndroidManifest.xml to include
1. a reference to a receiver
2 . a service ( if you plan to update via this approach)

File: AndroidManifest.xml

<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="org.me.widgettest">
    <application>

        <receiver android:name=".HelloWidget" android:label="Hello Widget">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>
            <meta-data android:name="android.appwidget.provider" android:resource="@xml/hello_widget_provider"/>
        </receiver>

        <service android:name=".UpdateService" />

    </application>
</manifest>

File: main.xml

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:gravity="center"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/bck" >
        
    <TextView
        android:id="@+id/widgetText"
        android:text="Time:"
        android:gravity="center"
        android:layout_width="100dip"
        android:layout_height="80dip"
        android:textColor="#33FF33"
        android:shadowRadius="1.2"
        android:textAppearance="?android:attr/textAppearanceLarge"/>
</LinearLayout>

File: hello_widget_provider.xml

<?xml version="1.0" encoding="UTF-8"?>
<appwidget-provider
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="100dip"
    android:minHeight="40dip"
    android:updatePeriodMillis="1000"
    android:initialLayout="@layout/main">

</appwidget-provider>

Steps:
1. Create a class extends AppWidgetProvider
2. Override the onUpdate method
3. Create a Service class OR a TimerTask for doing the work (I have included both for comparison)

UpdateService class:
4. For the Service class extend Service
5. Override the onStart method to do the work

UpdateTimerTask:
6. For the UpdateTimerTask extends TimerTask
7. Override the run method to do the work

For both Update classes:
8. Construct a RemoteViews object …for accessing the remote view
RemoteViews updateViews = new RemoteViews(getPackageName(), R.layout.main);

9. Construct a ComponentName :
ComponentName widget =
new ComponentName(getApplicationContext(), HelloWidget.clas
s);

10. Create an AppWidgetManager :
AppWidgetManager manager = AppWidgetManager.getInstance(getApplicationContext());

11. Update by calling the AppWidgetManager ‘s update method
manager.updateAppWidget(widget, updateViews);

Here’s the Code:
File: HelloWidget.java

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.widget.TextView;

/**
*
* @author wagied davids
*/
public class HelloWidget extends AppWidgetProvider {

// Using a Service
@Override
public void onUpdate(Context context, AppWidgetManager manager, int[] appWidgetIds) {
Intent intent = new Intent(context, UpdateService.class);
context.startService(intent);
}
//  <strong>UNCOMMENT THIS IF YOU WANT TO USE a TimerTask</strong>
//    @Override
//    public void onUpdate(Context context, AppWidgetManager manager, int[] appWidgetIds) {
//        Timer timer = new Timer();
//        timer.scheduleAtFixedRate( new UpdateTimerTask( context, manager) , 1, 1000);
//    }
}

File: UpdateService.java

import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.RemoteViews;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
* @author wagied davids
*/
public class UpdateService extends Service {

private static final String tag = "UpdateService";
private static final DateFormat dateFormatter = SimpleDateFormat.getTimeInstance(SimpleDateFormat.MEDIUM,
Locale.getDefault());

@Override
public IBinder onBind(Intent arg0) {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public void onStart(Intent intent, int startId) {
Date time = new Date();
String msg = "Time : " + dateFormatter.format(time);
Log.d(tag, msg);

RemoteViews updateViews = new RemoteViews(getPackageName(), R.layout.main);
updateViews.setTextViewText(R.id.widgetText, msg);

ComponentName widget =
new ComponentName(getApplicationContext(), HelloWidget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(getApplicationContext());
manager.updateAppWidget(widget, updateViews);
}
}

File: UpdateTimerTask.java

import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Context;
import android.widget.RemoteViews;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimerTask;

/**
* @author wagied
*/
public class UpdateTimerTask extends TimerTask {

private RemoteViews remoteViews;
private AppWidgetManager manager;
private ComponentName widget;
private static final DateFormat dateFormatter = SimpleDateFormat.getTimeInstance(SimpleDateFormat.MEDIUM,
Locale.getDefault());

public UpdateTimerTask(Context context, AppWidgetManager manager) {
this.manager = manager;
this.remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
this.widget = new ComponentName(context, HelloWidget.class);
}

@Override
public void run() {
String msg = "Time is now: " + dateFormatter.format(new Date());
remoteViews.setTextViewText(R.id.widgetText, msg);
manager.updateAppWidget(widget, remoteViews);
}
}

For adding the widget to Android Home Screen:
1. Perform a loooonnng tap on the Home Screen …then a pop-up will appear:

2. Select your widget: HelloWidget

3. Viola!