How to create android app with app widget in single application

Harshid picture Harshid · Feb 23, 2013 · Viewed 44.5k times · Source

I have already done one android application which stores date with person name and phone number.

Now I have to develop one widget for this application to show today's (date, person name, and phone number) with button.

Requirements:

  1. Single application with app and widget.
  2. When I click on a button which is in the widget, it will start my application.
  3. Widget data should always synchronize with my app - when today's day (person 5 and application add 5 more person then widget display 10 person data)
  4. How can I design this widget? Any guidelines? I have to use horizontal ScrollView.

I have done a simple widget demo but I don't know how to synchronize it with my application.

Please share your experience and give some idea about my widget and application.

Answer

Roadies picture Roadies · Mar 5, 2013

Create and configure widget

To register a widget you create a BroadcastReceiver with an intent filter for the android.appwidget.action.APPWIDGET_UPDATE action.

<receiver
       android:icon="@drawable/icon"
       android:label="Example Widget"
       android:name="MyWidgetProvider" >
       <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> 

You also specify the meta-data for the widget via the android:name="android.appwidget.provider attribute. The configuration file referred by this meta-data contains the configuration settings for the widget. If contains for example the update interface, the size and the initial layout of the widget.

Create a new file myshape.xml in the /res/drawable directory. This file will define the background we use in your widget.

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
           android:shape="rectangle" >

    <stroke
        android:width="2dp"
        android:color="#FFFFFFFF" />

    <gradient
        android:angle="225"
        android:endColor="#DD2ECCFA"
        android:startColor="#DD000000" />

    <corners
        android:bottomLeftRadius="7dp"
        android:bottomRightRadius="7dp"
        android:topLeftRadius="7dp"
        android:topRightRadius="7dp" />

</shape> 

Define the following widget_layout.xml file under the res/layout folder.

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/layout"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_margin="8dip"
          android:background="@drawable/myshape" >

        <TextView
            android:id="@+id/update"
            style="@android:style/TextAppearance.Medium"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="center_horizontal|center_vertical"
            android:layout_margin="4dip"
            android:text="Static Text" >
        </TextView>

    </LinearLayout> 

now make the class.

public class MyWidgetProvider extends AppWidgetProvider {

  private static final String ACTION_CLICK = "ACTION_CLICK";  

     @Override
  public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

    // Get all ids
    ComponentName thisWidget = new ComponentName(context,
        MyWidgetProvider.class);
    int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
    for (int widgetId : allWidgetIds) {
      // Create some random data
      int number = (new Random().nextInt(100));

      RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
          R.layout.widget_layout);
      Log.w("WidgetExample", String.valueOf(number));
      // Set the text
      remoteViews.setTextViewText(R.id.update, String.valueOf(number));

      // Register an onClickListener
      Intent intent = new Intent(context, MyWidgetProvider.class);

      intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
      intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);

      PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
          0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
      remoteViews.setOnClickPendingIntent(R.id.update, pendingIntent);
      appWidgetManager.updateAppWidget(widgetId, remoteViews);
    }
  }