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">
  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

Comments

Thanks for this tutorial

This was really helpful. Thank you for posting this! I am wondering why they made the program so hard to use if it requires that much work?

Great Step By Step Tutorial

I must say this is an excellent, step by step tutorial on making a decent app on the Android platform. Nice work. I'm definitely bookmarking this for future reference when I'm ready to make a good app myself. And it will be related to this app in the sense that it would be about helping people come up with a good Christmas songs list on the go, and have links to the song mp3s that can be downloaded...still in the infancy stage but I think it'll be a good one.

Thanks very informative and

Thanks very informative and simple tutorial, keep walking by heartbreaker

Nice widget

Thank you for this article, very usefull.
Thomas, NY
Data recovery engineer

In a couple of books and

In a couple of books and examples from developer.android.com I red about using a Service or a AlarmManager with pending intents to update a widget from a lenghty background proces.
Thanks
usa business directory

Thank you for this article

Thank you for this article man !

It will be very useful for me ;)

BTscene torrent downloads|Free torrents

mp3 dinle

Forex Signals

I did add a feature to it that would update it every second (instead of every 60) and change the font color to a random color :P.
Forex Signals

Bravo but need help

Great post, bravo!

by waiting I have a problem which nothing to see, but I need some help.

to write in Arabic with a keyboard " normal " that not good with windows.
I will want a simple installation to write in Arabic with my keyboard without spent d 'argent on top of that.

I found this site:
http://www.le-clavier-arabe.fr

but I understand nothing in computer

I so tried since this site: http.arabeclavier.eu

who can help please ?

Kenza

In a couple of books and

In a couple of books and examples from developer.android.com I red about using a Service or a AlarmManager with pending intents to update a widget from a lenghty background proces.Thanks
Menopause Treatment

I just have to say this is an

I just have to say this is an amazing tutorial. I will need this for Christmas this year! I'll try and share it with everyone too and point them to this site. Great tutorial!

Your Hawaii wedding photographer :)

I read about using a Service

I read about using a Service or a AlarmManager with pending intents to update a widget from a lenghty background proces.
Ok, you are just calculating the amount of days, so this won't hurt.
Thanks

Mouse Pads

I did add a feature to it

I did add a feature to it that would update it every second (instead of every 60) and change the font color to a random color :P.
Watch Review

In a couple of books and

In a couple of books and examples from developer.android.com I red about using a Service or a AlarmManager with pending intents to update a widget from a lenghty background proces.

Florida Health Insurance

I read about using a Service

I read about using a Service or a AlarmManager with pending intents to update a widget..
iPhone Unlock 3.1.3

Countdown widgets have always

Countdown widgets have always been very helpful to me to use for business deadlines and such.
french roulette table

cool!

I like android apps! thank to this article!

alloshowtv

I think this is a great

I think this is a great widget.
iPad apps affiliate program

thank you

thank you for this interesting article, regards eric bts dupuis

Good job

Great info, nice article. Thanks for sharing.

HostGuin | DWKZONE | Vegetarian Info

You're great, this is a niche

You're great, this is a niche information.
Banking Info | Smart Review | Budget News

HTC Sense Fail :/

I loved the tutorial, its much harder to find the most basic of walk through than I thought. I did enjoy installing this app (FYI: using dropbox as a server, is a super easy way of doing so), But soon after i installed it, my "HTC Sense UI" started to crash over and over agains :( I had to reboot and take out DaystoXmas while it was still loading. Any ideas as to what would cause this?

I did add a feature to it that would update it every second (instead of every 60) and change the font color to a random color :P.

thanks ahead if someone gets back to me ^_^

Timer (2)

I am also agree with the previous comment about the timer.
And the post was very interested.
I think he remind me a part of my life in morocco when i went to do bivouac maroc.
Thanks.

Lately but surely, I just

Lately but surely, I just read this tutorial; this looks like a pretty good idea to try.

I'll have a try for christmas 2010 :)
- Toiture québec

Thanks

Thanks for the tutorial. But 1 thing, is this a countdown type of widget?
Matthew Proman

Thx this widget tutorial

Thx this widget tutorial think me an other idea for a next time !

Money Tips Website

I read about using a Service

I read about using a Service or a AlarmManager with pending intents to update a widget from a lenghty background proces.
Ok, you are just calculating the amount of days, so this won't hurt.
CFD

nice tutorial

Nice tutorials, it helps me a lot.
it was quite difficult but i begin ! I've just started to understand the basics...
radiateur seche serviette

I red about using a Service

I red about using a Service or a AlarmManager with pending intents to update a widget from a lenghty background proces.
Phoenix pool service

I think this is a great

I think this is a great widget. The only problem is the days section only has two digits. I like to count down to Christmas all year. :)
motorcycle exhausts

Xmas is soon!

it's good, idea, the Xmas widget to Android, i make an android application to christmas in June. ^^

Abou Sofiane

What a lovely gift!

Thanks for sharing such a lovely Christmas gift. The cool stuff with all the Artificial Christmas Trees, Bells, Gift Boxes is spreading effectively the essence of Christmas. I'll surely try it.

Nice!

this is a cool stuff you got here. you can make it look great with its appearance and functions. resume

thank to this post

its very cool article.

cougar

Homes For Sale

That splash screen is so cute!
I think I'm gonna try this one out come Christmas 2010!
Home For Sale | 3.5% FHA | Realestate | 100% VA | Home | Zero Down USDA |

Xmas xmas

We are not children now, but Xmas is always Xmas and we wait this day every year like a children.

chenille caoutchouc

thx

thx

Fun Widget

This was the first widget I have ever tried to make and I have to say that its harder than it looks. I want to thank you for teaching me how to do this, I know this had to take you a long time to put together.

Thanks Toni Miller
Company Checks Designer

Widget

I am not glad to see that it's harder than what it actually looks, but perhaps it's just a minor problem. I will have to see experience this with my own in order to comment about it, but thanks anyway. It reminds me about the electric griddle that fell from the counter and was broken, so I have to look for a new unit.

Neat Widget

I think widgets like this are lots of fun and fairly simple to create. Countdown widgets have always been very helpful to me to use for business deadlines and such. Thanks for the great information.

Thanks Julian Clarkson
Hard Drive Recovery Expert

Thank you for this article

Thank you for this article man !

It will be very useful for me ;)

Yes, you're rigth. I know

Yes, you're right. I know about AlarmManager, but this is the fastest way to create an own widget, simple example. You can read more about widgets with professional solutions at: http://www.helloandroid.com/tutorials/mastering-android-widget-developme...

Timer?

Hi there,

I'm also creating a home screen widget and i was wondering if your approach with the java.util.Timer is correct or not.

In a couple of books and examples from developer.android.com I red about using a Service or a AlarmManager with pending intents to update a widget from a lenghty background proces.
Ok, you are just calculating the amount of days, so this won't hurt.

Also, your updatePeriodMillis is set (to 10000), so every 30 minutes (that's the lowest accepted value), your onUpdate will run again and trigger another Timer...

But for performance and multi-threading support (what will happen with multiple timers or if you use multiple x-mas widgets?), i think you better use a AlarmManager with pending intents, so the appwidgetprovider can be destroyed and recreated, in your case the appwidgetprovider remains in memory all the time...

Any thoughts on this?