Android webview tutorial aide ide
Tutorial on how to create webview in aide ide, before creating webview you must to know what is webview.
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.
First goto your "AndroidManifest.xml" and add this two permission, this permission is very important.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />Next goto your "main.xml" located res/layout and add this webview code layout.
<WebView
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:id="@+id/webView"/>
Last step, goto your "MainActivity.java" located at your java folder and add this following codes:
import android.webkit.*;
private WebView webView;
webView=(WebView)findViewById(R.id.webView); webView.getSettings().setLoadsImagesAutomatically(true);         webView.getSettings().setJavaScriptEnabled(true);  webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.loadUrl("http://yourwebsite.com");
if you have problem adding codes? follow my screenshot below.
Enable download in android webview
Add a new two permission in "AndroidManifest.xml" this permission allow your created app to save files in your device.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
In your "MainActivity.java" add this following codes for download.
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();
    }});
Note for higher version of android you need to add this following code:
if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
   Log.e("Permission error","You have permission");
   
  }else{
   ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE);
  }
Done.
For more webview code please visit Android webview settings and more tutorials aide ide


 
 
Comments
Post a Comment
Leave a comment hdh