0

Am new in Android programming I want to display TWO webviews the second aligned below the first VERTICALLY. I wish both of them display at the same time. I have tried through different post on the topic but the sample codes seem to display in different activity or window.

Here is what I have developed yet I need your idea on how I can go around to tackle this issue

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
>

    <WebView
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/webView2"
        />

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

This is the sample java file connecting the activity_main.xml which contains the two webviews MainActivity.java

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity{
    private WebView webView1, webView2;



     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

         setContentView(R.layout.activity_main);

        webView1 = (WebView) findViewById(R.id.webView);
        webView2 = (WebView) findViewById(R.id.webView2);

        webView1.setWebViewClient(new MyWebViewClient1());
        webView1.getSettings().setJavaScriptEnabled(true);
        webView1.getSettings().setAllowUniversalAccessFromFileURLs(true);
        webView1.getSettings().setDomStorageEnabled(true);
        webView1.getSettings().setSupportMultipleWindows(true);
        webView1.getSettings().setAppCacheEnabled(true);
        webView1.getSettings().setAllowFileAccess(true);
        webView1.getSettings().setLoadsImagesAutomatically(true);

         webView2.setWebViewClient(new MyWebViewClient2());
        webView2.getSettings().setJavaScriptEnabled(true);
        webView2.getSettings().setAllowUniversalAccessFromFileURLs(true);
        webView2.getSettings().setDomStorageEnabled(true);
        webView2.getSettings().setSupportMultipleWindows(true);
        webView2.getSettings().setAppCacheEnabled(true);
        webView2.getSettings().setAllowFileAccess(true);
        webView2.getSettings().setLoadsImagesAutomatically(true);

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


        }


    }

    @Override
    public void onRefresh() {
        initView();
    }

    public class JavaScriptReceiver
    {
        Context mContext;

        /** Instantiate the receiver and set the context */
        JavaScriptReceiver(Context c) {
            mContext = c;
        }

        @JavascriptInterface
        public void showProfileDetails(){
            Intent intent = new Intent(mContext, DetailActivity.class);
            mContext.startActivity(intent);
        }

    }


    private class MyWebViewClient1 extends WebViewClient {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (Uri.parse(url).getHost().equals(url)) {  
                return false;
            }
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(intent);
            return true;
        }


        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);

        }
    }

     private class MyWebViewClient2 extends WebViewClient {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (Uri.parse(url).getHost().equals(url)) {  
                return false;
            }
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(intent);
            return true;
        }


        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);

        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // adds item to action bar
        getMenuInflater().inflate(R.menu.menu_search, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (item.getItemId() == R.id.add_post) {
            Intent intent = new Intent(getApplicationContext(), PostActivity.class);
            startActivity(intent);
            return true;
        }

        if (item.getItemId() == R.id.upload) {
            Intent intent = new Intent(getApplicationContext(), CustomChooserActivity.class);
            startActivity(intent);
            return true;
        }
        if (item.getItemId() == R.id.action_settings) {
            Intent intent = new Intent(getApplicationContext(), ProfileActivity.class);
            startActivity(intent);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
  • use weight. https://stackoverflow.com/questions/2698817/linear-layout-and-weight-in-android – Rohit5k2 Dec 06 '19 at 08:38
  • This answer describes about component alignment. Its fine, and am now comfortable with vertical align. I would like to see any sample code to help me display both of the webviews on bottom of the other. Thanks – Mahadi Matsawily Dec 06 '19 at 08:56
  • that post has many sample codes. If you are looking for the exact code for you, then you are at wrong website. – Rohit5k2 Dec 06 '19 at 09:28
  • Dear @Rohit5k2, I know am at right website but the link you added does not talk about wevbiew layout. Mind you Linear layout is a container of the webview Layout – Mahadi Matsawily Dec 06 '19 at 09:38

1 Answers1

0

In your LinearLayout set orientation to vertical instead of horizontal. Also use weight attribute to properly align the layouts.

Prajwal Waingankar
  • 2,534
  • 2
  • 13
  • 20