home widgets

android home widgets sample code

Search Google for: android home widgets sample code

Search Android Developers Group for: android home widgets sample code

Search Android Beginers Group for: android home widgets sample code

Search Google Code for: android home widgets sample code

Search Android Issues Database for: android home widgets sample code

search 1.5 samples if there are any examples

blog: introducing home screen widgets

Here is an article:Creating a Home Screen App Widget on Android

Take a look at this as well

titled: 'Introducing Home Screen Widgets'

Read this widget design guidelines

Here is a formal topic on app widgets

Android RemoteViews

Search Google for: Android RemoteViews

Search Android Developers Group for: Android RemoteViews

Search Android Beginers Group for: Android RemoteViews

Search Google Code for: Android RemoteViews

Search Android Issues Database for: Android RemoteViews

Class overview of RemoteViews

lifecycle of a broadcast receiver

Sample code for word of the day

Here is a weblog to go with it

Users can also interact with your app through the widget, for example pausing or switching music tracks. If you have a background service, you can push widget updates on your own schedule, or the AppWidget framework provides an automatic update mechanism.

when a timer goes off, you may not want to perform a lengthy operation as part of the timer. Instead let the timer start a service. This service can take 10 minutes. Then the service has the ability to broadcast out the values to the widgets.


public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {
    // To prevent any ANR timeouts, we perform the update in a service
    context.startService(new Intent(context, UpdateService.class));
}

Notice how similar it is to start a service as to start an activity. Both use an intent and gets invoked in a similar manner.

read this blog post as well:what-the-docs-dont-tell-you

Here is a forecast app widget from Jeff Sharkey

AppWidgetManager API reference

Android app widgets edit controls

Search Google for: Android app widgets edit controls

Search Android Developers Group for: Android app widgets edit controls

Search Android Beginers Group for: Android app widgets edit controls

Search Google Code for: Android app widgets edit controls

Search Android Issues Database for: Android app widgets edit controls

This is called to update the App Widget at intervals defined by the updatePeriodMillis attribute in the AppWidgetProviderInfo. This method is also called when the user adds the App Widget, so it should perform the essential setup, such as define event handlers for Views and start a temporary Service, if necessary.

However, if you have declared a configuration Activity, this method is not called when the user adds the App Widget, but is called for the subsequent updates. It is the responsibility of the configuration Activity to perform the first update when configuration is done.

This is called every time an App Widget is deleted from the App Widget host.

This is called when an instance the App Widget is created for the first time. For example, if the user adds two instances of your App Widget, this is only called the first time. If you need to open a new database or perform other setup that only needs to occur once for all App Widget instances, then this is a good place to do it.

This is called when the last instance of your App Widget is deleted from the App Widget host. This is where you should clean up any work done in onEnabled(Context), such as delete a temporary database.

This is called for every broadcast and before each of the above callback methods. You normally don't need to implement this method because the default AppWidgetProvider implementation filters all App Widget broadcasts and calls the above methods as appropriate.

Here is the "ondeleted" bug thread


@Override 
public void onReceive(Context context, Intent intent) { 
    final String action = intent.getAction(); 
    if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) { 
        final int appWidgetId = extras.getInt 
(AppWidgetManager.EXTRA_APPWIDGET_ID, 
                AppWidgetManager.INVALID_APPWIDGET_ID); 
        if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) { 
            this.onDeleted(context, new int[] { appWidgetId }); 
        } 
    } else { 
        super.onReceive(context, intent); 
    } 



}

<meta-data android:name="android.appwidget.provider"
         android:resource="@xml/appwidget_provider" />

//Look for your widget provider class name
ComponentName thisWidget = new ComponentName(this, WordWidget.class);

//get the app widget manager
AppWidgetManager manager = AppWidgetManager.getInstance(this);

//update all instances of this widget
manager.updateAppWidget(thisWidget, updateViews);

//or
int widgetId = 12;
manager.updateAppWidget(12, updateViews);

//Where updateviews are the remoteviews

This means if you want to manage state of a widget between two calls to update, you will need to manage that state using either static variables, registries, or preferences, or a combination of these techniques.


// Find the widget id from the intent. 
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        if (extras != null) {
            mAppWidgetId = extras.getInt(
                    AppWidgetManager.EXTRA_APPWIDGET_ID, 
                    AppWidgetManager.INVALID_APPWIDGET_ID);
        }

<activity android:name=".ExampleAppWidgetConfigure">
    <intent-filter>
        <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
    </intent-filter>
</activity>

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    ...
    android:configure="com.example.android.ExampleAppWidgetConfigure" 
    ... >
</appwidget-provider>

It is the responsibility of the configuration Activity to request an update from the AppWidgetManager when the App Widget is first created.

The App Widget host calls the configuration Activity and the configuration Activity should always return a result. The result should include the App Widget ID passed by the Intent that launched the Activity (saved in the Intent extras as EXTRA_APPWIDGET_ID).


Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
setResult(RESULT_OK, resultValue);
finish();

how does android app widget work if there is no time interval

Search for: how does android app widget work if there is no time interval

How can I clean up previous instances of android app widgets

Search Google for: How can I clean up previous instances of android app widgets

Search Android Developers Group for: How can I clean up previous instances of android app widgets

Search Android Beginers Group for: How can I clean up previous instances of android app widgets

Search Google Code for: How can I clean up previous instances of android app widgets

Search Android Issues Database for: How can I clean up previous instances of android app widgets

is there an android widget manager UI?

Search for: is there an android widget manager UI?

finish receiver called but none active

Search Google for: finish receiver called but none active

Search Android Developers Group for: finish receiver called but none active

Search Android Beginers Group for: finish receiver called but none active

Search Google Code for: finish receiver called but none active

Search Android Issues Database for: finish receiver called but none active

android widgets custom scheduling

Search for: android widgets custom scheduling

See this note on uninstalling app widgets

User is expected to clean up the widgets before uninstalling.

uninstall android app widgets

Search for: uninstall android app widgets

This is a better search

Read this to see more of uninstall

when a configuration activity is invoked on behalf of an instance, it may still be emitting update events. You may want to check for this fact.

are static variables retained between two android appwidget invocations?

Search for: are static variables retained between two android appwidget invocations?

android static variables

Search for: android static variables

what is the lifecycle of an android broadcast receiver

Search for: what is the lifecycle of an android broadcast receiver

Read up on the lifecycle of services

android broadcast receiver local service

Search for: android broadcast receiver local service

Understand SharedPreferences

Search for: Understand SharedPreferences

Here is the API reference

Here is the API for the SharedPreferences editor

Here is an interesting app with preferences and passwords

Search for: android SharedPreferences

Do you need to set something for persisting shared preferences?

The post seem to suggest to set android:persistent=true.

Here is a longer discussion on the topic

api: getSharedPreferences

where are android shared preferences stored?

Search for: where are android shared preferences stored?

file location of android shared preferences

Search for: file location of android shared preferences

Can I have two app widgets in one android apk file or package?

Search for: Can I have two app widgets in one android apk file or package?


FrameLayout 
LinearLayout 
RelativeLayout 

AnalogClock 
Button 
Chronometer 
ImageButton 
ImageView 
ProgressBar 
TextView

The values for the minWidth and minHeight attributes specify the minimum area required by the App Widget's layout.

The default Home screen positions App Widgets in its window based on a grid of cells that have a defined height and width. If the values for an App Widget's minimum width or height don't match the dimensions of the cells, then the App Widget dimensions round up to the nearest cell size. (See the App Widget Design Guidelines for more information on the Home screen cell sizes.)

Because the Home screen's layout orientation (and thus, the cell sizes) can change, as a rule of thumb, you should assume the worst-case cell size of 74 pixels for the height and width of a cell.

However, you must subtract 2 from the final dimension to account for any integer rounding errors that occur in the pixel count. To find your minimum width and height in density-independent pixels (dp), use this formula:

(number of cells * 74) - 2

Following this formula, you should use 72 dp for a height of one cell, 294 dp and for a width of four cells.

If the device is asleep when it is time for an update (as defined by updatePeriodMillis), then the device will wake up in order to perform the update. If you don't update more than once per hour, this probably won't cause significant problems for the battery life.

If, however, you need to update more frequently and/or you do not need to update while the device is asleep, then you can instead perform updates based on an alarm that will not wake the device.

To do so, set an alarm with an Intent that your AppWidgetProvider receives, using the AlarmManager. Set the alarm type to either ELAPSED_REALTIME or RTC, which will only deliver the alarm when the device is awake. Then set updatePeriodMillis to zero ("0").

See a follow up notes as we move on to honeycomb