Duplicate webview, I want to download the same in each

How to display two web screens in split screen mode, which works the same way, I want to duplicate the web view, 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 tried this, but I want to duplicate webview. and it shows another, and I need to upload the url again to the second one.

I do not want to load agin in the second, I just want to be a mirror.

+4
source share
4 answers

Try it,

  • 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 a class ObservableWebViewfor the handle event fromoriginalWebview
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.javawith
    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;
        }
    }
}
+6
source

. , - .it -. , ? , :) GlbMP

0

, , .

 <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" />

Java

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

** ... **

-

<?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>

= . , , ,

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

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);
    }



}
0

Source: https://habr.com/ru/post/1648058/


All Articles