Sunday, August 29, 2010

A simple Home Screen App Widget to get Date/Time.

A simple Home Screen App Widget to get Date/Time.

In former exercise, "A dummy Android Home Screen App Widget" without any function have been implemented. Here, we add some code in the onUpdate() method to get and show the date/time on home screen in 30 minutes interval.

A simple Home Screen App Widget to get Date/Time.

Bascically the structure is same as the exercise of "Create a dummy Android Home Screen App Widget".

Modify android:updatePeriodMillis to "1800000" in /res/xml/hellowidgetproviderinfo.xml. Because "updatePeriodMillis will not be delivered more than once every 30 minutes".

Modify /res/layout/hellowidget_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/widgettext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
/>
</LinearLayout>


Modify HelloWidgetProvider.java to override methods onDeleted(), onDisabled(), onEnabled(), and mainly in onUpdate().
package com.exercise.HelloWidget;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.widget.RemoteViews;
import android.widget.Toast;

public class HelloWidgetProvider extends AppWidgetProvider {

private SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy hh:mm:ss a");
String strWidgetText = "";

@Override
public void onDeleted(Context context, int[] appWidgetIds) {
// TODO Auto-generated method stub
//super.onDeleted(context, appWidgetIds);
Toast.makeText(context, "onDeleted()", Toast.LENGTH_LONG).show();
}

@Override
public void onDisabled(Context context) {
// TODO Auto-generated method stub
//super.onDisabled(context);
Toast.makeText(context, "onDisabled()", Toast.LENGTH_LONG).show();
}

@Override
public void onEnabled(Context context) {
// TODO Auto-generated method stub
//super.onEnabled(context);
Toast.makeText(context, "onEnabled()", Toast.LENGTH_LONG).show();
}

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// TODO Auto-generated method stub

String currentTime = formatter.format(new Date());
strWidgetText = strWidgetText + "\n" + currentTime;

RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.hellowidget_layout);
updateViews.setTextViewText(R.id.widgettext, strWidgetText);
appWidgetManager.updateAppWidget(appWidgetIds, updateViews);

super.onUpdate(context, appWidgetManager, appWidgetIds);
Toast.makeText(context, "onUpdate()", Toast.LENGTH_LONG).show();

}
}
CORRECTION@2010-10-03: The following statement should be moved to the first inside onUpdate() method.
super.onUpdate(context, appWidgetManager, appWidgetIds);

Download the files.

next: A simple Home Screen App Widget with configure activity

Related Article:
- Home Screen Battery Widget


5 comments:

travelbuff said...

hi i tried the code and its awesome.. i would like to try putting some rss viewer on it.. is that possible? how can i insert rss on this project? many thanks!

Erik said...

hello travelbuff, I think you want to display rss as a ListView. As my nderstanding, ListView cannot be embedded in App Widget.

May be you can display rss as TextView, and change the content periodicaly.

Raadius said...
This comment has been removed by the author.
Raadius said...

Awesome! I deleted my comment because I got it to show up, my question is, how can I turn this into a calendar widget, with a mock calender icon that I created? I also want to take out the time text as well and show just the date and possibly bold it and change the font.

Hugh said...

@Android Er

ListViews can be embedded in app widgets.

See here:
http://developer.android.com/guide/topics/appwidgets/index.html#CreatingLayout