1

I have several cases where a GridView within a DialogFragment needs a contextual menu. My project supports API 8+, so I'm using the appcompat support library. So far I've tried calling Activity.startSupportActionMode() on the underlying ActionBarActivity, but that actually starts the Contextual ActionBar/ActionMode underneath the dialog. I'm looking for an alternative to the common-but-hacky (and high-overhead) method of using an Activity themed as a dialog.

Community
  • 1
  • 1
MandisaW
  • 971
  • 9
  • 21
  • I would assume because you have not actually asked a question or included any example code of where you might be having an issue. Although, I would not really consider 1 downvote to be worthy of mentioning. – indivisible May 30 '14 at 01:18
  • I've never done the asked-and-answered in one shot before, so I set up the question as basically an intro to the answer below. But I suppose I could put a graphic in... – MandisaW May 30 '14 at 01:30
  • 1
    You should rephrase the question to actually be one. You can ask a general question about how to do what you explain in your answer from the point of view of somebody who doesn't know the answer. Each should be able to stand alone - the question ask, the answer gives a solution. If it helps try to think of how people might search to try and find your question/answer and include many of those terms in the question (using proper language and not just a tag dump). – indivisible May 30 '14 at 01:33

1 Answers1

2

Poking around in the source for both the framework and appcompat support library, we get:

For API 11+ standard framework:

Dialog dialog;
ActionMode.Callback actionMode;

Window window = dialog.getWindow();
View toplevel = window.getDecorView();
if (toplevel == null) { return; }

toplevel.startActionMode (actionMode);

For appcompat support library:

Window window = dialog.getWindow();
View toplevel = window.getDecorView();
if (toplevel == null) { return; }

android.view.ActionMode.Callback frameworkActionMode = 
    new CallbackWrapper (context, supportActionMode);
toplevel.startActionMode (frameworkActionMode);

Note that if you're using DialogFragment, you'll need a reference to getDialog(), which may be null, depending on where you are in the DialogFragment lifecycle.

As of revision 19.1.0 (Mar 2014), the appcompat support library includes an internal class (android.support.v7.internal.view.ActionModeWrapper.CallbackWrapper) that can be used to wrap a support-library ActionMode.Callback in a framework Callback. But if you prefer not to rely on internal classes, you can easily roll your own wrapper.

Community
  • 1
  • 1
MandisaW
  • 971
  • 9
  • 21
  • Thank you. The upper option worked for me also for newer API levels. The lower one (for appcompat) cannot work, just check the type clash. – tm1701 Aug 09 '16 at 17:14