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

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

Let’s start from the begining.
Open Eclipse (with android sdk)
File > new Project > other > Android 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:
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:
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="hello">loading widget...</string>
- <string name="app_name">DaysToXmas</string>
- </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:
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.helloandroid.daystoxmas"
- android:versionCode="1"
- android:versionName="1.0">
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <receiver android:name=".DaysToXmas" android:label="@string/app_name">
- <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>
- </application>
- <uses-sdk android:minSdkVersion="4" />
- </manifest>
After that open the main.xml(layout/main.xml) and modify it like this:
- <?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"
- android:background="@drawable/background">
- <TextView
- android:id="@+id/xmas"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello"
- android:paddingTop="35dip" android:paddingLeft="29dip" android:textColor="#000000"/>
- </LinearLayout>
Create directory /res/xml, and copy this to hello_widget_provider.xml.
- <?xml version="1.0" encoding="utf-8"?>
- <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
- android:minWidth="50dip"
- android:minHeight="50dip"
- android:updatePeriodMillis="10000"
- android:initialLayout="@layout/main"
- />
Open the DaysToXmas.java file, located inside DaysToXmas> src > com.helloandroid.daystoxmas. Modify it like this:
- package com.helloandroid.daystoxmas;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.GregorianCalendar;
- import java.util.Timer;
- import java.util.TimerTask;
- import android.appwidget.AppWidgetManager;
- import android.appwidget.AppWidgetProvider;
- import android.content.ComponentName;
- import android.content.Context;
- import android.widget.RemoteViews;
- public class DaysToXmas extends AppWidgetProvider {
- @Override
- int[] appWidgetIds) {
- timer.scheduleAtFixedRate(new MyTime(context, appWidgetManager), 1, 60000);
- }
- RemoteViews remoteViews;
- AppWidgetManager appWidgetManager;
- ComponentName thisWidget;
- this.appWidgetManager = appWidgetManager;
- remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
- thisWidget = new ComponentName(context, DaysToXmas.class);
- }
- @Override
- public void run() {
- long days = (((calendar.getTimeInMillis()- date1.getTime())/1000))/86400;
- remoteViews.setTextViewText(R.id.xmas,""+ days);
- appWidgetManager.updateAppWidget(thisWidget, remoteViews);
- }
- }
- }
We create a timer that will run our MyTime-Thread every 60 seconds.
- 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
Great tutorial for this information
Thanks for your help about this great tutorial.
coklat lebaran coklat valentine coklat tips kecantikan kecantikan Belajar Internet Marketing Belajar Internet Marketing beasiswa s1 Web Studio Indonesia Sarang walet
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
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?