Pages

Thursday 26 October 2017

Android web view set wrap_content height

Use following code to set android web view set wrap_content height


  • webview's parent should have match parent height
  • and set webview as wrap_content height
---Layout file ---
<ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:padding="10dp">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_margin="@dimen/twenty"
                android:src="@drawable/img_sc2_logo" />

            <WebView
                android:id="@+id/wvInfo"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
            </WebView>
       </LinearLayout>
</ScrollView>

---java file code---
Following is IMP part

binding.wvInfo.loadUrl(ApiRequestUtil.PAGE_APP_WELCOME);
        binding.wvInfo.getSettings().setLoadWithOverviewMode(true);
        binding.wvInfo.getSettings().setUseWideViewPort(true);
        binding.wvInfo.getSettings().setJavaScriptEnabled(true);
        binding.wvInfo.getSettings().setBuiltInZoomControls(false);

        ViewTreeObserver viewTreeObserver  = binding.wvInfo.getViewTreeObserver();

        viewTreeObserver.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                int height = binding.wvInfo.getMeasuredHeight();
                if( height != 0 ){
//                    Toast.makeText(getActivity(), "height:"+height,Toast.LENGTH_SHORT).show();
                    binding.wvInfo.getViewTreeObserver().removeOnPreDrawListener(this);
                }
                return false;
            }
        });

No comments :

Post a Comment