Search code examples
androidandroid-fragmentsandroid-dialogfragment

Android PopupWindow vs. DialogFragment


I'm attempting to make a popup window that inflates on a button click in my application shows a listview populated by an adapter. Currently, I'm using a DialogFragment and it works alright. But I'd like to be able to adjust the size and ideally the position of the dialog.

After doing some research, PopupWindow allows you to adjust the size and position of the box, but it doesn't play nice with instantiating a fragment within its view. DialogFragment works nicely for using a fragment and listview, but to my knowledge you can't control the size and position of it.

Has anyone had a similar situation or experience in creating a dynamic popup?


Solution

  • To change the DialogFragment's size, I did the following withing the OnCreateView method

    Note: Code in C# for MonoDroid; eaisly translated to Java for raw Android

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        base.OnCreateView(inflater, container, savedInstanceState);
    
        View thisView = inflater.Inflate(Resource.Layout.NewContactDialog, container, false);
    
        thisView.Post(() =>
            {
                ViewGroup.LayoutParams lp = thisView.LayoutParameters;
    
                lp.Width = 500;
    
                thisView.LayoutParameters = lp;
    
                thisView.Invalidate();
            }
    
        );
    
        return thisView;
    }
    

    This code is from experimentation. What I settled on was just using a centered DialogFragment (the default behavior) and undimming the background. You can override the style for the DF, and either base it on the existing theme

    <style name="MyDialogFragment" parent="android:Theme.Holo.Dialog">
      <item name="android:windowIsFloating">true</item>
    </style>
    

    Or completely style it from the ground up

    <style name="MyDialogFragment">
      ... (set all items, check docs)
    </style>
    

    For starts try using the MyDialogFragment style above and set windowIsFloating to false and see the effects (just set your theme to this with the SetStyle member).

    Some good links for styles and themes you'll need to do your own styling and overriding the defaults:

    http://www.therealjoshua.com/2012/01/styling-android-with-defaults/

    http://developer.android.com/guide/topics/ui/themes.html/

    https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/res/res/values/styles.xml/

    https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/res/res/values/themes.xml/

    I'll be honest, working with DialogFragments and PopupWindows is a pain, but they are specialized views that solve some real GUI issues, so don't give up. If you need any more info, just ask in the comments below (I realize this is not an answer, I'm just trying to help, b/c I'm working on this stuff right now).