Blog purpose for android basic example for android app developer. any query please my contact

Wednesday, 20 September 2017

Android Web View Example


                            Android Web View Example


In Android, WebView is a view used to display the web pages in application. This class is the basis upon which you can roll your own web browser or simply use it to display some online content within your Activity. We can also specify HTML string and can show it inside our application using a WebView. Basically, WebView turns application into a web application.

Internet Permission Required For Webview:

Create Android Application

1. The Application First Step Create a New Project Start project



2. Rename Application Name Below Screen Ex: Web View
Click Next



3. After Spacific Minium sdk API 23 Select Lolipop 5.0
Click Next



4. Final Select Blank Activity Click Next



5. Then Select MainActivity Click Finish



6. Then Open a Android Studio

1. MainActivity.Java

package com.example.WebView;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.webkit.WebView;

import android.webkit.WebViewClient;

import android.widget.Button;

import android.widget.EditText;

public class MainActivity extends Activity  {

   Button b1;

   EditText ed1;

   private WebView wv1;

   @Override

   protected void onCreate(Bundle savedInstanceState) {

      super.onCreate(savedInstanceState);

      setContentView(R.layout.activity_main);

      b1=(Button)findViewById(R.id.button);

      ed1=(EditText)findViewById(R.id.editText);

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

      wv1.setWebViewClient(new MyBrowser());

      b1.setOnClickListener(new View.OnClickListener() {

         @Override

         public void onClick(View v) {

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

            wv1.getSettings().setLoadsImagesAutomatically(true);

            wv1.getSettings().setJavaScriptEnabled(true);

            wv1.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

            wv1.loadUrl(url);

         }

      });

   }

   private class MyBrowser extends WebViewClient {

      @Override

      public boolean shouldOverrideUrlLoading(WebView view, String url) {

         view.loadUrl(url);

         return true;

      }

   }

}


2. Activity Main.xml



<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" android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
 
   <TextView android:text="WebView" android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/textview"
      android:textSize="35dp"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true" />
   
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="WebView"
      android:id="@+id/textView"
      android:layout_below="@+id/textview"
      android:layout_centerHorizontal="true"
      android:textColor="#ff7aff24"
      android:textSize="35dp" />
   
   <EditText
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/editText"
      android:hint="Enter Text"
      android:focusable="true"
      android:textColorHighlight="#ff7eff15"
      android:textColorHint="#ffff25e6"
      android:layout_marginTop="46dp"
      android:layout_alignParentLeft="true"
      android:layout_alignParentStart="true"
      android:layout_alignRight="@+id/imageView"
      android:layout_alignEnd="@+id/imageView" />
   
 
   
   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Enter"
      android:id="@+id/button"
      android:layout_alignTop="@+id/editText"
      android:layout_toRightOf="@+id/imageView"
      android:layout_toEndOf="@+id/imageView" />
   
   <WebView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/webView"
      android:layout_below="@+id/button"
      android:layout_alignParentLeft="true"
      android:layout_alignParentStart="true"
      android:layout_alignParentRight="true"
      android:layout_alignParentEnd="true"
      android:layout_alignParentBottom="true" />
   
</RelativeLayout>


3. Activity Mainifests.xml

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

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

   package="com.example.WebView" >

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

   <application

      android:allowBackup="true"

      android:icon="@mipmap/ic_launcher"

      android:label="@string/app_name"

      android:theme="@style/AppTheme" >

      

      <activity

         android:name=".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>



                                 <<  PREVIEW >>


                               <<      NEXT      >>



No comments:

Post a Comment