4

My question is similar to Full Screen DialogFragment in Android, but that question doesn't quite solve my problem.

I have a DialogFragment that I would like to set maximum dimensions for, and automatically resize down if the DialogFragment extends past the bounds of the Display. This seems like something that should be baked into the OS, but the only solution I've been able to come up with is polling for the display dimensions and manually resizing if the dimensions extend past the bounds of the display.

In the DialogFragment's onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState):

    // Display the first screen
    m_rootView = inflater.inflate(R.layout.fragment_popup, container, false);

    // Readjust the size of the dialog fragment if it is too large for the display
    Globals.AdjustSizeOfDialogFragment(getActivity(), container);

Resizing code in Globals.java:

// Adjusts the size of a dialog fragment's popup view to ensure that it fits within the display of the current device
public static void AdjustSizeOfDialogFragment(final Activity parentActivity, final View popupRoot)
{
    // If all of our function parameters are valid
    if ((parentActivity != null) && (popupRoot != null))
    {
        // If the dialog's root view has already rendered (has defined layout parameters)
        if (popupRoot.getLayoutParams() != null)
        {
            // Resize the dialog now
            ResizeDialogFragment(parentActivity, popupRoot);
        }
        // Else the view has not finished rendering yet
        else
        {
            // Assign a listener to notify us when it has finished rendering
            popupRoot.addOnLayoutChangeListener(new OnLayoutChangeListener()
            {
                public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom)
                {
                    // Remove the layout listener
                    v.removeOnLayoutChangeListener(this);

                    // Now we can render the dialog
                    ResizeDialogFragment(parentActivity, popupRoot);
                }
            });
        }
    }
}

// Does the actual logic/math behind the AdjustSizeOfDialogFragment function
private static void ResizeDialogFragment(Activity parentActivity, View popupRoot)
{
    // Retrieves information about the display of the current device
    DisplayMetrics              deviceDisplayMetrics    = new DisplayMetrics();
    // Layout parameters that need to be applied to the popup's root view
    LinearLayout.LayoutParams   llParams                = null;
    FrameLayout.LayoutParams    flParams                = null;

    // If all of our function parameters are valid
    if ((parentActivity != null) && (popupRoot != null) && (popupRoot.getLayoutParams() != null))
    {
        // Retrieve the layout parameters from the popup's root view and also get information on the current device's display
        if (popupRoot.getLayoutParams() instanceof LinearLayout.LayoutParams)
        {
            llParams = (LinearLayout.LayoutParams) popupRoot.getLayoutParams();
        }
        else if (popupRoot.getLayoutParams() instanceof FrameLayout.LayoutParams)
        {
            flParams = (FrameLayout.LayoutParams) popupRoot.getLayoutParams();
        }
        parentActivity.getWindowManager().getDefaultDisplay().getMetrics(deviceDisplayMetrics);

        // If either the height or width of the popup view is greater then the height or width
        // of the current display, clip them to approximately 90% of the display's width/height.
        if (llParams != null)
        {
            if (llParams.width > deviceDisplayMetrics.widthPixels)
            {
                llParams.width = (int) (deviceDisplayMetrics.widthPixels * 0.9);
            }
            if (llParams.height > deviceDisplayMetrics.heightPixels)
            {
                llParams.height = (int) (deviceDisplayMetrics.heightPixels * 0.9);
            }
        }
        else if (flParams != null)
        {
            if (flParams.width > deviceDisplayMetrics.widthPixels)
            {
                flParams.width = (int) (deviceDisplayMetrics.widthPixels * 0.9);
            }
            if (flParams.height > deviceDisplayMetrics.heightPixels)
            {
                flParams.height = (int) (deviceDisplayMetrics.heightPixels * 0.9);
            }
        }
    }
}

The question is: What is the real Android way to do this? Is this my only option? This seems like something that should be built into the OS, but nobody I've asked has been able to find it.

Community
  • 1
  • 1
paulrehkugler
  • 3,241
  • 24
  • 45

1 Answers1

0

Maybe you have this issue because windows is adjusting on display your DialogFragment, so if set the windowSoftInputMode property to adjustNothing in the AndroidManifest.xml of the displayed activity, could solve your problem.

<activity
    ...
    android:windowSoftInputMode="adjustNothing">
...

FYI: https://developer.android.com/training/keyboard-input/visibility.html#ShowOnStart

Daniel De León
  • 13,196
  • 5
  • 87
  • 72