-1

I am using WebView to open a webpage in an Android app. I described the layout as:

<?xml version="1.0" encoding="utf-8"?>
<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>

Added the permission in Android Manifest file as:

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

And used WebView in my activity class as:

WebView theWebPage = new WebView(this);
setContentView(theWebPage);
theWebPage.loadUrl("http://www.google.com");

Having done that, I ran the app on a couple Android phones. After opening, the app asks for which application(Out of Chrome, Mozilla or any other browser installed on the phone) to use to open the webpage i.e "http://www.google.com".

I want the app to open the webpage inside it itself. Help?

pxthxk
  • 2,405
  • 2
  • 12
  • 12
  • Look here: http://stackoverflow.com/questions/7746409/android-webview-launches-browser-when-calling-loadurl – Mike M. Aug 29 '14 at 14:59

2 Answers2

1

Your xml is correct.You need to setWebViewClient as your custom. This is the code for making a simple browser. Maybe this would help you clarify your doubts.

Broweser.java

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.simplebrowser);

    ourBrow = (WebView)findViewById(R.id.wvBrowser);
    ourBrow.getSettings().setJavaScriptEnabled(true);
    ourBrow.getSettings().setLoadWithOverviewMode(true);
    ourBrow.getSettings().setUseWideViewPort(true);
    ourBrow.setWebViewClient(new ourViewClient());
    ourBrow.getSettings().setBuiltInZoomControls(true);
    try{
    ourBrow.loadUrl("http://www.google.com");
    }catch(Exception e){
        e.printStackTrace();
    }
    Button go = (Button)findViewById(R.id.bGo);
    Button back = (Button)findViewById(R.id.bBack);
    Button refresh = (Button)findViewById(R.id.bRefresh);
    Button forward = (Button)findViewById(R.id.bForward);
    Button clearHistory = (Button)findViewById(R.id.bHistory);
    url = (EditText)findViewById(R.id.etURL);

    go.setOnClickListener(this);
    back.setOnClickListener(this);
    refresh.setOnClickListener(this);
    forward.setOnClickListener(this);
    clearHistory.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch(v.getId()){
    case R.id.bGo:
        String theWebSite = url.getText().toString();
        ourBrow.loadUrl(theWebSite);
        //hiding keyboard after the input in EditText
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(url.getWindowToken(),0);
        break;
    case R.id.bBack:
        if(ourBrow.canGoBack())
            ourBrow.goBack();
        break;
    case R.id.bRefresh:
        ourBrow.reload();
        break;
    case R.id.bForward:
        if(ourBrow.canGoForward())
            ourBrow.goForward();
        break;
    case R.id.bHistory:
        ourBrow.clearHistory(); 
        break;

    }
}

ourViewClient.java

public class ourViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView v,String url){
    v.loadUrl(url);
    return true;
}
Maverick
  • 535
  • 3
  • 13
0

You need to set a custom WebViewClient and override shouldOverrideUrlLoading

http://developer.android.com/reference/android/webkit/WebViewClient.html#shouldOverrideUrlLoading(android.webkit.WebView, java.lang.String)

r2DoesInc
  • 3,704
  • 3
  • 27
  • 58