Thursday, 8 October 2015

WebView in Android

WebView is a view that display web pages inside your application. You can also specify HTML string and can show it inside your application using WebView. WebView makes turns your application to a web application.
activity_main.xml
-----------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:tools="http://schemas.android.com/tools"
   
xmlns:app="http://schemas.android.com/apk/res-auto"
   
android:layout_width="match_parent"
   
android:orientation="vertical"
    
android:layout_height="match_parent"
   
app:layout_behavior="@string/appbar_scrolling_view_behavior"
   
tools:showIn="@layout/activity_main"
   
tools:context=".MainActivity">

    <
Button
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text="Go Anil Android"
       
android:id="@+id/button"
       
android:layout_marginTop="5dp"
       
android:layout_gravity="center_horizontal" />

    <
WebView
       
android:layout_width="fill_parent"
       
android:layout_height="fill_parent"
       
android:id="@+id/webView" />
</
LinearLayout>

MainActivity.Java
----------------------------
package com.android.anil.webview;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
Button
go;
    WebView
webView;
   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.
toolbar);
        setSupportActionBar(toolbar);
       
go=(Button)findViewById(R.id.button);
       
webView=(WebView)findViewById(R.id.webView);
       
go.setOnClickListener(new View.OnClickListener() {
           
@Override
           
public void onClick(View v) {
               
webView.loadUrl("http://anilandroid123.blogspot.in/");
            }
        });
    }
}

Output is:
-----------------------------

No comments:

Post a Comment