8

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>
user3203969
  • 83
  • 1
  • 1
  • 3
  • 1
    possible duplicate of [Disable address bar in Android webview](http://stackoverflow.com/questions/4136362/disable-address-bar-in-android-webview) – CommonsWare Jan 16 '14 at 19:30
  • FYI: I do not feel this is a duplicate of the mentioned SO entry, because I had an example just like this and was able to work through my problem using the question and the answer below. This is a good SO entry. I read over the possible duplicate entry and I never would've gotten to my solution from that entry. – raddevus Sep 11 '14 at 21:24

5 Answers5

27

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;
    }
});
A.S.
  • 4,574
  • 3
  • 26
  • 43
  • It works but could you please provide more info about the reason that we should override this method. – MeLean Mar 03 '16 at 09:11
6

You need to try this

webview.setWebViewClient(new WebViewClient());
Neha Shukla
  • 3,572
  • 5
  • 38
  • 69
user4918678
  • 61
  • 1
  • 1
1

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");
Siddharth
  • 9,349
  • 16
  • 86
  • 148
Sherali Turdiyev
  • 1,745
  • 16
  • 29
0

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
Thiago
  • 12,778
  • 14
  • 93
  • 110
0

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 ;
}
DanielBarbarian
  • 5,093
  • 12
  • 35
  • 44
Dhruv Gupta
  • 61
  • 2
  • 8