Dynamically decrease the height of dialog with WebView as HTML content changes, … or fix height

StackOverflow https://stackoverflow.com/questions/6816444

  •  25-10-2019
  •  | 
  •  

سؤال

I have a DialogFragment (displayed as a dialog) whose layout contains a WebView control. The layout is setup to wrap_content. I'm working on a tablet and do want to display as a dialog but do not want to have the height match that of the screen.

The web view displays HTML content which is defined at build time.

The HTML contains links which can hide and show content (hiding/showing 'divs').

As I click on links to expand them then the height of the dialog increases as content is shown.

However, when I collapse entries the height of the dialog does not contract.

My preference would be to fix the height of the dialog after the content has loaded. Failing that to have the height of the dialog decrease as content is hidden.

Thanks in advance, Peter.

هل كانت مفيدة؟

المحلول 2

Although the code needs cleaning up and there may be neater ways of doing it but I put the following in the onPageFinished event for the WebView and it does the job for me of fixing the dialog height after the html content has been loaded.

public void onPageFinished(WebView view, String url) {
    ....
    webView.setOnTouchListener(new View.OnTouchListener() {
      @Override
      public boolean onTouch(View v, MotionEvent event) {
        if (webView.getHeight() < currentHeight) {
          currentHeight = webView.getHeight();
        }
        final LayoutParams params = webView.getLayoutParams();
        params.height = currentHeight;
        webView.setLayoutParams(params);
        return false;
      }
    });
}

نصائح أخرى

Simple Try

Try to invalidate(); in the onClickListener

yourWebView.setOnTouchListener(new OnTouchListener() { 
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub
        // TRY to invalidate();

        // Try to setVisibility(View.GONE); then directly (View.VISIBLE)      
        return false;
    } 
});

Harder Stuff

  • Navigate through the source code of the WebView
  • Follow the execution of the clicks (I beleive you should start from loadUrl())
  • Try to reach to the point which is causing the WebView to become larger when a div is shown

There, you might find a way to imitate this behavior when the div is gone

Although this might be possible, I believe having a dialog that changes size as the user interacts with it is not a very good user interface (I don't know your application so perhaps I might be misjudging). Perhaps you should consider a regular Activity instead of displaying your Webview in a dialog box.

I come from an iOS background. You need to use something similar to a popover control in iPad.

Alternatively you can use LayoutInflator to adjust the size of the view. This blog post (Layou resources in Android) seems to describe exactly how to do this. Looks like people have had good results with this.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top