WebView is a view that displays web pages inside your application. WebView turns your application as a web application.
Below is the syntax for WebView
1. Open mainactivity.xml and write the below code.
2. Open MainActivity.java and write the below code.
3. Open Mainfest.xml and add Internet Permission
Below is the syntax for WebView
<WebView
android:i"@+id/webview"
andorid:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
In order to use WebView you have to get a reference of it in java file. To get reference you have to make an Object of WebView class.android:i"@+id/webview"
andorid:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
WebView webview = (WebView)findViewById(R.id.webview);
In Order to load a web url in webview you have to call loadUrl(String url) method of the WebView class.
webview.loadUrl("http://www.androidcooltech.blogspot.in/");
1. Open mainactivity.xml and write the below code.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.minixithub.webviewtutorial.MainActivity"> <WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"> </WebView> <ProgressBar
android:id="@+id/progressbar"
android:layout_width="35dp"
android:layout_height="35dp"
app:layout_constraintTop_toBottomOf="parent"
app:layout_constraintBottom_toTopOf="parent"
app:layout_constraintRight_toLeftOf="parent"
app:layout_constraintLeft_toRightOf="parent" /> </android.support.constraint.ConstraintLayout>
package com.minixithub.webviewtutorial; import android.app.Activity; import android.graphics.Bitmap; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.ProgressBar; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private WebView webView; private Activity activity; private ProgressBar progressBar; @Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); activity = this; webView = (WebView)findViewById(R.id.webview); webView.getSettings().setJavaScriptEnabled(true); progressBar = (ProgressBar)findViewById(R.id.progressbar); // For Open webpage in WEBVIEW webView.setWebViewClient(new WebViewClient() { public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } @Override
public void onPageStarted(WebView view, String url, Bitmap favicon) { // Displaying Loading Bar on Page Load Start
progressBar.setVisibility(View.VISIBLE); super.onPageStarted(view, url, favicon); } @Override
public void onPageFinished(WebView view, String url) { // Removing Loading on PageLoad Finish
progressBar.setVisibility(View.GONE); super.onPageFinished(view, url); } }); // Loading WebPage
webView.loadUrl("http://androidcooltech.blogspot.in/"); } @Override
public void onBackPressed() { // Going Back to last WebPages on BackButton Pressed
if(webView.canGoBack()){ webView.goBack(); }else
super.onBackPressed();
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.minixithub.webviewtutorial"> <uses-permission android:name="android.permission.INTERNET"/> <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
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>

No comments:
Post a Comment