0

I've got a simple homescreen widget, which displays a list of items. Since I want to support older devices, instead of ListView I'm using a series of TextView objects that "emulate" ListView.

It works really well, but I'd like to assign a click listener for these items. In the listener I'd like to distinguish between them and take action depengind on item's content.

How to do this? Since setOnClickPendingIntent takes view's ID, I can't assign separate intents for individual items - they all share the same ID, so the handler won't know which item was tapped. I can instantiate RemoteViews only from XML layout, so I can't add items with different ID's (I'd need to create a lot of layout files, differing only by layout ID).

Since Honeycomb there is setOnClickFillInIntent method that deals with my problem in an acceptable way, but I can't use it if I want my widget to work on Gingerbread.

2 Answers 2

1

I can't assign separate intents for individual items - they all share the same ID, so the handler won't know which item was tapped.

Your widgets have to have unique IDs.

I can instantiate RemoteViews only from XML layout, so I can't add items with different ID's

Sure you can. You have one layout file per app widget size (possibly, therefore, only one layout file period if you are only supporting one size). In that layout file, you give your widgets unique IDs.

3
  • Currently, I have a layout file for the widget, with one empty container and other file for the row layout. I'm inserting rows into that container using RemoteViews.addView(ID,RemoteViews) during widget update. From what I understand from you answer, do you suggest that I should hardcode some rows with different IDs in the widget XML and stop using addView (and then, setup clickIntents for every "row")? Feb 12, 2012 at 21:03
  • @user1234567: Well, the addView() logic isn't helping you much. You can only have so many rows for a given app widget size. And, you have to hardwire this in. And, since you need unique widget IDs anyway, you may as well just manually clone the row N times into your main app widget layout and supply the unique IDs. Feb 12, 2012 at 21:08
  • Well, this can be even a bit faster, since I will inflate XML only once per update and will not modify it's view tree after that (like I used to with addView() method). Thanks! Feb 12, 2012 at 21:11
0

You might be able to! I had the same issue, but then I realized that I was generating the rows (individual rows within a layout, each row from the same template) one at a time. Each row was its own RemoteViews, so within that for loop, I set the intent on itself. For example, if I'm adding new innerRemoteViews to outerRemoteViews, I'd call:

innerRemoteViews.setOnClickPendingIntent(R.id.inner_row_template_id, PendingIntent.getActivity(context, 0, intent, 0));

This way, the Intent gets added to the innerRemoteViews, where the id is still specific to that row.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.