<!-- ANDROID APP USING WEBVIEW CONCEPT -->
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.
To add WebView to your application, you have
to add <WebView> element to your xml layout file.
activity_main.xml
<WebView
android:id="@+id/Webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Use it, you have to get a reference of this view in Java file.
To get a reference, create an object of the class WebView.
activity_main.java
WebView webView = (WebView) findViewById(R.id.webview);
Load a web url into the WebView, you need to call a method loadUrl(String url)
of the WebView class, specifying the required url. Its syntax is:
webView.loadUrl("https://sathiyaramanproducts.blogspot.com");
If you click on any link inside the webpage of the WebView, that page will not
be loaded inside your WebView. In order to do that you need to extend your class from
WebViewClient and override its method.
private class MyBrowser extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
--------------------
Simply start to create application step by step
--------------------
Step 1: Create empty activity application in android
lang : JAVA
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"><uses-permission android:name="android.permission.INTERNET" /><application
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication"
tools:targetApi="31">
<activity</application>android:name=".splashscreen"android:exported="true"android:screenOrientation="portrait"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activityandroid:name=".MainActivity"android:exported="true"android:screenOrientation="portrait"/>
</manifest>
Step 4: create design for your splash screen
activity_splashscreen.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
android:background="#4A2D8C"
tools:context=".splashscreen">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="133dp"
android:layout_marginEnd="133dp"
android:text="Your app name"
android:textColor="#FFFFFF"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView2" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="214dp"
android:layout_height="169dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.501"
app:srcCompat="@mipmap/ic_launcher" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Powered By Your Brand name."
android:textColor="#567E7E7E"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintVertical_bias="0.862" />
</androidx.constraintlayout.widget.ConstraintLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MainActivity">
<WebView
android:id="@+id/Webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
Step 5: Design complete after that to create functional part
splashscreen.java
Note: this is my package name ( your can use your package name)
package com.sathiyaramanproducts.myapplication;
package com.sathiyaramanproducts.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ProgressBar;
public class splashscreen extends AppCompatActivity {
private ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splashscreen);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// This method will be executed once the timer is over
Intent i = new Intent(splashscreen.this, MainActivity.class);
startActivity(i);
finish();
}
}, 3000);
}
}
MainActivity.java
package com.sathiyaramanproducts.myapplication;
//use your package nameimport androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.net.ConnectivityManager;
import android.content.DialogInterface;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.Manifest;
import android.app.DownloadManager;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;
import android.webkit.CookieManager;
import android.webkit.DownloadListener;
import android.webkit.URLUtil;
import android.webkit.WebViewClient;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.Network;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
WebView webView;
ConnectivityManager connectivityManager;
NetworkInfo info;
String url = "https://sathiyaramanproducts.blogspot.com/";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//code for loading time process bar
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading Data...");
progressDialog.setCancelable(false);
webView = (WebView) findViewById(R.id.Webview);
webView.loadUrl(url);
webView.getSettings().setJavaScriptEnabled(true);
//code for load url link within the application
webView.setWebViewClient(new WebViewClient());
//code for process bar
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
if (progress < 100) {
progressDialog.show();
}
if (progress == 100) {
progressDialog.dismiss();
}
}
});
boolean checkConnection=new MainActivity().checkConnection(this);
if (checkConnection) {
Toast.makeText(MainActivity.this, "Launching...",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this,
"Internet not found... Try again later :/",
Toast.LENGTH_LONG).show();
MainActivity.super.onBackPressed();
}
//code for handle downloads
webView.setDownloadListener(new DownloadListener()
{
@Override
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimeType,
long contentLength) {
DownloadManager.Request request = new DownloadManager.Request(
Uri.parse(url));
request.setMimeType(mimeType);
String cookies = CookieManager.getInstance().getCookie(url);
request.addRequestHeader("cookie", cookies);
request.addRequestHeader("User-Agent", userAgent);
request.setDescription("Downloading File...");
request.setTitle(URLUtil.guessFileName(url, contentDisposition,
mimeType));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request
.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(
Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(
url, contentDisposition, mimeType));
DownloadManager dm = (DownloadManager)
getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
Toast.makeText(getApplicationContext(),
"Downloading File", Toast.LENGTH_LONG).show();
}});
}
//code for exit event and webview goback when back btn pressed
@Override
public void onBackPressed() {
if (webView != null && webView.canGoBack()) {
webView.goBack();// if there is previous page open it
} else {
new AlertDialog.Builder(this)
.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
MainActivity.super.onBackPressed();
}
})
.setNegativeButton("No", null)
.show();
}
}
//code for connectivity manager
public boolean checkConnection(Context context) {
// TODO Auto-generated method stub
boolean flag = false;
try {
connectivityManager = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
info = connectivityManager.getActiveNetworkInfo();
if (info.getType() == ConnectivityManager.TYPE_WIFI) {
System.out.println(info.getTypeName());
flag = true;
}
if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
System.out.println(info.getTypeName());
flag = true;
}
} catch (Exception exception) {
System.out.println("Exception at network connection....."
+ exception);
}
return flag;
//return false;
}
}
The webview application code was completed
Step 6: Build the application