user3203969 user3203969 - 3 months ago 26
Android Question

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

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>

Answer

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;
    }
});
Comments