Upgrade to Pro — share decks privately, control downloads, hide ads and more …

How to create a widget for your application

How to create a widget for your application

Oursky Limited

February 05, 2012
Tweet

More Decks by Oursky Limited

Other Decks in Programming

Transcript

  1. My first lesson of Android How to create a widget

    for your application Friday, 13 January, 12
  2. In AndroidManifest.xml • Adding new element to specify the widget

    <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="oursky.spentable" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="3" android:targetSdkVersion="14" /> <application android:label="@string/app_name" android:icon="@drawable/ic_launcher" android:theme="@style/AppTheme"> <activity android:name=".ui.MainActivity" android:label="@string/app_name" android:screenOrientation="portrait"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".ui.SettingsActivity" android:label="@string/settings" android:screenOrientation="portrait"> </activity> <receiver android:name=".ui.WidgetProvider" 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/widget_info" /> </receiver> </application> </manifest> Friday, 13 January, 12
  3. res/xml/widget.xml • Android home screen is 4x4 grid and the

    formula to calculate the size • size = (number of cells * 74) – 2 • android:updatePeriodMillis - How often, in milliseconds, that this AppWidget wants to be updated. • Updates requested with updatePeriodMillis will not be delivered more than once every 30 minutes. <?xml version="1.0" encoding="utf-8"?> <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:initialLayout="@layout/widget" android:minHeight="72dp" android:minWidth="294dp" android:updatePeriodMillis="1000" > </appwidget-provider> Friday, 13 January, 12
  4. res/layout/widget.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="4dp" android:background="@drawable/widget_background"

    android:gravity="center" android:orientation="vertical" > <TextView android:id="@+id/widget1label" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/new_expenses" /> </LinearLayout> Friday, 13 January, 12
  5. WidgetProvider public class WidgetProvider extends AppWidgetProvider { private SpentableDbAdapter mDbAdapter;

    Formatter f = new Formatter(); public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { final int N = appWidgetIds.length; // Perform this loop procedure for each App Widget that belongs to this mDbAdapter = new SpentableDbAdapter(context); for (int i = 0; i < N; i++) { int appWidgetId = appWidgetIds[i]; // Create an Intent to launch ExampleActivity Intent intent = new Intent(context, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); // Get the layout for the App Widget and attach an on-click listener // to the button RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget); views.setOnClickPendingIntent(R.id.button, pendingIntent); // To update a label int total = mDbAdapter.getAmountSum(); views.setTextViewText(R.id.widget1label, f.format("Total Expenses: %,d", total).toString()); // Tell the AppWidgetManager to perform an update on the current app appWidgetManager.updateAppWidget(appWidgetId, views); } } public void onReceive (Context context, Intent intent){ String action = intent.getAction(); if (action.equals(AppWidgetManager.ACTION_APPWIDGET_UPDATE)){ AppWidgetManager gm = AppWidgetManager.getInstance(context); int[] ids = gm.getAppWidgetIds(new ComponentName(context, WidgetProvider.class)); this.onUpdate(context, gm, ids); } } } Friday, 13 January, 12