lkahtz
lkahtz

Reputation: 4796

Web browser is not showing anything

Anyone can take a look at my code and let me know what I am missing?

MainActivity.java:

public class MainActivity extends AppCompatActivity {
    private WebView mWebView;

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    mWebView.loadUrl("https://yahoo.com");
                    return true;
                case R.id.navigation_dashboard:
                    mWebView.loadUrl("https://google.com");
                    return true;
                case R.id.navigation_notifications:
                    mWebView.loadUrl("https://apple.com");
                    return true;
            }
            return false;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        BottomNavigationView navView = findViewById(R.id.nav_view);
        mWebView = (WebView) findViewById(R.id.webkit);
        navView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        webSettings.setDomStorageEnabled(true);
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                mWebView.loadUrl("https://google.com");
                return true;
            }
        });
    }

}

activity_main.xml (layout file):

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <WebView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/webkit"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="0dp"
        android:layout_marginStart="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/bottom_nav_menu" />

</androidx.constraintlayout.widget.ConstraintLayout>

My code is at: https://github.com/tlkahn/neonx

If you think this is too vague or off-topic, please comment and I will close it.

Upvotes: 0

Views: 238

Answers (4)

Bek
Bek

Reputation: 8471

Replace

mWebView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            mWebView.loadUrl("https://google.com");
            return true;
        }
    });

With

mWebView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {

            return false;
        }
    });

Give the host application a chance to take control when a URL is about to be loaded in the current WebView. If a WebViewClient is not provided, by default WebView will ask Activity Manager to choose the proper handler for the URL. If a WebViewClient is provided, returning true causes the current WebView to abort loading the URL, while returning false causes the WebView to continue loading the URL as usual.

Upvotes: 2

Shivam Sharma
Shivam Sharma

Reputation: 96

mWebView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            mWebView.loadUrl("https://facebook.com");
            return true;
        }
    });

Upvotes: 0

Vasudev Vyas
Vasudev Vyas

Reputation: 742

if you are using android 8+ then you must have to do 2 things.

  1. Add this android:usesCleartextTraffic="true" attribute in manifest application tag. AndroidManifest.xml

  2. you should override this method.

        mWebView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
    
           view.load(request.getUrl().toString()) 
    
            return true;
        }
    });
    

Upvotes: 0

vasmos
vasmos

Reputation: 2586

So far from what I can tell you are supposed to extend the WebViewClient class with a separate class:

   private class MyBrowser extends WebViewClient {
      @Override
      public boolean shouldOverrideUrlLoading(WebView view, String url) {
         view.loadUrl(url);
         return true;
      }
   }

and then set it to the WebViewClient:

mWebView.setWebViewClient(new MyBrowser());

Also try adding settings for loading images automatically, enabling javascript and setting scrollbar:

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    switch (item.getItemId()) {
    mWebView.getSettings().setLoadsImagesAutomatically(true);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        case R.id.navigation_home:
            mWebView.loadUrl("https://yahoo.com");
            return true;
        case R.id.navigation_dashboard:
            mWebView.loadUrl("https://google.com");
            return true;
        case R.id.navigation_notifications:
            mWebView.loadUrl("https://apple.com");
            return true;
    }
    return false;
}

Upvotes: 1

Related Questions