0

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.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
lkahtz
  • 4,706
  • 8
  • 46
  • 72
  • Please try doing `mWebView.loadUrl("https://google.com")` outside `mWebView.setWebViewClient()`. If this works it means at you must manually load the mWebView. – Saswata Jul 29 '19 at 03:44
  • Thanks for getting back so fast! I tried and it sees not working. I have pushed the change to github. – lkahtz Jul 29 '19 at 03:48
  • Please check your network status which may affect, (may refer to https://stackoverflow.com/a/4239019/10941112 for connectivity code). Also I am working on your the code from git. – Saswata Jul 29 '19 at 03:57
  • I just did and traced the program. It seems network is fine: https://github.com/tlkahn/neonx/commit/c52195102ea9044136b0fcbf918c8c3b6e28fa99 – lkahtz Jul 29 '19 at 04:28
  • Answer given by Bek. Also you have another major issue at possibly `com.neonxorg.neonx.MyView` – Saswata Jul 29 '19 at 04:39
  • Thanks! I just accepted it. Can I know more about the issue please? Also if you want to log an issue, please kindly. – lkahtz Jul 29 '19 at 04:46
  • 1
    The error has been reported to your Github repository. – Saswata Jul 29 '19 at 05:11

4 Answers4

2

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.

Bek
  • 7,790
  • 4
  • 18
  • 31
1

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;
}
vasmos
  • 2,472
  • 1
  • 10
  • 21
0

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;
        }
    });
    
Vasudev Vyas
  • 726
  • 1
  • 10
  • 28
  • 1
    Thanks for the answer. I did already have the line: https://github.com/tlkahn/neonx/blob/b1d61245e875ecabf568831eea5d99dcef394fc3/app/src/main/AndroidManifest.xml#L14 – lkahtz Jul 29 '19 at 04:54
  • keep both shouldOverrideUrlLoading() or add targeted api lolipop to this method. just for knowledge. – Vasudev Vyas Jul 29 '19 at 06:01
0
mWebView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            mWebView.loadUrl("https://facebook.com");
            return true;
        }
    });