4

Hey i am developing an android application , and i want to connect to web inside that application. However i have tried WebView to someextent but its displaying file's on my directory fine but when connecting to google.com it display's an error !

Then i added this file <uses-permission android:name="android.permission.INTERNET" />

in my Manifest.xml and now the the url(google.com) is being displayed in the browser Any help how i can open the browser inside my application ?

Anuj
  • 301
  • 1
  • 4
  • 20

4 Answers4

11

Using webview.

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;

public class WebViewDemo extends Activity 
{
    private WebView mWebView = null;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

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

add <uses-permission android:name="android.permission.INTERNET" /> in manifest file.

main.xml file which used here.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<WebView  
        android:id="@+id/webView"
        android:scrollbars="none" 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
    />
</LinearLayout>
Chirag
  • 56,621
  • 29
  • 151
  • 198
  • well trying this also comes up with the same problem , i am redirected to the browser and once i get redirected i can come back to my application! – Anuj Jul 12 '11 at 06:13
  • I dont understand what you are trying to say ? please can you explain more – Chirag Jul 12 '11 at 06:18
  • Okay , Basically i am working on an android application and i want to open a webpage inside my application so that i can close it anytime i want and can get back to my application , but with webview the webpage is getting displayed in the external browser and i find no way to return to my application from it......... – Anuj Jul 12 '11 at 06:24
  • 1
    No,Anuj this webview demo is not open in browser its open in your application's webview . So when webpage is load you are in your application, not in another browser. – Chirag Jul 12 '11 at 06:33
1

it must useful to u.. it will not load in browser..

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;

public class WebViewDemo extends Activity {
    private class MyWebViewClient extends WebViewClient {
      @Override
      public boolean shouldOverrideUrlLoading(WebView view, String url) {
          view.loadUrl(url);
          return true;
      }
  }
    private WebView webView;
    private EditText urlField;

    private Button goButton;

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

        // Create reference to UI elements
        webView  = (WebView) findViewById(R.id.webview_compontent);
        urlField = (EditText)findViewById(R.id.url);
        goButton = (Button)findViewById(R.id.go_button);

        // workaround so that the default browser doesn't take over
        webView.setWebViewClient(new MyWebViewClient());

        // Setup click listener
        goButton.setOnClickListener( new OnClickListener() {
            public void onClick(View view) {
                openURL();
            }
        });

        // Setup key listener
        urlField.setOnKeyListener( new OnKeyListener() {
            public boolean onKey(View view, int keyCode, KeyEvent event) {
                if(keyCode==KeyEvent.KEYCODE_ENTER) {
                    openURL();
                    return true;
                } else {
                    return false;
                }
            }
        });

    }

    /** Opens the URL in a browser */
    private void openURL() {
        webView.loadUrl(urlField.getText().toString());
        webView.requestFocus();
    }    
}

xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

    <LinearLayout 
        android:orientation="horizontal"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content">

        <EditText  
        android:id="@+id/url" 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content"     
        android:lines="1"
        android:layout_weight="1.0" android:hint="http://"/>

        <Button
        android:id="@+id/go_button"
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content"     
        android:text="@string/go_button"
        />

    </LinearLayout>

    <WebView  
        android:id="@+id/webview_compontent"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:layout_weight="1.0"   
    />
</LinearLayout>

and in mainfiest file don't forget to give internet permission.. i hope this help u..

Nirav Dangi
  • 3,607
  • 4
  • 49
  • 60
  • 1
    have tried this but the webpage is opening in the browser with this , whereas my problem was to load the webpage inside the application – Anuj Jul 12 '11 at 06:09
  • no it will no load in browser.. you just decrease the height width of webview in xml.. u will see that its loading in particular portation,,, – Nirav Dangi Jul 16 '11 at 06:03
1

Another way to do this is via Intents:

Uri uri = Uri.parse("http://www.gmail.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);

You need to specify permission in manifest file

    <uses-permission android:name="android.permission.INTERNET" />
Sumit
  • 736
  • 8
  • 26
0

You can use webview.What you said,it doesn't show google.Maybe problem is in other place.There is no way of being an error while showing google.Webview is ofcourse for the purpose of showing web pages.

Rasel
  • 15,499
  • 6
  • 40
  • 50
  • with webview it shows google.com but in the browser whereas i want the webpage to be displayed inside the application itself. – Anuj Jul 12 '11 at 06:14