Atul Mittal Atul Mittal - 25 days ago 5
Android Question

Embedding a Web page in an Android App

I am trying to embed a webpage inside an android app.
Following is the code written in the main_activity java file of my app.

package com.example.webpage;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView webview = new WebView(this);
setContentView(webview);
webview.loadUrl("http://www.google.com/");
}

@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;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}


The problem is whenever i launched my app in an emulator it shows me the "Page not Found" error.
Please help me out.
Output in the emulator

Answer

Try this:

  1. First, you take the webview in XML.

  2. Bind to them like:

    WebView webView = (WebView)findViewById(R.id.webView1);
    
  3. Then do this:

    String url = "http://www.google.com/";
    WebView webView = (WebView)findViewById(R.id.webView1);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.loadUrl(url);