7

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.

Limon Monte
  • 44,025
  • 43
  • 163
  • 189
Manan Gupta
  • 465
  • 1
  • 6
  • 11

4 Answers4

16

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.

Community
  • 1
  • 1
marcin.kosiba
  • 3,071
  • 11
  • 17
  • I tried that but that too is not working,please help if there is some other way. the app canbe accessed by the following url :j.mp/thekfac – Manan Gupta Feb 06 '14 at 11:49
  • @MananGupta you linked your APK. That doesn't make it any simpler to investigate. Also, what do you mean by "not working"? – marcin.kosiba Feb 06 '14 at 13:29
  • Okay, i attached just to lt you see. And not working means it doesn't happen to bring change in the app. App is working same as before, i added that line and recompiled, it is launching the site to external browser. I hope it is clear now. – Manan Gupta Feb 06 '14 at 16:25
  • @MananGupta - try doing this: webView.setWebViewClient(new WebViewClient() { public boolean shouldOverrideUrlLoading(WebView view, String url) { android.util.Log.w("Test", "shouldOverrideUrlLoading " + url); return false; } }); webView.loadUrl("http://www.j.mp/tkf4mApp"); and check that you see the debug message in the log. – marcin.kosiba Feb 06 '14 at 16:27
  • thanks buddy, just doing it, i'll leave a reply ASAP – Manan Gupta Feb 06 '14 at 16:35
  • thanks it works fine man, but like when i click on icon it open that xml which is blank, it is shown until the page is loaded.Is there a way i can show a loading icon or something that gives user a idea that it is loading. Please help me out with this thing. So i can show a loading icon or image until webpage completely appears. – Manan Gupta Feb 06 '14 at 17:51
  • @MananGupta - you could start showing the loading icon in WebViewClient.onPageStarted (or shouldOverrideUrlLoading even) and hide it in WebViewClient.onPageFinished. – marcin.kosiba Feb 06 '14 at 18:11
  • Will you please elaborate more, it will be more than grateful – Manan Gupta Feb 07 '14 at 02:44
  • yes, it worked well....working by calling webView.setWebViewClient(new WebViewClient()); even after calling loadUrl :) – Narendra Singh Oct 30 '15 at 08:58
0
package com.example.webview;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {


   private WebView browser;

   @Override        
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      browser = (WebView)findViewById(R.id.webView1);
      browser.setWebViewClient(new MyBrowser());
   }


   public void open(View view){
      String url = "http://www.j.mp/tkf4mApp";
      browser.getSettings().setLoadsImagesAutomatically(true);
      browser.getSettings().setJavaScriptEnabled(true);
      browser.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
      browser.loadUrl(url);

   }
   private class MyBrowser extends WebViewClient {
      @Override
      public boolean shouldOverrideUrlLoading(WebView view, String url) {
         view.loadUrl(url);
         return true;
      }
   }

   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
   }

}

This Link may Help You... http://www.tutorialspoint.com/android/android_webview_layout.htm

Md. Rahman
  • 1,682
  • 12
  • 15
0

Refer to the following project, this one works

https://code.google.com/p/html5webview/

0

This is late but it might help other developers....

Here is the code that you need to add before loading your URL.

webview.setWebViewClient(new WebViewClient() {
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return false;
        }
    });

As per the documentation, documentational words goes like below

/*@return {@code true} if the host application wants to leave the current WebView
             *         and handle the url itself, otherwise return {@code false}.*/

So it simply says, If you return true from shouldOverrideUrlLoading method, it'll ask the default browser of your device to handle the request of opening the URL and if you return false, then your URL will be loaded through webview only.

Now you can load your URL in webview either after this setWebViewClient call or you can also load your URL inside shouldOverrideUrlLoading method before returning the value.

Abhishek Kumar
  • 2,792
  • 3
  • 17
  • 34