5

How to display two webview in split screen which works similar, I want to duplicate a webview, and i want to scroll both at the same time. Thanks

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="2">

    <WebView
        android:id="@+id/webView"
        android:layout_width="150dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        >

    </WebView>

    <WebView
        android:id="@+id/webView2"
        android:layout_width="150dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"></WebView>
</LinearLayout>

I have tried this, but i want to duplicate webview. and this is showing another and i have to load url again for second.

I dont want to load agin in second one i just want to be mirror.

Shahzain ali
  • 1,679
  • 1
  • 20
  • 33

4 Answers4

6

Try this,

  1. create layout file content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="9">

    <com.pramod.webviewdemo.ObservableWebView
        android:id="@+id/webViewOriginal"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="4"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Second View"/>


    <WebView
        android:id="@+id/webViewMirror"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="5"/>

</LinearLayout>
  1. Create Class ObservableWebView for handle event from originalWebview
public class ObservableWebView extends WebView
{
    private OnScrollChangedCallback mOnScrollChangedCallback;

    public ObservableWebView(final Context context)
    {
        super(context);
    }

    public ObservableWebView(final Context context, final AttributeSet attrs)
    {
        super(context, attrs);
    }

    public ObservableWebView(final Context context, final AttributeSet attrs, final int defStyle)
    {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onScrollChanged(final int l, final int t, final int oldl, final int oldt)
    {
        super.onScrollChanged(l, t, oldl, oldt);
        if(mOnScrollChangedCallback != null) mOnScrollChangedCallback.onScroll(l, t);
    }

    public OnScrollChangedCallback getOnScrollChangedCallback()
    {
        return mOnScrollChangedCallback;
    }

    public void setOnScrollChangedCallback(final OnScrollChangedCallback onScrollChangedCallback)
    {
        mOnScrollChangedCallback = onScrollChangedCallback;
    }

    /**
     * Impliment in the activity/fragment/view that you want to listen to the webview
     */
    public static interface OnScrollChangedCallback
    {
        public void onScroll(int l, int t);
    }
}
  1. Update MainActivity.java with,
    public class MainActivity extends AppCompatActivity {

    private WebView  webViewMirror;
    private ObservableWebView webViewOriginal;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_main);

        // initialize view
        webViewOriginal = (ObservableWebView)findViewById(R.id.webViewOriginal);
        webViewMirror = (WebView)findViewById(R.id.webViewMirror);

        //set web settings for original
        webViewOriginal.getSettings().setLoadsImagesAutomatically(true);
        webViewOriginal.getSettings().setJavaScriptEnabled(true);
        webViewOriginal.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

        //set web settings for duplicate / mirroe
        webViewOriginal.getSettings().setLoadsImagesAutomatically(true);
        webViewOriginal.getSettings().setJavaScriptEnabled(true);
        webViewOriginal.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

        // set clients for webview
        webViewOriginal.setWebViewClient(new WebClientForOriginal());
        webViewMirror.setWebViewClient(new WebClientForMirror());

        // add listener to listen scroll For original webview
        webViewOriginal.setOnScrollChangedCallback(new ObservableWebView.OnScrollChangedCallback() {
            public void onScroll(int x, int y) {
                Log.d("MainActivity", "We Scrolled etc..." + " X " + x + " Y " + y);
                webViewMirror.scrollTo(x,y);
            }
        });

        //disabled touch event event.   
        webViewMirror.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                return true;
            }
        });

        // load urls
        webViewOriginal.loadUrl("http://www.google.com");
        webViewMirror.loadUrl("http://www.google.com");

    }

    /**
     * Set web client for original web view
     */
    private class WebClientForOriginal extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            webViewMirror.loadUrl(url);
            return true;
        }
    }

    /**
     * Set web client for mirror web view
     */
    private class WebClientForMirror extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }
}
Pramod Waghmare
  • 1,273
  • 13
  • 21
0

Same use case was in my project. I have achieved it through putting web view in List view.it will scroll entire screen irrespective of webview. Try once and let me know whether it is working for you or not? Hope it will work :) GlbMP

0

i have used this type of view for horizontal scroll and inside it there is imageview and textview hope this will work for you

 <LinearLayout
    android:id="@+id/rel_c_content"
  android:layout_width="wrap_content"
 android:layout_height="wrap_content"
  android:gravity="center"
   android:orientation="horizontal"
  android:padding="5dp" />

and here is java code

int i = 0;
    do {
        if (i >= arrayofRelated.size()) {
            return;
        }

        View view = getLayoutInflater().inflate(R.layout.related_content, null);
        final ImageView imageView = (ImageView) view.findViewById(R.id.img_rela);
        final TextView txt_relmname = (TextView) view.findViewById(R.id.text_relmname);
        imageView.setId(i);

        linearContent.addView(view);

        objChildBean = arrayofRelated.get(i);
        imageLoader.DisplayImage(Constant.SERVER_IMAGE_THUMB + objChildBean.getRelatedImage().toString(), imageView);
        txt_relmname.setText(objChildBean.getRelatedTitle().toString());

        <Here you can load url in webview>

        i++;
    } while (true);
0

** I am trying this...**

take Layout set height to webview to as you wish

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
  >
    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"

 android:background="@color/colorAccent"
    android:weightSum="1">


    <WebView
        android:id="@+id/webView"
        android:layout_width="0dp"
        android:layout_height="1000dp"
     android:layout_weight="0.50"
        >

    </WebView>


    <WebView
        android:id="@+id/webView2"
        android:layout_width="0dp"
        android:layout_height="1000dp"
        android:layout_weight="0.50">

    </WebView>
    </LinearLayout>


</ScrollView>

=Try this FOr match parent I am trying to Fix height than it's work try more to match parent by this

 android:fillViewport="true"
     android:scrollbars="vertical"

Code

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;


public class MainActivity extends AppCompatActivity {
    private WebView webView1,webView2;
    private static final String TAG = "TAGGBOX";
    private ProgressDialog progressBar;
    // check secret prefix
    String msg="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.demo);
    SetypUI();

    WebSettings settings = webView1.getSettings();
    WebSettings settings1 = webView2.getSettings();
    settings.setJavaScriptEnabled(true);
    settings1.setJavaScriptEnabled(true);
    //ws.setJavaScriptEnabled(true);
    settings.setJavaScriptCanOpenWindowsAutomatically(true);
    settings1.setJavaScriptCanOpenWindowsAutomatically(true);





//        /wcc = new MyWebChromeClient();


    webView1.setScrollContainer(false);
    webView2.setScrollContainer(false);
    webView1.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
        return (event.getAction() == MotionEvent.ACTION_MOVE);
        }
    });

    webView2.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
        return (event.getAction() == MotionEvent.ACTION_MOVE);
        }
    });


    webView1.setVerticalScrollBarEnabled(false);
    webView1.setHorizontalScrollBarEnabled(false);

    webView2.setVerticalScrollBarEnabled(false);
    webView2.setHorizontalScrollBarEnabled(false);


    webView1.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
    webView2.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);


    webView1.setWebViewClient(new WebViewClient() {

        public boolean shouldOverrideUrlLoading(WebView view, String url) {
        Log.i(TAG, "Processing webview url click..."+url);
        view.loadUrl(url);

        return true;
        }


        public void onPageFinished(WebView view, String url) {
        Log.e(TAG, "Finished loading URL: " + url);


        //webview.reload();
        }


        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
          /*  Log.e(TAG, "Error: " + description);
        Toast.makeText(MainActivity.this, "Please Check the Internet Connection", Toast.LENGTH_SHORT).show();
        alertDialog.setTitle("Error");
        alertDialog.setMessage("Please Check the Internet Connection");
        alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            return;
            }
        });
        alertDialog.show();*/


        }
    });

    webView1.loadUrl("http://stackoverflow.com/");


    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.


    webView2.setWebViewClient(new WebViewClient() {

        public boolean shouldOverrideUrlLoading(WebView view, String url) {
        Log.i(TAG, "Processing webview url click..."+url);
        view.loadUrl(url);

        return true;
        }


        public void onPageFinished(WebView view, String url) {
        Log.e(TAG, "Finished loading URL: " + url);


        //webview.reload();
        }


        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
          /*  Log.e(TAG, "Error: " + description);
        Toast.makeText(MainActivity.this, "Please Check the Internet Connection", Toast.LENGTH_SHORT).show();
        alertDialog.setTitle("Error");
        alertDialog.setMessage("Please Check the Internet Connection");
        alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            return;
            }
        });
        alertDialog.show();*/


        }
    });

    webView2.loadUrl("http://stackoverflow.com/");


    }



    private void SetypUI() {
    webView1 = (WebView) findViewById(R.id.webView);
    webView2 = (WebView) findViewById(R.id.webView2);
    }



}
Arjun saini
  • 4,223
  • 3
  • 23
  • 51