Pages

Tuesday, 14 November 2017

Android WebView

Hi guys,
Here i'm giving the tutorial on basic Webview in android. That means you can load your own website in android app.Below code will help you to create a basic webview.


Start you android project and create Activity files..

1. Add below code to your activity_main.xml file

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

    <Webview
        android:id="@+id/mywebview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</LinearLayout>


2. Below Code should be the MainActivity.java File

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

public class MainActivity extends Activity {

 private WebView webView;

 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  webView = (WebView) findViewById(R.id.mywebview);
  webView.getSettings().setJavaScriptEnabled(true);
  webView.loadUrl("https://www.google.co.in");

 }

}

3. Webview needs permission for internet in your Androidmanifest file. so add following code for permission in your AndroidManifest.xml

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


4. Now you can run the file to your android phone and you'll get an output like below




Enjoy codeing :-) 










No comments:

Post a Comment