Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 827
user3203969 Avatar asked Jan 16 '14 19:01

user3203969


People also ask

Which browser is used in Android WebView?

Google combined WebView with Google Chrome for versions 7.0, 8.0 and 9.0. However, with Android 10.0, it went back to having it as a separate component. Users can update WebView in Android 10 through Google Play Store.

Is Android WebView deprecated?

The Android system webview custom cache file has been deprecated and removed in Android 13. New apps and any app updates will now use the operating system default cache location.

How does WebView work in Android?

WebView is a view that display web pages inside your application. You can also specify HTML string and can show it inside your application using WebView. WebView makes turns your application to a web application. This method specifies the WebView has a back history item.


2 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;
    }
});
like image 181
A.S. Avatar answered Oct 15 '22 13:10

A.S.


You need to try this

webview.setWebViewClient(new WebViewClient());
like image 26
user4918678 Avatar answered Oct 15 '22 12:10

user4918678