Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActionBar in a DialogFragment

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.

enter image description here

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 image 514
Paul Blessing Avatar asked Jul 11 '12 03:07

Paul Blessing


People also ask

What is an ActionBar?

An on-screen toolbar displaying icons that are clicked or tapped to perform various functions. For example, the menu bar at the top of an Android app is called an action bar.

Where is ActionBar in Android?

Android ActionBar is a menu bar that runs across the top of the activity screen in android. Android ActionBar can contain menu items which become visible when the user clicks the “menu” button.

How do you pass arguments to DialogFragment?

you can set your args. class IntervModifFragment : DialogFragment(), ModContract. View { companion object { fun newInstance( plom:String,type:String,position: Int):IntervModifFragment { val fragment =IntervModifFragment() val args = Bundle() args. putString( "1",plom) args.


2 Answers

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

<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); } 
like image 96
StrikeForceZero Avatar answered Sep 17 '22 13:09

StrikeForceZero


If you are using ActionBarSherlock, declare the theme as 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 a SherlockActivity with PopupTheme according to Luke Sleeman'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

like image 36
nagoya0 Avatar answered Sep 18 '22 13:09

nagoya0