37

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.

Darush
  • 9,625
  • 7
  • 53
  • 53
Harshid
  • 5,438
  • 4
  • 33
  • 49

2 Answers2

36

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);
    }
  }
Community
  • 1
  • 1
Roadies
  • 3,171
  • 2
  • 27
  • 45
8

I am finding couple of days I got somehow the solution. So I am sharing my idea.

This site has been very useful for me.

http://kasperholtze.com/android/how-to-make-a-simple-android-widget/

I made some changes and did my work.

Requirement solution:

1. Make Receiver and put into manifest permission like this.

<receiver
        android:name=".WatchWidget"
        android:label="WatchWidget" >
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>
        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/watch_widget_provider" />
    </receiver>

2. In my side require top TextView click so can make using TextView id in onUpdate().

    remoteViews = new RemoteViews(context.getPackageName(),R.layout.widget_lay);

    Intent intent = new Intent(context, TestActivity.class);
    intent.setAction("callActivity");
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
    remoteViews.setOnClickPendingIntent(R.id.widget_textview, pendingIntent); 
    appWidgetManager.updateAppWidget(new ComponentName(context,WatchWidget.class), remoteViews);

3. I want to call database and fetch record and display it. Now question what about update?

So I have used timer class and run every 1000sec because of over widget is always update 30min(may be).

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

    this.appWidgetManager = appWidgetManager;
    try {
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new MyTime(context, appWidgetManager,appWidgetIds), 1, 1000);

    } catch (Exception e) {
        System.out.println("Exception:");
    }
    updateSwitch1(context, appWidgetManager, appWidgetIds[0]);

}

If any have queries then put a comment and thanks to all who gave me full support to make this answer useful.

Darush
  • 9,625
  • 7
  • 53
  • 53
Harshid
  • 5,438
  • 4
  • 33
  • 49
  • hai i post one question for this link please help me http://stackoverflow.com/questions/18376263/automatically-update-widget-in-the-home-screen-when-i-make-modification-in-my-ap – Satheesh Aug 22 '13 at 12:06
  • The tutorial link gives 500 Internal Server Error. – Enoobong Jul 30 '15 at 14:14