[JAVA] Automatically adjust the height of the WebView dialog with the size of the content

Thing you want to do

・ Display content on the web in a dialog -Dialog width is 90% of screen width -Dialog height can be changed according to the content size

do

  1. Fixed dialog width
  2. Calculate and change the height of the dialog when the WebView finishes loading

Source

WebViewDialogFragment.java



public class WebViewDialogFragment extends DialogFragment {

	//Omit irrelevant things

	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        binding = DialogWebViewBinding.inflate(inflater, container, false);

		String url = "https://hogehoge";

		binding.webview.setWebViewClient(new WebViewClient(){
			@Override
			public void onPageFinished(WebView view , String url){
				//Change dialog height depending on content size
				Dialog dialog = getDialog();
				WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();

				//dp → px conversion
				DisplayMetrics metrics = getResources().getDisplayMetrics();
				lp.height = (int)(view.getContentHeight() * metrics.scaledDensity);

				dialog.getWindow().setAttributes(lp);
			}
		});
		binding.webview.loadUrl(url);

        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        //Only the width is fixed at the start
        Dialog dialog = getDialog();
        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();

        DisplayMetrics metrics = getResources().getDisplayMetrics();
		float scale = 0.9f;
        lp.width = (int)(metrics.widthPixels * scale);
		//Set the height to 0 and hide the dialog once
        lp.height = 0;

        dialog.getWindow().setAttributes(lp);
    }
}

point

About resizing the dialog

Even if you use dialog.getWindow (). SetAttributes (lp); with ʻonCreateDialog ʻonCreateView, it doesn't work. Must be addressed by overriding ʻonActivityCreated`.

Reference: Y.A.M's miscellaneous notes: In Android DialogFragment, specify the size of Dialog with onActivityCreated

About the height of the content

You can get the height of the content with WebView.getContentHeight (), but the unit is dp. Multiply by DisplayMetrics.scaledDensity to convert to px and then set.

Recommended Posts

Automatically adjust the height of the WebView dialog with the size of the content
JavaFX --Match the size of ImageView with other nodes
Automatically set the width and height of the UI Label
[Android] Dynamically set the height of ListView with copy (for beginners)
Automatically scroll the background with libGDX
[Android] Automatically adjust textScaleX of TextView
Check the contents of params with pry
[Swift] Get the height of Safe Area
Easily measure the size of Java Objects
About the treatment of BigDecimal (with reflection)
Format the contents of LocalDate with DateTimeFormatter