Sunday 25 December 2016

Android Webview

Android - Webview

Android’s WebView allows you to integrate a webpage as a part of the app. WebView comes with all the features that of a desktop browser like managing history, cookies, HTML5 support and lot more.

1. Create a new project 

    Android Studio --> Open --> Create  a new project


Then, select the Target devices


Next, Select Activity


Then,Click on finish.

 After creation of project, open Manifest.xml (left side top):

 And write Internet permission.

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

1. Now open
   activity_main.xml: 

   <WebView
    android:id="@+id/webview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>


2. In  MainActivity.java

  Initialize the webview

  WebView webView;

  webView = (WebView) findViewById(R.id.webview);
  webView.loadUrl("http://www.gmail.com/);


  Add the extra features to webview

  webView.getSettings().setJavaScriptEnabled(true);
  webView.loadUrl(postUrl);
  webView.setHorizontalScrollBarEnabled(false);



 Full Code:

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<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="end.sample.webviewandroid.MainActivity">

    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </WebView>

</RelativeLayout>



MainActivity.java:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;

public class MainActivity extends AppCompatActivity {

    private String url = "https://www.google.co.in/?gfe_rd=cr&ei=x7iFWIz0C4vy8AeCsZ3QBw&gws_rd=ssl";
    WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        webView = (WebView) findViewById(R.id.webview);

        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl(url);

    }
}



Manifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="end.sample.webviewandroid">

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


Now, Run the code.

Output:

Output will be like this