The Pianist
The Pianist

Reputation: 556

Android popup menu from home screen widget

I want to show a popup menu from a home screen widget like the one in the picture below, but there is a View anchor argument in the PopupMenu constructor method.

But I don't have access to the view in AppWidgetProvider class and only the RemoteViews are available.

So, How can I implement a popup menu in my widget?

Any help would be appreciated. Thanks.

Popup menu from widget

Upvotes: 1

Views: 766

Answers (1)

user1185087
user1185087

Reputation: 4848

You can't use the android.support.v7.widget.PopupMenu directly with RemoteViews. But you are able to copy that behaviour:

  1. Add an anchor view to your RemoteViews layout (this view can be any supported view: https://developer.android.com/guide/topics/appwidgets/index.html#CreatingLayout)
  2. Create a PendingIntent with a defined intent action and set it with setOnClickPendingIntent for the anchor view
  3. Catch that Intent in onReceive of your AppWidgetProviderby the intent action
  4. Call intent.getSourceBounds() to get the screen position of the clicked anchor view
  5. Start a full transparent Activity and transmit the screen position to that Activity
  6. Create and open a PopupMenu at the transmitted screen position in the transparent Activity
  7. finish() the transparent Activity if the user clicks not on an item of the PopupMenu

Upvotes: 1

Related Questions