Question

I'm using the RemoteViewsService.RemoteViewsFactory (link)

to display a listview in a homescreen widget. Everything works fine and I can capture the clicks on the listview items. Now I want to add a button above the listview to allow the user to jump to a config activity:

This is my widget layout xml :

<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/widget_frame"
    android:orientation="vertical" >

 <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

<ListView android:id="@+id/listview"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_marginTop="0dp"
  android:layout_marginLeft="0dp"/>


</LinearLayout> 

This is my onUpdate method within the WidgetProvider.java class:

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

  Log.w(LOG, "onUpdate method called");


  for (int i=0; i<appWidgetIds.length; i++) {

      Intent svcIntent=new Intent(context, WidgetService.class);
      svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
      // http://stackoverflow.com/questions/13199904/android-home-screen-widget-remoteviews-setremoteadapter-method-not-working
      Random generator = new Random();
      int randomNumber = generator.nextInt(1000);
      svcIntent.putExtra("random", randomNumber);
      svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME)));

      RemoteViews remoteviews=new RemoteViews(context.getPackageName(), R.layout.widget_layout);

      //------------------------------------------------------------------------------------
      // trying to capture the button click ... ?
      RemoteViews btn_remoteviews=new RemoteViews(context.getPackageName(), R.id.button1);      

      Intent btn_clickIntent = new Intent(context, Config.class);
      btn_clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
      //PendingIntent btn_pendingIntent = PendingIntent.getBroadcast(context, appWidgetIds[i], btn_clickIntent, 0);
      PendingIntent btn_pendingIntent = PendingIntent.getActivity(context, 0, btn_clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
      btn_remoteviews.setPendingIntentTemplate(R.id.button1, btn_pendingIntent);        
      appWidgetManager.updateAppWidget(appWidgetIds[i], btn_remoteviews);
      //------------------------------------------------------------------------------------




      remoteviews.setRemoteAdapter(appWidgetIds[i], R.id.listview, svcIntent);

      Intent clickIntent=new Intent(context, LoremActivity.class);
      clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
      PendingIntent clickPI=PendingIntent.getActivity(context, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
      remoteviews.setPendingIntentTemplate(R.id.listview, clickPI);       

      appWidgetManager.updateAppWidget(appWidgetIds[i], remoteviews);

  }

super.onUpdate(context, appWidgetManager, appWidgetIds);

}

How do I capture the button click ???

All the clicks on my listview are captured but I can't capture the click on the button just above the listview ???

Thanks a lot for your help!

Was it helpful?

Solution

As stated in the page provided by you:

This AppWidgetProvider defines only the onUpdate() method for the purpose of defining a PendingIntent that launches an Activity and attaching it to the App Widget's button with setOnClickPendingIntent(int, PendingIntent). Notice that it includes a loop that iterates through each entry in appWidgetIds, which is an array of IDs that ide

Also:

As described in Using the AppWidgetProvider Class, you normally use setOnClickPendingIntent() to set an object's click behavior—such as to cause a button to launch an Activity. But this approach is not allowed for child views in an individual collection item (to clarify, you could use setOnClickPendingIntent() to set up a global button in the Gmail app widget that launches the app, for example, but not on the individual list items). Instead, to add click behavior to individual items in a collection, you use setOnClickFillInIntent(). This entails setting up up a pending intent template for your collection view, and then setting a fill-in intent on each item in the collection via your RemoteViewsFactory.

What about that example from the link that you provided? I didn't read that page carefully (you definitely should), but shouldn't you setOnClickPendingIntent() or setOnClickFillInIntent()?:

public class ExampleAppWidgetProvider extends AppWidgetProvider {

    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 provider
        for (int i=0; i<N; i++) {
            int appWidgetId = appWidgetIds[i];

            // Create an Intent to launch ExampleActivity
            Intent intent = new Intent(context, ExampleActivity.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.appwidget_provider_layout);
            views.setOnClickPendingIntent(R.id.button, pendingIntent);

            // Tell the AppWidgetManager to perform an update on the current app widget
            appWidgetManager.updateAppWidget(appWidgetId, views);
        }
    }
}

OTHER TIPS

This is what I ended up doing ...

for (int i=0; i<appWidgetIds.length; i++) {

      Intent svcIntent=new Intent(context, WidgetService.class);
      svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
      // http://stackoverflow.com/questions/13199904/android-home-screen-widget-remoteviews-setremoteadapter-method-not-working
      Random generator = new Random();
      int randomNumber = generator.nextInt(1000);
      svcIntent.putExtra("random", randomNumber);
      svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME)));

      RemoteViews remoteviews=new RemoteViews(context.getPackageName(), R.layout.widget_layout);

      //------------------------------------------------------------------------------------
      // trying to capture the button click ... ?   

      Intent btn_clickIntent = new Intent(context, Config.class);
      btn_clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
      PendingIntent btn_pendingIntent = PendingIntent.getActivity(context, 0, btn_clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
      remoteviews.setOnClickPendingIntent(R.id.button1, btn_pendingIntent);     

      Intent btn2_clickIntent = new Intent(context, Config2.class);
      btn2_clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
      PendingIntent btn2_pendingIntent = PendingIntent.getActivity(context, 0, btn2_clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
      remoteviews.setOnClickPendingIntent(R.id.button2, btn2_pendingIntent);        

      remoteviews.setRemoteAdapter(appWidgetIds[i], R.id.listview, svcIntent);

      Intent clickIntent=new Intent(context, LoremActivity.class);
      clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
      PendingIntent clickPI=PendingIntent.getActivity(context, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
      remoteviews.setPendingIntentTemplate(R.id.listview, clickPI);       

      appWidgetManager.updateAppWidget(appWidgetIds[i], remoteviews);  
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top