Mastering Android Widget Development - Part2


SDK Version: 
M3

In Mastering Android Widget Development - Part1 we have gone trough the basics of appwidgets. Now we start to develop the example application, which couts time left to a given date.

First implement the configuration activity. It will contain a DatePicker, an OK button and a Cancel button, defined in the configuration.xml layout:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3.   xmlns:android="http://schemas.android.com/apk/res/android"
  4.   android:layout_width="wrap_content"
  5.   android:layout_height="wrap_content"
  6.   android:orientation="vertical">
  7.         <DatePicker android:layout_width="wrap_content"
  8.         android:layout_height="wrap_content"
  9.         android:id="@+id/DatePicker">
  10.         </DatePicker>
  11.        
  12.         <LinearLayout
  13.         android:id="@+id/LinearLayout01";
  14.         android:layout_width="wrap_content"
  15.         android:layout_height="wrap_content"
  16.         android:orientation="horizontal">
  17.        
  18.                 <Button android:layout_width="wrap_content"
  19.                 android:layout_height="wrap_content"
  20.                 android:id="@+id/okbutton"
  21.                 android:text="OK"></Button>
  22.                 <Button android:layout_width="wrap_content"
  23.                 android:layout_height="wrap_content"
  24.                 android:text="Cancel"
  25.                 android:id="@+id/cancelbutton"></Button>
  26.         </LinearLayout>
  27. </LinearLayout>

We store the choosen date in SharedPreferences, if OK is pressed, and cancel widget creation on cancel button.
The code below shows the configuration activity with comments.

  1. package com.helloandroid.countdownexample;
  2.  
  3. import java.util.Date;
  4.  
  5. import android.app.Activity;
  6. import android.appwidget.AppWidgetManager;
  7. import android.content.Context;
  8. import android.content.Intent;
  9. import android.content.SharedPreferences;
  10. import android.os.Bundle;
  11. import android.view.View;
  12. import android.view.View.OnClickListener;
  13. import android.widget.Button;
  14. import android.widget.DatePicker;
  15.  
  16. public class CountdownConfiguration extends Activity {
  17.         private Context self = this;
  18.         private int appWidgetId;
  19.  
  20.         @Override
  21.         protected void onCreate(Bundle savedInstanceState) {
  22.                 super.onCreate(savedInstanceState);
  23.                 // get the appWidgetId of the appWidget being configured
  24.                 Intent launchIntent = getIntent();
  25.                 Bundle extras = launchIntent.getExtras();
  26.                 appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
  27.                                 AppWidgetManager.INVALID_APPWIDGET_ID);
  28.  
  29.                 // set the result for cancel first
  30.                 // if the user cancels, then the appWidget
  31.                 // should not appear
  32.                 Intent cancelResultValue = new Intent();
  33.                 cancelResultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
  34.                                 appWidgetId);
  35.                 setResult(RESULT_CANCELED, cancelResultValue);
  36.                 // show the user interface of configuration
  37.                 setContentView(R.layout.configuration);
  38.  
  39.                 // the OK button
  40.                 Button ok = (Button) findViewById(R.id.okbutton);
  41.                 ok.setOnClickListener(new OnClickListener() {
  42.                         @Override
  43.                         public void onClick(View arg0) {
  44.                                 // get the date from DatePicker
  45.                                 DatePicker dp = (DatePicker) findViewById(R.id.DatePicker);
  46.                                 GregorianCalendar date = new GregorianCalendar(dp.getYear(), dp
  47.                                                 .getMonth(), dp.getDayOfMonth());
  48.  
  49.                                 // save the goal date in SharedPreferences
  50.                                 // we can only store simple types only like long
  51.                                 // if multiple widget instances are placed
  52.                                 // each can have own goal date
  53.                                 // so store it under a name that contains appWidgetId
  54.                                 SharedPreferences prefs = self.getSharedPreferences("prefs", 0);
  55.                                 SharedPreferences.Editor edit = prefs.edit();
  56.                                 edit.putLong("goal" + appWidgetId, date.getTime());
  57.                                 edit.commit();
  58.  
  59.                                 // change the result to OK
  60.                                 Intent resultValue = new Intent();
  61.                                 resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
  62.                                                 appWidgetId);
  63.                                 setResult(RESULT_OK, resultValue);
  64.                                 // finish closes activity
  65.                                 // and sends the OK result
  66.                                 // the widget will be be placed on the home screen
  67.                                 finish();
  68.                         }
  69.                 });
  70.  
  71.                 // cancel button
  72.                 Button cancel = (Button) findViewById(R.id.cancelbutton);
  73.                 cancel.setOnClickListener(new OnClickListener() {
  74.                         @Override
  75.                         public void onClick(View arg0) {
  76.                                 // finish sends the already configured cancel result
  77.                                 // and closes activity
  78.                                 finish();
  79.                         }
  80.                 });
  81.         }
  82. }

We must also register the activity in the AndroidManifest.xml:

  1. <activity
  2. android:name="CountdownConfiguration&quot;
  3. android:label="@string/app_name">
  4. <intent-filter>
  5.     <action
  6.         android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
  7.     </intent-filter>
  8. </activity>

Now, lets place something on the appWidget itself to be wisible.
We crate a layout for this named countdownwidget.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3.   xmlns:android="http://schemas.android.com/apk/res/android"
  4.   android:layout_width="wrap_content"
  5.   android:layout_height="wrap_content" android:orientation="vertical">
  6.  
  7. <TextView android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello!"></TextView>
  8. </LinearLayout>

And for last today we show this layout on the widget with the code below in the CountdownWidget class onUpdate method.
It loops trough all of the given appwidgetIds and sets up the layout for each.

  1. for (int appWidgetId : appWidgetIds) {
  2.                 RemoteViews remoteView = new RemoteViews(context.getPackageName(),
  3.                                 R.layout.countdownwidget);
  4.                 appWidgetManager.updateAppWidget(appWidgetId, remoteView);
  5.         }
  6. super.onUpdate(context, appWidgetManager, appWidgetIds);

To be continued...

Comments

Good post. Very impressive. Thanks for sharing.
android tablet
wenger backpack

Thank you very much. I couldn't find the second part of your article. Good job!
Mick from mobile app development

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

توبيكات 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

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

thank you for information

حجز فنادق مكة
فنادق مكة

This article gives the light in which we can observe the reality. this is very nice one and gives in-depth information. thanks for this nice article... Mercedes-Benz Mobil Mewah Terbaik Indonesia | Mercedes-Benz Mobil Mewah Terbaik Indonesia | Meriahkan pesta ulang tahun bersama GarudaFood | Mari Berkomunitas Di Faceblog | Harga Jual Blackberry iPhone Laptop Murah | Video Music | Mp3 | Beauty and Health

This features that help you to have innovative customisation for Android unlock store. To unlock the phone, there is simple slide bar to unlock the phone. Widget locker gives two options to unlock the screen and make it a useful tool. First thing is that on unlock screen any android widget can be placed. Baju Muslim | Property Investment

Android is a software stack for mobile devices that includes an operating system, middleware and key applications. business plan writing

I was very pleased to find this site.I wanted to thank you for this great read.
vps hosting | forex vps

This features that help you to have innovative customisation for Android unlock store.

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

This features that help you to have innovative customisation for Android unlock store. To unlock the phone, there is simple slide bar to unlock the phone. Widget locker gives two options to unlock the screen and make it a useful tool. First thing is that on unlock screen any android widget can be placed.
Wholesale

this is really helpful since im in too widgets. tax liens

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. Madaline Oceguera

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

Oh, how well thought out. It is necessary to take advantage. Needed to keep pace with the age of 21! Beauty salon

I thank the very Code, which provides management services Almmmezp each member who participates Bmodia deals and the progress of the new and more Thank you
شات
دردشة
شات مصرى
شات مصرية
دردشة مصرية
دردشة مصر
منتديات بحبك
شات بنات
شات مصر
شات بنات مصر
العاب
دليل
اختصار روابط

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

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

Bonjour, pour ajouter un widget à l’écran d’accueil de ton tel, il suffit d’appuyer 3 secondes sur l’ecran, une liste va alors s’afficher. il suffit de naviguer dans Applications, puis choisir celle qui t’intéresse.
tu verras alors le widget s’afficher sur l’ecran d’accueil.
Generique viagra generique, acheter clomid femme acheter clomid.

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. lån

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

Nice job guys :)