ActionBar in dialog box

In the Calendar application on my Galaxy Tab 10.1 tab, when I create a new event, the Finish and Cancel buttons appear in the title bar / action bar in the dialog box.

enter image description here

I would like to implement this in my application. I tried using setHasOptionsMenu(true) in addition to overriding onCreateOptionsMenu in my DialogFragment subclass, but my action items are not displayed. I also tried calling getDialog().getActionBar() from onCreateView , but always returns null .

I can get this working if I start an Activity and not display a dialog, but it takes up the whole screen. Is there a standard way to do this using DialogFragment ?

+45
android android-actionbar android-dialogfragment
Jul 11 '12 at 3:14
source share
8 answers

using the idea from the google group post , I was able to remove it from creating the style. You would like to change the height and width to the β€œdynamic” size of your choice. Then set any ActionBar buttons you would like

 <style name="PopupTheme" parent="android:Theme.Holo.Light.Dialog"> <item name="android:windowIsFloating">false</item> <item name="android:windowContentOverlay">@null</item> <item name="android:windowSoftInputMode">stateAlwaysHidden</item> <item name="android:windowActionModeOverlay">true</item> <item name="android:windowIsTranslucent">true</item> </style> 

-

 public static void showAsPopup(Activity activity) { //To show activity as dialog and dim the background, you need to declare android:theme="@style/PopupTheme" on for the chosen activity on the manifest activity.requestWindowFeature(Window.FEATURE_ACTION_BAR); activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND, WindowManager.LayoutParams.FLAG_DIM_BEHIND); LayoutParams params = activity.getWindow().getAttributes(); params.height = 850; //fixed height params.width = 850; //fixed width params.alpha = 1.0f; params.dimAmount = 0.5f; activity.getWindow().setAttributes((android.view.WindowManager.LayoutParams) params); setContentView(R.layout.activity_main); } 
+83
Sep 05
source share
β€” -

If you are using ActionBarSherlock, declare a topic below:

 <style name="PopupTheme" parent="Theme.Sherlock"> <item name="android:windowFrame">@null</item> <item name="android:windowIsFloating">false</item> <item name="android:windowContentOverlay">@null</item> <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item> <item name="android:windowSoftInputMode">stateAlwaysHidden</item> <item name="android:windowActionModeOverlay">true</item> <item name="android:colorBackgroundCacheHint">@null</item> <item name="android:windowCloseOnTouchOutside">true</item> <item name="android:windowIsTranslucent">true</item> <item name="windowContentOverlay">@null</item> </style> 

And, initialize SherlockActivity with PopupTheme in accordance with Luke Slaman's Answer .

 private void showAsPopup(SherlockActivity activity) { //To show activity as dialog and dim the background, you need to declare android:theme="@style/PopupTheme" on for the chosen activity on the manifest //activity.requestWindowFeature(Window.FEATURE_ACTION_BAR); // NO NEED to call this line. activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND, WindowManager.LayoutParams.FLAG_DIM_BEHIND); LayoutParams params = activity.getWindow().getAttributes(); params.alpha = 1.0f; params.dimAmount = 0.5f; activity.getWindow().setAttributes((android.view.WindowManager.LayoutParams) params); // This sets the window size, while working around the IllegalStateException thrown by ActionBarView activity.getWindow().setLayout(width,height); } 

Result:

enter image description here

+14
Mar 04 '13 at 16:22
source share

There were problems with the implementation of the proposed solutions from StrikeForceZero and Luke Sleeman, so I wanted to contribute. I am sure that just something is missing me, so the reviews will be very grateful.

I have done the following:

  • Create a style using the provided PopupTheme, direct copy / paste:

     <style name="PopupTheme" parent="android:Theme.Holo.Light.Dialog"> <item name="android:windowIsFloating">false</item> <item name="android:windowContentOverlay">@null</item> <item name="android:windowSoftInputMode">stateAlwaysHidden</item> <item name="android:windowActionModeOverlay">true</item> <item name="android:windowIsTranslucent">true</item> </style> 
  • Add the showAsPopup () method as a method in the fragment that will open the fake dialog fragment, direct copy / paste:

     private void showAsPopup(Activity activity) { //To show activity as dialog and dim the background, you need to declare android:theme="@style/PopupTheme" on for the chosen activity on the manifest activity.requestWindowFeature(Window.FEATURE_ACTION_BAR); activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND, WindowManager.LayoutParams.FLAG_DIM_BEHIND); LayoutParams params = activity.getWindow().getAttributes(); params.alpha = 1.0f; params.dimAmount = 0f; activity.getWindow().setAttributes((android.view.WindowManager.LayoutParams) params); // This sets the window size, while working around the IllegalStateException thrown by ActionBarView activity.getWindow().setLayout(850,850); } 
  • Create an instance of the new action with a simple call to new (), and then pass it to the showAsPopup () method:

     DialogTestActivity test = new DialogTestActivity(); showAsPopup(test); 
  • For testing (I was just trying to confirm that I could open the activity represented as a dialog with the action bar), I used an extremely simple test, stolen directly from the api demo button view (for the layout file, see the buttons_1.xml file in the api examples) :

     public class DialogTestActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.buttons_test); } } 

Unfortunately, every time I tried this, I get an unspecified null pointer exception on the first call, activity.requestWindowFeature (Window.FEATURE_ACTION_BAR);

 04-29 16:39:05.361: W/System.err(15134): java.lang.NullPointerException 04-29 16:39:05.361: W/System.err(15134): at android.app.Activity.requestWindowFeature(Activity.java:3244) 04-29 16:39:05.371: W/System.err(15134): at packagenameremovedforlegalreasons.classname.showAsPopup(classname.java:602) 04-29 16:39:05.371: W/System.err(15134): at packagenameremovedforlegalreasons.classname.onMapLongClick(classname.java:595) 04-29 16:39:05.371: W/System.err(15134): at com.google.android.gms.maps.GoogleMap$5.onMapLongClick(Unknown Source) 04-29 16:39:05.371: W/System.err(15134): at com.google.android.gms.internal.k$a.onTransact(Unknown Source) 04-29 16:39:05.381: W/System.err(15134): at android.os.Binder.transact(Binder.java:310) 04-29 16:39:05.381: W/System.err(15134): at com.google.android.gms.maps.internal.IOnMapLongClickListener$Stub$Proxy.onMapLongClick(IOnMapLongClickListener.java:93) 04-29 16:39:05.381: W/System.err(15134): at maps.isa(Unknown Source) 04-29 16:39:05.381: W/System.err(15134): at maps.yvd(Unknown Source) 04-29 16:39:05.381: W/System.err(15134): at maps.y.bf.onLongPress(Unknown Source) 04-29 16:39:05.381: W/System.err(15134): at maps.dvonLongPress(Unknown Source) 04-29 16:39:05.381: W/System.err(15134): at maps.dhc(Unknown Source) 04-29 16:39:05.381: W/System.err(15134): at maps.dhc(Unknown Source) 04-29 16:39:05.381: W/System.err(15134): at maps.djhandleMessage(Unknown Source) 04-29 16:39:05.391: W/System.err(15134): at android.os.Handler.dispatchMessage(Handler.java:99) 04-29 16:39:05.391: W/System.err(15134): at android.os.Looper.loop(Looper.java:137) 04-29 16:39:05.391: W/System.err(15134): at android.app.ActivityThread.main(ActivityThread.java:5041) 04-29 16:39:05.391: W/System.err(15134): at java.lang.reflect.Method.invokeNative(Native Method) 04-29 16:39:05.391: W/System.err(15134): at java.lang.reflect.Method.invoke(Method.java:511) 04-29 16:39:05.391: W/System.err(15134): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 04-29 16:39:05.391: W/System.err(15134): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 04-29 16:39:05.391: W/System.err(15134): at dalvik.system.NativeStart.main(Native Method) 

As you can see from the stack trace, the intended behavior is to open the window by long clicking on the GoogleMap instance (using MapFragments from API 2). So my first thought was that because of the attempt to open from the fragment, a problem arose, so I transferred the call back to my own activity. The same error, the same no additional information.

My best guess at this point was that calling new () did not create an instance of the class / view enough to make calls to change its view. It turns out that this is at least somewhat true, since migrating the view modification code into activity and just opening the operation normally works:

Defiant activity:

  public void openMapDialog() { Intent intent = new Intent(this, DialogTestActivity.class); startActivity(intent); } 

New class code:

  public class DialogTestActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // From: https://stackoverflow.com/questions/11425020/actionbar-in-a-dialogfragment this.requestWindowFeature(Window.FEATURE_ACTION_BAR); this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND, WindowManager.LayoutParams.FLAG_DIM_BEHIND); LayoutParams params = this.getWindow().getAttributes(); params.alpha = 1.0f; params.dimAmount = 0f; this.getWindow().setAttributes((android.view.WindowManager.LayoutParams) params); // This sets the window size, while working around the IllegalStateException thrown by ActionBarView this.getWindow().setLayout(600,600); setContentView(R.layout.buttons_test); } } 

So, I think that all I do is find out that if you want to do what the above indicators offer, you cannot just activate new () activity and call showAsPopup (). Perhaps this is my inexperience with Android, but although it seems a little obvious, it also seems natural to interpret showAsPopup () as being called by the current view, not the created view, as you are passing an instance of the activity (which would be this if it were to be done in onCreate (), as I did).

So, if the goal is to call showAsPopup () in the active creation , and not the active created , it is not clear how to get an instance of the Activity that can be modified earlier by calling onCreate (). The problem is that you cannot call things like requestWindowFeature () after calling setContentView () ( example ), which is a problem since it is usually called in onCreate ().

Again, if there is an easy / better way to do this, I really appreciate the feedback. We hope this helps people who want to use this approach.

+11
Apr 29 '13 at 21:58
source share

I spent an incredible amount of time playing with this. The accepted answer works on the nexus 7 galaxy (Android 4.2), but does not work on the SIII galaxy (Android 4.1) and the Samsung 10.2 galaxy (Android 4.0) with the following exception:

 IllegalStateException: ActionBarView can only be used with android:layout_width="match_parent" (or fill_parent) 

This is caused by code in ActionBarView.onMeasure (int, int) , which checks that the layout has been set to match_parent. The correct solution instead sets the window width through setLayout instead of using setAttributes.

This is a fixed version of showAsPopup () that works on all the devices I tested:

 private void showAsPopup(Activity activity) { //To show activity as dialog and dim the background, you need to declare android:theme="@style/PopupTheme" on for the chosen activity on the manifest activity.requestWindowFeature(Window.FEATURE_ACTION_BAR); activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND, WindowManager.LayoutParams.FLAG_DIM_BEHIND); LayoutParams params = activity.getWindow().getAttributes(); params.alpha = 1.0f; params.dimAmount = 0f; activity.getWindow().setAttributes((android.view.WindowManager.LayoutParams) params); // This sets the window size, while working around the IllegalStateException thrown by ActionBarView activity.getWindow().setLayout(850,850); } 

For completeness, here's the PopupTheme again:

 <style name="PopupTheme" parent="android:Theme.Holo.Light.Dialog"> <item name="android:windowIsFloating">false</item> <item name="android:windowContentOverlay">@null</item> <item name="android:windowSoftInputMode">stateAlwaysHidden</item> <item name="android:windowActionModeOverlay">true</item> <item name="android:windowIsTranslucent">true</item> </style> 
+4
Feb 11 '13 at 10:40
source share

Like Veeti, you can try to implement Activity using the Dialog theme. In the Android manifest:

 <activity android:name=".YourActivity" android:theme="@android:style/Theme.Dialog </activity> 

Hope this helps.

+1
Jul 11 '12 at 3:27
source share

I'm probably not so good at it, but I would use activity, add action bar items, and then:

<activity android:name=".YourActivity" android:theme="@android:style/Theme.Holo.Light.Dialog" />

Hope this helps!

+1
Jul 11 '12 at 4:01
source share

I know that this is not a real answer, but the solutions above seem so inelegant that you might think that it would be easier to avoid the action bar altogether, for example, inherit a dialogue topic from something using .noActionBar, and then create a top view of your panel , which mimics the action bar, at least in such a way that all this remains in xlm.

0
Oct 20 '14 at 1:18
source share

I found a good way to do this using setCustomTitle in the alertDialog builder for DialogFragment (also possible on the alert itself).

 public Dialog onCreateDialog(final Bundle savedInstanceState) { final AlertDialog.Builder builder = new AlertDialog.Builder(activity,...); final Toolbar toolbar = (Toolbar) inflater.inflate(R.layout.dialog_app_filter_toolbar, null, false); // <= prepare toolbar and dialog here return builder.create(); } 

And the result (from my application ):

enter image description here

enter image description here

If you do not want to use AlertDialog, you can simply put the toolbar in the layout of the dialog box and use it.

0
Aug 12 '16 at 12:04 on
source share



All Articles