Structure Query Language, C programming, Java, Servlet, Jsp, Unix

Friday, 26 October 2012

Create an application to open any URL inside the application and clicking on any link from that URl should not open Native browser but that URL should open the same screen.

Note:Give "INTERNET" permission in android Manifest file.
WebViewActivity.java
package ps.webview;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class WebviewActivity extends Activity implements OnClickListener 
{
    /**
     *  www.master-gtu.blogspot.com
     *  pankaj sharma(8460479175),
     *  chavda vijay(8460420769) 
     */

//CREATE WEBVIEW OBJECT
 WebView web;
 TextView edurl;
 Button btgo;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        // INTIALIZE WEBVIEW OBJECT
        // GIVE PERMISSION INTERNET IN ANDROIDMAINFEST FILE
        
        web=(WebView) findViewById(R.id.webView1);
        edurl=(TextView) findViewById(R.id.editTexturl);
        btgo=(Button) findViewById(R.id.buttongo);
        
        btgo.setOnClickListener(this);
    }
 @Override
 public void onClick(View arg0) {
  // TODO Auto-generated method stub
  if(edurl.getText().toString().equals(""))
  {
   Toast.makeText(this, "Pls Enter URL...", 1).show();
  }
  else 
  {
   String stringurl=edurl.getText().toString();
   
   if(!stringurl.startsWith("http://") &&  !stringurl.startsWith("https://") )
    stringurl="http://"+stringurl;
   
   //ENABELING JAVA SCRIPT ON WEBVIEW
   web.getSettings().setJavaScriptEnabled(true);
   
   //LOAD URL ON WEBVIEW
   web.loadUrl(stringurl);

   //SETTING WEBVIEW CLIENT TO OPEN IN SAME SCREEN NOT ON NATIVE BROWSER
   web.setWebViewClient(new WebViewClient());
  }
 }
}

3 comments:

  1. Rajkumar Arumugam7 May 2013 at 00:58

    i am new to android development.
    i am getting error.

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

    edurl=(TextView) findViewById(R.id.editTexturl);

    btgo=(Button) findViewById(R.id.buttongo);

    Error: webView cannot be resolved or is not a field
    Error: editTexturl cannot be resolved or is not a field
    Error: buttongo cannot be resolved or is not a field

    ReplyDelete
  2. Hi Rajkumar Arumugam,
    Ok this error comes because may be you have imported "android.R.*" on top lines because R is resource file for your project and also android, so it check webView1, editTexturl and buttongo in android Resource file so you have to remove that imported line so it will take R file from your project.

    Remove That Line and try

    Happy Coding :)

    ReplyDelete