Show dialog from fragment?

I must cautiously doubt the previously accepted answer that using a DialogFragment is the best option. The intended (primary) purpose of the DialogFragment seems to be to display fragments that are dialogs themselves, not to display fragments that have dialogs to display. I believe that using the fragment’s activity to mediate between the dialog and … Read more

Fragment must be a public static class to be properly recreated from instance state

The error is not especially weird. If you were not getting this error before, that was weird. Android destroys and recreates fragments as part of a configuration change (e.g., screen rotation) and as part of rebuilding a task if needed (e.g., user switches to another app, your app’s process is terminated while it is in … Read more

Fragment inner class should be static

Non static inner classes do hold a reference to their parent classes. The problem with making a Fragment inner class non-static is that you always hold a reference to the Activity. The GarbageCollector cannot collect your Activity. So you can ‘leak’ the Activity if for example the orientation changes. Because the Fragment might still live … Read more

How to create a DialogFragment without title?

Just add this line of code in your HelpDialog.onCreateView(…) getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); This way you’re explicitly asking to get a window without title 🙂 EDIT As @DataGraham and @Blundell pointed out on the comments below, it’s safer to add the request for a title-less window in the onCreateDialog() method instead of onCreateView(). This way you can prevent … Read more

Position of DialogFragment in Android

Try something like this: @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { getDialog().getWindow().setGravity(Gravity.CENTER_HORIZONTAL | Gravity.TOP); WindowManager.LayoutParams p = getDialog().getWindow().getAttributes(); p.width = ViewGroup.LayoutParams.MATCH_PARENT; p.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE; p.x = 200; … getDialog().getWindow().setAttributes(p); … or other methods for getDialog().getWindow(). be sure to set the position after calling set-content.

How to change the background color around a DialogFragment?

I had to set android:windowIsFloating to false and android:windowBackground to my custom color in the dialog style: styles.xml <resources xmlns:android=”http://schemas.android.com/apk/res/android”> <style name=”MyDialog” parent=”@android:style/Theme.Dialog”> <item name=”android:windowFrame”>@null</item> <item name=”android:windowBackground”>@color/orange_transparent</item> <item name=”android:windowIsFloating”>false</item> <item name=”android:windowContentOverlay”>@null</item> <item name=”android:windowTitleStyle”>@null</item> <item name=”android:colorBackgroundCacheHint”>@null</item> <item name=”android:windowAnimationStyle”>@android:style/Animation.Dialog</item> <item name=”android:windowSoftInputMode”>stateUnspecified|adjustPan</item> <item name=”android:gravity”>center</item> </style> </resources> MyDialogFragment public class MyDialogFragment extends DialogFragment { @Override public void onCreate(Bundle savedInstanceState) … Read more

DialogFragment and onDismiss

Make your Activity implement OnDismissListener public final class YourActivity extends Activity implements DialogInterface.OnDismissListener { @Override public void onDismiss(final DialogInterface dialog) { //Fragment dialog had been dismissed } } DialogFragment already implements OnDismissListener, just override the method and call the Activity. public final class DialogFragmentImage extends DialogFragment { ///blah blah @Override public void onDismiss(final DialogInterface dialog) … Read more

DialogFragment with clear background (not dimmed)

What works for me is to adjust the WinowManager.LayoutParams in onStart() of the DialogFragment: @Override public void onStart() { super.onStart(); Window window = getDialog().getWindow(); WindowManager.LayoutParams windowParams = window.getAttributes(); windowParams.dimAmount = 0.90f; windowParams.flags |= WindowManager.LayoutParams.FLAG_DIM_BEHIND; window.setAttributes(windowParams); }

AlertDialog with custom view: Resize to wrap the view’s content

I have a working solution that to be honest, I think digs way too deep to obtain such a simple result. But here it is: What exactly is happening: By opening the Dialog layout with the Hierarchy Viewer, I was able to examine the entire layout of the AlertDialog and what exactly what was going … Read more