Widget TutorialHere is a tutorial for using the DroidDraw GUI builder to build a android widget for android phones. It builds off of the currency converter tutorial. 1) Create a layout in DroidDraw:
Yes, I realize this is wonky, part of the motivation for this tutorial is to improve widget 2) Create
3) In eclipse create a
4) Create a
Inside the manifest block add: 6) Add the following class (complete code here): Implement AppWidgetProvider:
public class CurrencyWidget extends AppWidgetProvider {
@Override
public void onUpdate(
Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// To prevent any ANR timeouts, we perform the update in a service
context.startService(new Intent(context, UpdateService.class));
}
...
// Implement the Service for updates.
public static class UpdateService extends Service {
@Override
public void onStart(Intent intent, int startId) {
// Build the widget update for this rate
RemoteViews updateViews = buildUpdate(this, getRate());
// Push update for this widget to the home screen
ComponentName thisWidget = new ComponentName(this, CurrencyWidget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
manager.updateAppWidget(thisWidget, updateViews);
}
public RemoteViews buildUpdate(Context context, double rate) {
// Build an update that holds the updated widget contents
RemoteViews updateViews =
new RemoteViews(context.getPackageName(), R.layout.widget2);
// Build the text for the widget
DecimalFormat format = new DecimalFormat("##.##");
String euroToDollar = format.format(rate);
String dollarToEuro = format.format(1 / rate);
updateViews.setTextViewText(R.id.etod, "Euros to Dollars: " + euroToDollar);
updateViews.setTextViewText(R.id.dtoe, "Dollars to Euros: " + dollarToEuro);
// Set up the button click handler
Final ProductCompile, install and activate the widget on your homescreen, you should see something like:
|