Home > DeveloperSection > Beginner > How to open url in our android application

How to open url in our android application


Android Android 
Ratings:
0 Comment(s)
 1162  View(s)
Rate this:

How to open url in our android application

Android’s WebView allows you to open an own windows for viewing URL or custom html markup page.

URL handling on Android devices works through intents. Intents let apps register themselves to get launched in response to certain specified actions, and URL opening is one of these actions but if you want to open url within your project instead of browser then you have need WebView.

WebView is a view that displays web pages. WebView  class is the basis upon which you can roll your own web browser or simply display some online content within your Activity. It uses the WebKit rendering engine to display web pages and includes methods to navigate forward and backward through a history, zoom in and out, and perform text searches and more.

Here I am creating an example of webview which open site according to type your url.

1.      Create an android project.

2.      Add permission for using internet in your AndroidManifest.xml file

 

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.example.androidwebview"

    android:versionCode="1"

    android:versionName="1.0" >

 

    <uses-sdk

        android:minSdkVersion="8"

        android:targetSdkVersion="18" />

 

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

 

    <application

        android:allowBackup="true"

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" >

        <activity

            android:name="com.example.androidwebview.MainActivity"

            android:label="@string/app_name" >

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

 

            <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

    </application>

 

</manifest>

 

3.      Add webview in activity_main.xml file

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".MainActivity" >

 

    <EditText

        android:id="@+id/urlField"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignTop="@+id/textView1"

        android:layout_centerHorizontal="true"

        android:ems="10" />

 

    <Button

        android:id="@+id/button1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_below="@+id/urlField"

        android:layout_centerHorizontal="true"

        android:onClick="open"

        android:text="Browse" />

 

    <WebView

        android:id="@+id/webView1"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_alignLeft="@+id/textView1"

        android:layout_alignParentBottom="true"

        android:layout_below="@+id/button1" />

 

</RelativeLayout>

4.       Add following code in MainActivity.class

package com.example.androidwebview;

 

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

import android.view.View;

import android.webkit.WebView;

import android.webkit.WebViewClient;

import android.widget.EditText;

 

public class MainActivity extends Activity {

 

                private EditText field;

                private WebView browser;

 

                @Override

                protected void onCreate(Bundle savedInstanceState) {

                                super.onCreate(savedInstanceState);

                                setContentView(R.layout.activity_main);

                                field = (EditText) findViewById(R.id.urlField);

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

                                browser.setWebViewClient(new MyBrowser());

                }

 

                public void open(View view) {

                                String url = field.getText().toString();

                                url = "http://" + url;

                                browser.getSettings().setLoadsImagesAutomatically(true);

                                browser.getSettings().setJavaScriptEnabled(true);

                                browser.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

                                // enabled javascript

 

                                browser.loadUrl(url);

                               

 

                }

 

                private class MyBrowser extends WebViewClient {

                                @Override

                                // ignore browser

                                public boolean shouldOverrideUrlLoading(WebView view, String url) {

                                                view.loadUrl(url);

                                                return true;

                                }

                }

 

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

                }

 

}

Now run your application

 


Don't want to miss updates? Please click the below button!

Follow MindStick