Search code examples
javaandroidurlwebviewwebviewclient

android application is opening a browser application instead of a webView, how do I stop that?


Right now when I push a specific button it starts an intent to open a web page into a webView, but instead it opens a Browser Application, revealing the address bar which is not what I want.

After doing some research I found out I can use the shouldOverrideUrlLoading() method to stop this from happening and just open the page in a webView, but I don't know how to implement this method in my code, help would be greatly appreciated!

This is what I have in the class where I'm opening the web page:

public class Codes extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.codes);

    View titleView = getWindow().findViewById(android.R.id.title);
    if (titleView != null) {
      ViewParent parent = titleView.getParent();
      if (parent != null && (parent instanceof View)) {
        View parentView = (View)parent;
        parentView.setBackgroundColor(Color.rgb(228, 108, 10));
      }
    }

    WebView codes = (WebView)findViewById(R.id.codesWebView);
    codes.getSettings().setJavaScriptEnabled(true);
    codes.loadUrl("http://dev10.affirmaconsulting.com:24009/keyentry");
    shouldOverrideUrlLoading(codes, "http://dev10.affirmaconsulting.com:24009/keyentry");

    }
}

Solution

  • We do this exact task in one of our activities and haven't ran into this issue. Here is what ours looks like:

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.document_viewer_activity);
    
        WebView wv = (WebView) findViewById(R.id.documentViewerWebview);
        wv.getSettings().setJavaScriptEnabled(true);
    
        String url = getString(R.string.document_viewer_url) + 
                getIntent().getExtras().getString("media");
        wv.loadUrl(url);
    
    }
    

    With the webview defined in the layout file looking like this:

      <WebView
            android:id="@+id/documentViewerWebview"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" 
     />
    

    Hope this helps.