Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebView not working, launching the url externally in browser. Displaying webpage inside app

I made a home or main activity which has some icons and one of them is facebook, and by clicking it a facebook.xml is launched via intent

The code for that xml page is set as below:

<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="10"
android:id="@+id/webView" >
</WebView>

And I want to load the url: "http://www.j.mp/tkf4mApp"

The Java file which is linked to this xml file is as below:

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;

public class Facebook extends Activity {

private WebView webView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.facebookpage);

    webView=(WebView)findViewById(R.id.webView);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.loadUrl("http://www.j.mp/tkf4mApp");


}
}

But instead of showing webpage inside the app in facebook.xml it launches browser externally, but i want it to be shown inside the app.

like image 727
Manan Gupta Avatar asked Feb 06 '14 11:02

Manan Gupta


People also ask

Why is my WebView not working?

You might often face issues in updating the chrome and Android System Webview. To fix this problem, you can reboot your device, check your internet connection, stop auto-updating all apps, clear Google Playstore cache, and storage, leave the beta testing program, and manually update Android WebView app from Playstore.

What can I use instead of WebView?

Alternatives to WebView If you want to send users to a mobile site, build a progressive web app (PWA). If you want to display third-party web content, send an intent to installed web browsers. If you want to avoid leaving your app to open the browser, or if you want to customize the browser's UI, use Custom Tabs.

How do I open a PDF link in WebView?

Opening a PDF file in Android using WebView All you need to do is just put WebView in your layout and load the desired URL by using the webView. loadUrl() function. Now, run the application on your mobile phone and the PDF will be displayed on the screen.

What is the difference between WebView and browser?

Android System WebView lets applications display browser windows in an app instead of transporting the user to another browser. Android developers use WebView when they want to display webpages in a Google app or other application.


1 Answers

As per this answer: How to load external webpage inside WebView you need to set a WebViewClient before you call loadUrl:

webView.setWebViewClient(new WebViewClient());

The reason you're being sent to the browser is that if no WebViewClient is set then the default action for navigations is to forward them to the browser.

like image 193
marcin.kosiba Avatar answered Sep 24 '22 15:09

marcin.kosiba