Last Updated on August 16, 2023 by Humera Hallari
For personal website owners sometimes there is a desire to create their own apk file to be uploaded to playstore. But all of that is constrained by limited knowledge of android mobile programming languages such as Java.
Finally to create their website mobile application there are two options, one done alone, or both paying people to create it. Actually there is already a lot of code on the internet about how we can create Android applications for websites by using webview.
With webview converting a website into apk format will be more practical and faster, than creating from the beginning of the website version in mobile form.
Here we share with you the source code that you can use to convert your personal website into apk file format.
Step create a webview for a personal website
1) Download Source Code, the link is below this article.
2) Extract the file and then open the project using Android Studio.
3) On “MainActivity.java” change the website URL as you see fit. Done and you can produce APKs file that can be uploaded to Google PlayStore.
MainActivity.java
package com.vidiomu.vidiomudownloader.vidiomudownloader;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.app.AlertDialog;
import android.app.DownloadManager;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.webkit.CookieManager;
import android.webkit.DownloadListener;
import android.webkit.MimeTypeMap;
import android.webkit.URLUtil;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import com.vidiomu.vidiomudownloader.R;
public class MainActivity extends AppCompatActivity {
private WebView webView;
private String sFileName,sUrl,sUserAgent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webview);
webView.setWebViewClient(new WebViewClient());
webView.setWebChromeClient(new WebChromeClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("https://www.nosware.com/");
webView.setDownloadListener(new DownloadListener() {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
String filename = URLUtil.guessFileName(url,contentDisposition,getFileType(url));
sFileName = filename;
sUrl = url;
sUserAgent = userAgent;
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED){
downloadFile(filename,url,userAgent);
}else {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},1001);
}
}else {
downloadFile(filename,url,userAgent);
}
}
});
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
private void downloadFile(final String filename, final String url, final String userAgent) {
final String cookie = CookieManager.getInstance().getCookie(url);
final DownloadManager downloadManager = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
final DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("[Download File]")
.setMessage("Do you want to download now?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
try {
request.setTitle(filename)
.setDescription("Downloading...")
.addRequestHeader("cookie",cookie)
.addRequestHeader("User-Agent",userAgent)
.setMimeType(getFileType(url))
.setAllowedOverMetered(true)
.setAllowedOverRoaming(true)
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,filename)
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE|
DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
downloadManager.enqueue(request);
Toast.makeText(MainActivity.this, "Download Started", Toast.LENGTH_SHORT).show();
sUrl = "";
sFileName = "";
sUserAgent = "";
}catch (Exception ignored){
Toast.makeText(MainActivity.this, ignored.toString(), Toast.LENGTH_SHORT).show();
}
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
})
.show();
}
private String getFileType(String url){
ContentResolver contentResolver = getContentResolver();
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
return mimeTypeMap.getExtensionFromMimeType(contentResolver.getType(Uri.parse(url)));
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode==1001){
if (grantResults.length>0&&grantResults[0]==PackageManager.PERMISSION_GRANTED){
if (!sUrl.equals("")&&!sFileName.equals("")&&!sUserAgent.equals("")){
downloadFile(sFileName,sUrl,sUserAgent);
}
}
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.vidiomu.vidiomudownloader">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
<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:usesCleartextTraffic="true"
android:requestLegacyExternalStorage="true"
android:hardwareAccelerated="true"
android:theme="@style/AppTheme">
<activity android:name="com.vidiomu.vidiomudownloader.vidiomudownloader.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
You can download the full source code of webview support to download files through the link below.
Size: 20.9 MB
IDE: Android Studio
Language: Java
Developer: Nosware
Website: https://www.nosware.com
Read more about Google Play Store Apk for Chinese Phone