19

This is my first question here. I know that this question has been asked before, but I didn't find an answer/solution that really explains the answer for a totally newbie like me.

I am creating an app with a linear layout that has a lot of buttons, each button should drive the user to a different web page. The buttons works well and every buttons goes to its specific web page, but in the default browser, not within the app.

This is my webview.xml file:

<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />

This is the WebViewActivity.java file:

public class WebViewActivity extends Activity {

private WebView webView;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.webview);

    webView = (WebView) findViewById(R.id.webView1);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.loadUrl(
            "http://egy-tech-droid.blogspot.com.eg/search/label/%D8%AA%D8%B7%D8%A8%D9%8A%D9%82%D8%A7%D8%AA%20%D8%AD%D8%B5%D8%B1%D9%8A%D8%A9");

}

I added the internet permission in the Manifest file:

 <uses-permission android:name="android.permission.INTERNET" />

This opens the web page but in the default browser of the device and I want it to open inside my app. Any help? (please give me a detailed answer/explanation)

M. Omar
  • 231
  • 1
  • 2
  • 9

7 Answers7

13

Add this to your code

webView.setWebViewClient(new WebViewClient(){

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url){
      view.loadUrl(url);
      return true;
    }
});
Sunny
  • 3,064
  • 3
  • 23
  • 38
  • `java.lang.NullPointerException: Attempt to invoke virtual method 'void android.webkit.WebView.setWebViewClient(android.webkit.WebViewClient)' on a null object reference` – auspicious99 Nov 22 '18 at 11:43
2

You need to set up a WebViewClient in order to override that behavior (opening links using the web browser).

Use this;

webview.setWebViewClient(new WebViewClient());

Android documentation says:

public void setWebViewClient (WebViewClient client)

Sets the WebViewClient that will receive various notifications and requests. This will replace the current handler.

Rajesh
  • 12,393
  • 4
  • 47
  • 76
2

Enjoy full code :

Oncreate () :

        webView = (WebView) findViewById(R.id.webView1);

        if(Constants.isNetworkAvailable(mContext)){
            webView.setWebViewClient(new MyWebViewClient());
            webView.setWebChromeClient(new WebChromeClient() );

            webView.getSettings().setJavaScriptEnabled(true);     
            webView.getSettings().setPluginState(PluginState.ON);
            webView.getSettings().setBuiltInZoomControls(true);
            webView.getSettings().setSupportZoom(true); 

            webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
            webView.setScrollbarFadingEnabled(false);

            webView.setInitialScale(30);
            webView.loadUrl(url);

        }else{
            Toast.makeText(mContext, Constants.msgNoInternet, Toast.LENGTH_LONG).show();
        }

MyWebViewClient :

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

            if (!pDialog.isShowing()) {
                pDialog.show();
            }

            return true;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            //view.loadUrl(url);
            System.out.println("on finish");
            if (pDialog.isShowing()) {
                pDialog.dismiss();
            }

        }
    }
KishuDroid
  • 5,858
  • 4
  • 28
  • 45
1

Kotlin version of Sunny's answer

webView.webViewClient = object : WebViewClient() {
    override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
        view?.loadUrl(request?.url.toString())
        return true
    }
}
Rammohan Raja
  • 391
  • 1
  • 3
  • 9
0

I hope this will help you.

In your web view layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainll"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/relay"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#c7bbac">

        <ImageView
            android:id="@+id/txtmain"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:scaleType="fitXY"
            android:src="@drawable/topbar50" />

        <ImageView
            android:id="@+id/backbutn"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:adjustViewBounds="true"
            android:paddingTop="2dp"
            android:src="@drawable/backbtn" />
    </RelativeLayout>

    <WebView  
        android:id="@+id/webView1"
        android:layout_below="@+id/relay"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</RelativeLayout>

Webview Button Onclick:

webbutton = (ImageView) findViewById(R.id.web);
webbutton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View arg0) {
        Intent intent = new Intent(getApplicationContext(), WebViewActivity.class);
        startActivity(intent);
    }
});

Webview Activity:

public class WebViewActivity extends Activity {

private WebView webViewurl;
ImageView back;
AndroidInterface AMW = AndroidInterface.GetInstance();

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.webview);
    back = (ImageView) findViewById(R.id.backbutn);
    webViewurl = (WebView) findViewById(R.id.webView1);

    webViewurl.getSettings().setJavaScriptEnabled(true);

    webViewurl.getSettings().setBuiltInZoomControls(true);
    final Activity activity = this;
    webViewurl.setWebViewClient(new WebViewClient() {
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
        }
    });
    webViewurl.loadUrl("http://example.com");

    back.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            finish();
        }
    });
}
Ilmari Karonen
  • 44,762
  • 9
  • 83
  • 142
MurugananthamS
  • 2,233
  • 4
  • 16
  • 47
  • Where should I put the: Webview Button Onclick ? ... And what is the "AndroidInterface"? is that a new class that I should add to the project (just reminding you that i am totally newbie, so your answer wasn't clear enough for me, but thanks any way) – M. Omar Oct 08 '15 at 19:08
  • android interface doesnt matter here you just omit that Android interface ,where you want to open the webview page that place you put the button,in onclick listener of the button you just call the webview activity@M.Omar – MurugananthamS Oct 09 '15 at 05:29
0
 webView = (WebView) findViewById(R.id.youtubelink);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl("your url");

        webView.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
S HemaNandhini
  • 185
  • 2
  • 5
-1

WebView myWebView = (WebView) findViewById(R.id.webview); myWebView.loadUrl("http://www.google.co.in");

this code works fine...

Above code opens the link in your App.

Naveen
  • 545
  • 2
  • 4
  • 17
  • I think you didn't read my code well !.. I am already using this code and it opens web pages in the default browser. – M. Omar Oct 08 '15 at 19:23