Home » Android » webview inside fragment android opening outside of the app in a browser

webview inside fragment android opening outside of the app in a browser

Posted by: admin June 15, 2020 Leave a comment

Questions:

I got a webview in my app and it opens outside of it, while I want it to open inside… I’ve read all examples on stackoverflow and still don’t know why it’s like this

NewsFragment.java

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;

public class NewsFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.news, container, false);

        WebView webView = (WebView)rootView.findViewById(R.id.webView);

        webView.setInitialScale(1);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setUseWideViewPort(true);
        webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
        webView.setScrollbarFadingEnabled(false);

        webView.loadUrl("https://www.google.com");

        return rootView;
    }
}

XML file:

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

    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>
How to&Answers:

Try this code, you have to override the “shouldOverrideUrlLoading()”

String url = "http://appprice.appday.de";

WebView wv = new WebView(context); 
// or 
// WebView wv = (WebView)findViewById(R.id.my_webview);

wv.loadUrl(url);
wv.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
});

Answer:

You need to try this

webview.setWebViewClient(new WebViewClient());

Answer:

Following code work for me:

webView = finder.find(R.id.webView_for_info);

webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);

webView.setWebViewClient(new WebViewClient());
//webView.setWebChromeClient(new WebChromeClient());

webView.loadUrl("file:///android_asset/info.html");

Answer:

try this:

public class WebViewGameActivity extends BaseActivity {
    private static final String TAG = WebViewGameActivity.class.getSimpleName();


    private WebView myWebView;

       @SuppressLint("SetJavaScriptEnabled")
    @Override        
       protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.layout_webview);


          myWebView = (WebView) findViewById(R.id.webview);

          // Configure related browser settings
          WebSettings webSettings = myWebView.getSettings();
          webSettings.setJavaScriptEnabled(true);
          webSettings.setLoadsImagesAutomatically(true);

          myWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

          // Configure the client to use when opening URLs
          myWebView.setWebViewClient(new MyBrowser());

          // Load the initial URL
          //myWebView.loadUrl("http://www.example.com");

          String path = Uri.parse("file:///android_asset/index.html").toString();
          myWebView.loadUrl(path);


       }//end onCreate



       // Manages the behavior when URLs are loaded
       private class MyBrowser extends WebViewClient {
          @Override
          public boolean shouldOverrideUrlLoading(WebView view, String url) {
             view.loadUrl(url);
             return true;
          }
       }




}//end WebViewGameActivity

Answer:

try this …as when u click on web view in the fragment it won’t ask you to open in a new browser application!!

    String url = "https://www.google.co.in/";
    WebView view = (WebView) rootView.findViewById(R.id.webView1);


    view.getSettings().setJavaScriptEnabled(true);
    view.getSettings().setLoadWithOverviewMode(true);
    view.getSettings().setUseWideViewPort(true);
    view.getSettings().setBuiltInZoomControls(true);
    view.getSettings().setPluginState(WebSettings.PluginState.ON);
    view.setWebViewClient(new WebViewClient());

    view.loadUrl(url);

    return rootView ;
}