I spent a lot of time looking for an android hello world widget how to but I couldn’t find a simple example so I had to spend some time on cutting an existing one into pieces and then start from scratch with a HelloWidget tutorial.

I hope this will help you to get started on android desktop widgets. Please give me some feedback about the tutorial and/or any errors or wrong explanations.

Download the tutorial here. Get the source code here.

17 Responses to “Android: HelloWidget Tutorial”

[...] to Android: HelloWidget Tutorial to get the updated turorial and sourcecode. I consider writing a small eBook. I will probably add [...]

mad.dogg

Juli 28th, 2009 - 21:01

Big thanks for the tutorial. I’ll look forward for the eBok :)

Latrell

August 5th, 2009 - 02:45

Many thanks!

Jeremy

August 7th, 2009 - 22:11

I ran into the same obstacle as you trying to piece together examples of widgets, then I found this link! Thanks!

For the time widget, why do you say the “service way” would eat more battery life (vs the “java way”)?

admin

August 9th, 2009 - 16:20

Hi Jeremy,

concerning the battery life, here’s what my tests tell me so far:
It seems, that the class derived from AppWidgetProvider is instantiated on every onUpdate call (which is called every ‘updatePerdiodMillis’) while my Task thread is hold in memory.
I tested that with some class variables. I created a private String test = “g” and did a test += “g” into the onUpdate method. My displayed String had the maximum of 2 times “g” (gg) while doing the same with a String test2 = “i” in my MyTime-class, I get a new “i” displayed to my widget every second my task is rerun.

I assume, that instantiating a new class every second compared to using an existing class in memory will eat up more battery. On the other hand, we might get the memory space problem when running a lot of widgets that store their stuff in memory which could be the point in this case
… is there any android core developer out there who can give us a statement about that?

ivy

August 13th, 2009 - 08:23

Hi.

Thanks for the tutorial. It helped me a lot. However, i still have questions when it comes to ui and listeners:

1. How can i add at least 3 button with different listeners for each.
2. I tried adding a seekbar and the “desktop widget” displayed “Problem loading widget”.

Hope you can help me with these. Many thanks. :)

Whirpig

Oktober 8th, 2009 - 02:18

Yes, it’s a good tutorial

tibo

November 30th, 2009 - 09:33

Thanks for this tutorial.

I want to update an ImageView on a desktop widget. How can i proceed. (I follow your tutorial and it works fine, but i’m unable to change my ImageView on upate)

Vikrant

Dezember 17th, 2009 - 18:29

Thanks a ton for beautiful tutorials….Well I agree with Jeremy comments about battery life and also it makes the phone unstable. The code instantiates a new class every 1 sec rather than using preloaded class and after 2-3 min the device shows up the signs of fatigue…Tut is good but design can be improved….anywys thnx for tut..

AC

Januar 8th, 2010 - 15:06

Thank you for this. Hugely appreciated. It’s like impossible to find a detailed tutorial on this.

Moppel

März 7th, 2010 - 23:32

Thanks to you! Very fine!

Jared

April 18th, 2010 - 19:52

I love the tutorial, thanks a ton!

I am having one issue with it though. It seems that when I place the widget, it will work for a minute or so. Then after a minute or two of testing on my G1, the app dies and I get the following message “INFO/ActivityManager(30659): Process com.example.hellowidget (pid 4508) has died.”
Now I do believe that this is due to the phone running out of memory. But is there some way to prevent the app in the background from being killed or some other fix for this problem?

rddev

Mai 3rd, 2010 - 21:08

I have got the same problem, does anybody correct this one?

(environment 1.5 for me)

TheSmile

Mai 4th, 2010 - 14:24

Do you guys have a lot of widgets running on your home screen? Remember that all home screen widgets run in the same process. Can you check the memory on the emulator with Eclipse? This might help to find out the problem. You can use your device for debugging over USB too.

lorenz

Juni 10th, 2010 - 13:50

Hello…

I’ve followed your tutorial, compiled it successfully but the widget is installed under launcher app, the widget doesn’t show on widget list (when i do long press on homescreen to choose widget). Need your help.

Thanx in advance

Lo'oris

Juli 7th, 2010 - 16:35

I was optimistic about this tutorial, but… it doesn’t work. I guess it’s too outdated or something. After all, it’s 1.5 based and we have 2.2 now.
A pity.

Tom

Juli 10th, 2010 - 08:24

This tutorial will not work … doing it in 1.5.

I got the following error:

error: Error: No resource found that matches the given name (at ‘resource’ with value ‘@xml/hello_widget_provider’).

Leave a Reply

Let the other Follow you at twitter by clicking here.