First make custom webview class and use it on xml like this,
<com.app.ui.views.CustomWebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</com.app.ui.views.CustomWebView>
Make sure, the height should not be fixed, it should be "wrap_content"
Now make custom webview class like this,
public class CustomWebView extends WebView {
private ProgressBar progressBar;
private Context context;
private LinearLayout loaderLayout;
public CustomWebView(Context context) {
super(context);
this.context = context;
initProgressBar();
loadUrlWithData();
}
public CustomWebView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
initProgressBar();
}
public CustomWebView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.context = context;
initProgressBar();
}
private void initProgressBar() {
progressBar = new ProgressBar(context);
progressBar.setIndeterminate(false);
}
private void setLoading(boolean isLoading) {
if (isLoading) {
progressBar.setVisibility(View.VISIBLE);
loaderLayout = new LinearLayout(context);
loaderLayout.setGravity(Gravity.CENTER);
loaderLayout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams parentLayoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
loaderLayout.setLayoutParams(parentLayoutParams);
LinearLayout.LayoutParams progressBarLayoutParams = new
LinearLayout.LayoutParams(75, 75);
progressBar.setLayoutParams(progressBarLayoutParams);
if (progressBar.getParent() != null) {
((ViewGroup) progressBar.getParent()).removeView(progressBar);
}
loaderLayout.addView(progressBar);
// add this loaderLayout to your parent view in which your webview is added
else {
progressBar.setVisibility(View.GONE);
}
}
private void loadUrlWithData(){
setLoading(true);
//Load your HTML data with url here
}
//When the content will be loaded, the webview will change it's height
@Override
protected void onSizeChanged(int w, int h, int ow, int oh) {
super.onSizeChanged(w, h, ow, oh);
if (h > 400) {
setLoading(false);
}
}
}
WebViewClient
, using this techique you don't have to take look on how much of data you have grabbed from server. – Funds