In the Calendar app on my Galaxy Tab 10.1, when creating a new event a dialog comes up with Done and Cancel buttons in the title bar/action bar area.
I'd like to implement this in my app. I've tried using setHasOptionsMenu(true)
in addition to overriding onCreateOptionsMenu
in my DialogFragment
subclass, but my action items do not appear. I've also tried calling getDialog().getActionBar()
from within onCreateView
but it always returns null
.
I am able to get this working if I start an Activity
rather than showing a dialog but that takes up the whole screen. Is there a standard way to do this using a DialogFragment
?
Like Veeti implied, you may want to try implementing an Activity with a Dialog Theme. In the Android Manifest:
Hopefully that will help.
I've found a nice way to do it , using setCustomTitle on the builder of the alertDialog for the DialogFragment (also possible on the alertDialog itself).
And the result (from my app ):
If you don't want to use AlertDialog, you can still just put a toolbar into your layout of the dialog and use it.
I'm probably not that experienced in that, but I would use an activity, add action bar items and then :
Hope that helps!
using the idea from a google group post I was able to pull it off styling an activity. you would want to modify the height and width to a "dynamic" size of your choice preferably. Then set whatever ActionBar buttons you would like
--
Had some trouble implementing the suggested solutions from StrikeForceZero and Luke Sleeman, so I wanted to contribute my experience. I'm sure there's just something I'm missing so feedback would be much appreciated.
What I did was the following:
Create a style using the provided PopupTheme, straight copy/paste:
Add the showAsPopup() method as a method in the fragment which would open the fake dialog fragment, straight copy/paste:
Create an instance of the new activity using a simple new() call, and then pass it to the showAsPopup() method:
For the purpose of the test (I was just trying to confirm that I could open an activity that is presented as a dialog with an action bar) I used an extremely simple test, stolen directly from the button view api demo (for the layout file, see buttons_1.xml in the api demos):
Unfortunately, every time I tried this, I get an unspecified null pointer exception on the very first call, activity.requestWindowFeature(Window.FEATURE_ACTION_BAR);
As you can see from the stack trace, the intended behavior is to open the window on a long press on a GoogleMap instance (using the MapFragments from API 2). So my first thought was that there was an issue from trying to open from a Fragment, so I passed the call back to the owning Activity. Same error, same no additional information.
My best guess at this point was that a new() call didn't sufficiently instantiate the class/view in order to make calls to modify its view. Turns out that this appears to be at least somewhat true, as migrating the view modification code into the activity and simply opening the activity in the normal way works:
Calling activity:
New class code:
So I guess the point of me posting all of this is to clarify that if you want to do what the above posters suggest, you can't just new() an activity and call showAsPopup(). This may be my inexperience with Android showing through, but while this seems a bit obvious, it also seems natural to interpret showAsPopup() as being called by the current view, not the view being created, as you're passing in the activity instance (which would just be this if it was supposed to be done in onCreate() like I ended up doing).
So if the intention is to call showAsPopup() in the creating activity and not the created activity, it's not obvious how to get the Activity instance that is modifiable prior to onCreate() being called. The problem being that you can't call things like requestWindowFeature() after setContentView() is called (example), which is a problem since it is typically called in onCreate().
Again, if there is an easy/better way to do this, I would very much appreciate feedback. Hopefully this is helpful for people who want to use this approach.
I know it is not real answer, but above solutions seem so inelegant that one might think that it would be easier to avoid action bar at all, like inheriting your dialog theme from something with .noActionBar and then create view on the top of your panel that imitates action bar, at least this way it all stays in xlm.