Mastering Android Widget Development - Part1


SDK Version: 
M3

In Days to Xmas tutorial you can see a simple widget example, which demonstrates what widgets are used for, and shows an example how they can work. Now I begin a series of tutorials to fully explain the working of widgets.
We will also create a sample application, during the tutorials, which will show a countdown to a given date in secunds, but things that are not required for this specific example applications will be explained too.

For this first part I will go though mainly the parts described in http://developer.android.com/guide/topics/appwidgets/index.html but I try to give more explanation and advice.

Some general thoughts at first:

Most of the applications has a launcher activity, and the running begins with that, but its not necessary to have one. If you don't have one, the application wont show among the other installed programs giving the user the opportunity to run it.

If an application has a class that implements the AppwidgetProwider class, it will be showed among the available widgets.

Our application will be made up from the widget itself and a configuration activity where you can set the date to countdown to. In the architecture of widgets a "configuration" activity can be defined exactly for this, so we will not need the application to have a launcher activity, if there is a configuration defined it will automatically launched when a new widget is placed on the home screen.

When working with widgets you must keep in mind that the users can place multiple instances of the same widget to the home screen. The functionality of this case must be planned and coded. In the countdown widget id would be fine to the different instances to count to a different date. It will be our goal.

Lets see the basics of how widgets work:

Widgets use 4 intents
•ACTION_APPWIDGET_UPDATE
•ACTION_APPWIDGET_DELETED
•ACTION_APPWIDGET_ENABLED
•ACTION_APPWIDGET_DISABLED

You have to create an XML file with some metadata about the widget. For example countwidget_info.xml :

  1. <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
  2.         android:minWidth="294dp"
  3.         android:minHeight="72dp"
  4.         android:updatePeriodMillis="86400000"
  5.         android:initialLayout="@layout/countdownwidget&quot;
  6.         android:configure="com.helloandroid.countdownexample.countdownConfigure&quot; >
  7. </appwidget-provider>

As you can see here we set:
•the widget size (you cant size it as you like, for details see App Widget Design Guidelines)
•the layout used
•the configuration activity mentioned before
•and the updateperiod

The update period is limited, for example in 1.5 it is said to refres every 30 min even if you set a shorter period.
If you set this an ACTION_APPWIDGET_UPDATE intent will be generated to perform the update.
If the device is asleep when it is time for an update then the device will wake up in order to perform the update.
Because the period can not be set as short as you like, and it wakes up the device we wont use it, we will generate the ACTION_APPWIDGET_UPDATE intents ourself using the AlarmManager class.

The widget must be registered in the AndroidMaifest.xml like this:

  1. <receiver android:name="CountdownWidget" >
  2.     <intent-filter>
  3.         <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
  4.     </intent-filter>    
  5.         <meta-data android:name="android.appwidget.provider"              
  6.         android:resource="@xml/countwidget_info&quot; />
  7. </receiver>

Here we set the CountWidget class to capture the APPWIDGET_UPDATE intent and attach the previously described metadata xml to it.
The system automatically cares about to the DELETE, ENABLE and DISABLE broadsets get captured too.

Now create the CountWidget class which implements the AppWidgetProvider.
At firs lest see the empty methods that we can owerride from the AppWidgetProvider.
In the comments I explain what they are used for.

  1. package com.helloandroid.countdownexample;
  2.  
  3. import android.appwidget.AppWidgetManager;
  4. import android.appwidget.AppWidgetProvider;
  5. import android.content.Context;
  6. import android.content.Intent;
  7.  
  8. public class CountdownWidget extends AppWidgetProvider {
  9.  
  10.         @Override
  11.         public void onDeleted(Context context, int[] appWidgetIds) {
  12.                 //called when widgets are deleted
  13.                 //see that you get an array of widgetIds which are deleted
  14.                 //so handle the delete of multiple widgets in an iteration
  15.                 super.onDeleted(context, appWidgetIds);
  16.         }
  17.  
  18.         @Override
  19.         public void onDisabled(Context context) {
  20.                 super.onDisabled(context);
  21.                 //runs when all of the instances of the widget are deleted from
  22.                 //the home screen
  23.                 //here you can do some setup
  24.         }
  25.  
  26.         @Override
  27.         public void onEnabled(Context context) {
  28.                 super.onEnabled(context);
  29.                 //runs when all of the first instance of the widget are placed
  30.                 //on the home screen
  31.         }
  32.  
  33.         @Override
  34.         public void onReceive(Context context, Intent intent) {
  35.                 //all the intents get handled by this method
  36.                 //mainly used to handle self created intents, which are not
  37.                 //handled by any other method
  38.                
  39.                
  40.                 //the super call delegates the action to the other methods
  41.                
  42.                 //for example the APPWIDGET_UPDATE intent arrives here first
  43.                 //and the super call executes the onUpdate in this case
  44.                 //so it is even possible to handle the functionality of the
  45.                 //other methods here
  46.                 //or if you don't call super you can overwrite the standard
  47.                 //flow of intent handling
  48.                 super.onReceive(context, intent);
  49.         }
  50.  
  51.         @Override
  52.         public void onUpdate(Context context, AppWidgetManager appWidgetManager,
  53.                         int[] appWidgetIds) {
  54.                 //runs on APPWIDGET_UPDATE
  55.                 //here is the widget content set, and updated
  56.                 //it is called once when the widget created
  57.                 //and periodically as set in the metadata xml
  58.                
  59.                 //the layout modifications can be done using the AppWidgetManager
  60.                 //passed in the parameter, we will discuss it later
  61.                
  62.                 //the appWidgetIds contains the Ids of all the widget instances
  63.                 //so here you want likely update all of them in an iteration
  64.                
  65.                 //we will use only the first creation run
  66.                 super.onUpdate(context, appWidgetManager, appWidgetIds);
  67.         }
  68.  
  69. }

And one last thing you must know for the beginning, there is a bug in android 1.5 that the onDeleted method is not called. The code below placed in the onRecive fixes the problem.
  1. final String action = intent.getAction();
  2.     if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
  3.         final int appWidgetId = extras.getInt
  4. (AppWidgetManager.EXTRA_APPWIDGET_ID,
  5.                 AppWidgetManager.INVALID_APPWIDGET_ID);
  6.         if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
  7.             this.onDeleted(context, new int[] { appWidgetId });
  8.         }
  9.     } else {
  10.         super.onReceive(context, intent);
  11.     }

In the next weeks tutorial we will start coding the application.

Comments

As you can see here we set:
•the widget size (you cant size it as you like, for details see App Widget Design Guidelines)
•the layout used
•the configuration activity mentioned before
•and the updateperiod

The update period is limited, for example in 1.5 it is said to refres every 30 min even if you set a shorter period.
If you set this an ACTION_APPWIDGET_UPDATE intent will be generated to perform the update.
If the device is asleep when it is time for an update then the device will wake up in order to perform the update.
Because the period can not be set as short as you like, and it wakes up the device we wont use it, we will generate the ACTION_APPWIDGET_UPDATE intents ourself using the AlarmManager class.
a b c d e f g h i j

=========================================================

ipad bag blog
Sutudeg Community
Education News

=========================================================

a visit b visit c visit d visit e visit f visit g visit h visit i visit j visit k visit l visit m visit n visit o visit p visit q visit r visit s visit t visit u visit v visit w visit x visit y visit z visit aa visit ab visit ac visit ad visit ae visit af visit ag visit ah visit ai visit aj visit ak visit al visit am visit an visit ao visit ap visit aq visit ar visit as visit at visit au visit av visit aw visit ax visit ay visit az visit ba visit bb visit bc visit bd visit be visit bf visit bg visit bh visit bi visit bk visit bl visit bm visit bn visit bo visit bp visit bq visit br visit bs visit bt visit bu visit bv visit bw visit bx visit by visit bz visit ca visit cb visit cc visit cd visit ce visit cf visit cg visit ch ci cj ck cl ccl cm cn co cp cq cr cs ct cu cv

=========================================================

Very interesting articles. Great job done
batik

The Android Market doesn't allow users to easily filter applications based on their license status. As a result a number of projects maintain ad-hoc lists of free and open source software software around the web. In recent times a couple of application repository projects have been started that focus on providing alternatives to the default Android Market. So far coverage of all the available open source applications is still sporadic.

As the android phones become more and more popular, having the knowledge to develop a widget for androids have become a powerful tool. As a riverside lawyer I use an android phone which many different applications to keep me up to date on what is happening with bankruptcy laws.

توبيكات نونو
توبيكات
توبيكات سعودي

توبيكات 2012

توبيكات بنات

العاب نونو

العاب

العاب بنات

games

شات لمني الصوتي

دردشة لمني الصوتية

شات صوتي لمني

شات صوتي

دردشة صوتية

دردشة

دردشه

شات سعودي

شات خليجي
سكر بنات

جات

شات صوتي سعودي خليجي

chat voice

ahj

خليجي الصوتي

سعودي الصوتي

دردشة صوتي

شات صوتي
دردشة صوتية
شات كتابي

شات كتابي خليجي

شات عسل الصوتي

دردشة كتابية

chat
سعودي كول
سعودي كول 6666

كول

سعودي

سعودي كول انحراف

سعودي كول بنات

سعودي كول 1994

chat saudi col ‏

شات سعودي كول

سعودي انحراف

سعودي انحراف2010

سعودي انحراف الصوتي

شات سعودي انحراف

دردشة سعودي انحراف

سعودي انحراف الصوتية

شبكة سعودي انحراف

سعودي انحراف الاصلي

سعودي انحراف كول

سعودي انحراف 2010

انحراف سعودي

saudideviation

دردشة صوتية سعوديه
دردشة صوتية سعودية

دردشة كتابية
دردشة كتابية خليجية
شات
دردشة
خاص للبنات

عرب ذوق

عرب ذوق الصوتي

عرب ذوق الصوتية

دردشة عرب ذوق

شات عرب ذوق

شبكة عرب ذوق

شات صوتي بنات

شات بنات الصوتي

دردشة بنات الصوتي

Girls Chat

شبكة عفناك

صوتية عفناك

شات عفناك

دردشة عفناك

عفناك الصوتي

دردشة عفناك

الخيال
الخيال كام
شبكة الخيال
الخيال الصوتي
الخيال الصوتية
دردشة الخيال
الخيال الصوتية
دردشة صوتية الخيال

شات سعودي خليجي

منتدى نونو

منتدى

منتديات

موقع

شبكة

نونو

Chat Nono

ahj w,jd

]v]am w,jdm

دليل مواقع ويب

دليل مواقع

دليل

مواقع

بنت كول
بنت كول الصوتي
شات بنت كول

دردشة بنت كول
شات بنت كول الصوتية

بنت كول الصوتيه
سعودي كول
صوتية سعودي كول
شات سعودي كول
دردشة سعودي كول
سعودي كول الصوتي
سعودي كول 6666
سعودي كول6666
سكر بنات
شات صوتي زين
شات صوتي ملوك
شات صوتي سعودي
شاتات صوتيه
مكتبة ماسنجر
شات صوتي حبي
شات صوتي كويت
YouTube - Broadcast Yourself.‏ , اليوتيوب نونو
صيف كام
شات صوتي كول
شات انحراف
وه بس
خريطة الموقع نونو
الرياض كول الصوتي
كامات 6666
شات المها
كامات6666
شات كامات 6666
كامات 666
كامات 66
سعودي انحراف
شاتكامات6666
سعودي احوه
شات سعودي احوه
سعودي احوه الصوتي
سعودي احوه كول
دردشة سعودي احوه
احوه سعودي
بنات احوه
دبي الصوتي
سعودي في اي بي الصوتي
شبكة الرياض الصوتي
روعة الليل
لايف كام
الخليج كام
شات كان زمان الصوتي
شات صوتي قصيمي
شات قلبي
ارجوان
شات صوتي قطري
بدور الخليج

منتدى روح

شبكة روح

روح ديزاين

تحميل ماسنجر بلس

توبيكات حزينه

توبيكات

ماسنجر

ماسنجر بلس

تحميل ماسنجر

توبيكات رومنسيه

منتديات روح
دردشة
شات سعودي
خليجي
شات صوتي
توبيك
موقع توبيكات
سعودي كول 6666
سعودي انحراف 2011

سعودي كول
سعودي انحراف

In the architecture of widgets a "configuration" activity can be defined exactly for this, so we will not need the application to have a launcher activity, if there is a configuration defined it will automatically launched when a new widget is placed on the home screen. Property Investment

Nice widget examples, It's very helpful to develop the widget. Thanks for sharing this information.

Rebate Realtor

I found the perfect place for my needs. Contains wonderful and useful messages. I have read most of them and has a lot of them. To me, he's doing the great work. frigidaire dehumidifier || Get your Ex Back

I have found this your post because I have been searching for some information about it almost three hours. You helped me a lot indeed and reading this your article I have found many new and useful information Taylor Lautner Topless

Le drapeau breton. Le symbole de la Bretagne. Egalement nommé Gwenn Ha Du, qui signifie noir et blanc en breton drapeau breton Le drapeau breton. Le symbole de la Bretagne. Egalement nommé Gwenn Ha Du, qui signifie noir et blanc en breton Le symbole de la Bretagne. Egalement nommé Gwenn Ha Du, qui signifie noir et blanc en breton Le symbole de la Bretagne. drapeau breton fanion breizh étendard breton

The update period is limited, for example in 1.5 it is said to refres every 30 min even if you set a shorter period.
If you set this an ACTION_APPWIDGET_UPDATE intent will be generated to perform the update.
If the device is asleep when it is time for an update then the device will wake up in order to perform the update.
Because the period can not be set as short as you like, and it wakes up the device we wont use it, we will generate the ACTION_APPWIDGET_UPDATE intents ourself using the AlarmManager class.
a b c d e f g h i j

it is an Informative article, thanks for sharing.
Solitaire

Yes, I truly agree.

I just bought my new android phone and I would like to learn how to build widgets. I find it fascinating to learn all the Android API.

Besides that, I spend my time with my site refrigerator review.

Most of the applications has a launcher activity, and the running begins with that, but its not necessary to have one. If you don't have one, the application wont show among the other installed programs giving the user the opportunity to run it. f

Nice widget examples, their pretty important when developing android apps as I recently figured out!
beads

Here we set the CountWidget class to capture the APPWIDGET_UPDATE intent and attach the previously described metadata xml to it.
The system automatically cares about to the DELETE, ENABLE and DISABLE broadsets get captured too.

Now create the CountWidget class which implements the AppWidgetProvider.
At firs lest see the empty methods that we can owerride from the AppWidgetProvider.
In the comments I explain what they are used for.

a b c d e

A friend of mine advised this site. And yes. it has some useful pieces of information and I enjoyed reading it. Therefore i would love to drop you a quick note to express my thanks. Take care
Riverside criminal defense attorney

making widgets and applications for android it's so hard. Thanks to blogs like this we can improve our knowledge on android systems. As always, great post!

Mercedes GLK

I am a new comer to this website! But i find it is really useful here! Thanks for sharing all this views and i do appreciate your time for posting such good article!

GSM Kampanya | Bilişim Teknoloji | Cep Telefonu | Turkcell Kampanyaları | Avea Kampanyaları | Vodafone Kampanyaları

Le message est écrit en très bonne manière et qu'elle comporte de nombreuses informations utiles pour moi. Je suis heureux de trouver votre manière distinguée de la rédaction du post. Maintenant, vous le rendre facile pour moi de comprendre et mettre en œuvre le concept. Beauty salon Merci pour le post

I just passed this onto a colleague who was doing a little research on that. And he actually bought me lunch because I found it for him smile So let me rephrase that: i casino su gamblingportal

Good work by the blogger, Thanks a lot for the wonderful share.. Keep it up
windows vps | forex vps

Hmm interesting, thanks for providing the coding examples! not too sure i can get my head around this though, its tough stuff. Will keep trying though i think and of by the way, thanks for all these tutorials!
Tips dan Informasi

Well, I am so excited that I have found this your post because I have been searching for some information about it almost three hours. You helped me a lot indeed and reading this your article I have found many new and useful information about this subject. the diet solution

Hmm interesting, thanks for providing the coding examples! not too sure i can get my head around this though, its tough stuff. Will keep trying though i think and of by the way, thanks for all these tutorials!
Peter, from greenhouses salisbury

We can give them a good education that they can use in their life to get a better chance in this life. Im sure with a good education all of us will see the changing that all that kids can have. Mac DVD Ripper

Companies these days are using ergonomic furniture to ensure that there are no injuries from work related hazards. Use an ergonomic chair and feel better comfort in your back and shoulder. rabattkod

Informative post, I like the article.

doubledome.com

nice tutorial it was helpful as i am new to Android

and also can u give me idea to display multiple widgets on he home screen
from different application(example:from customized home application i have to display
widgets in place of icons for diff apps)think i have cleared

an idea could be helpful

thanx in advance

it is sometimes hard to Pandora in a world of adults Pandora Bracelets

Wow I really Want to develop a widget and i am trying hard on it.Thanks for sharing this info.

Social Networking

Hello! Just dropping by to let you know we featured this post in SoftCity's roundup on application development. Thanks for the great info and links!

http://cafe.softcity.com/article/view/5MjMxMzN/mobile-application-develo...

Nice widget examples, their pretty important when developing android apps as I recently figured out!

vitamin b12 deficiency symptoms

I am a new comer to this website! But i find it is really useful here! Thanks for sharing all this views and i do appreciate your time for posting such good article!
Pop Art
Popart