Menggunakan WebView di Android Studio
Pada postingan kali ini saya akan berbagi tentang cara penggunaan webview di android studio, webview ini digunakan untuk menampilkan halaman website yang kita panggil dari program android.
ok langsung saja buka android studionya dan buat project baru (pilih yang empty activity).
setelah membuat project tambahkan permission di AndroidManifest
<uses-permission android:name="android.permission.INTERNET"/>
Kemudian tambahkan webview di activity_main.xml, berikut design layout activity_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/web_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
MainActivty.Java
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.net.http.SslError;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import java.security.NoSuchProviderException;
public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {
SwipeRefreshLayout swipeRefreshLayout;
WebView webView;
WebSettings webSettings;
String URL = "https://husnulmshare.blogspot.com/";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try{
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh);
webView = (WebView) findViewById(R.id.web_view);
swipeRefreshLayout.setOnRefreshListener(this);
webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true); // Untuk mengaktifkan javascript
webSettings.getUseWideViewPort();
webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int newProgress) {
// Menampilkan loading ketika webview proses load halaman
swipeRefreshLayout.setRefreshing(false);
}
});
webView.setWebViewClient(new WebViewClient(){
// Ketika webview error atau selesai load page loading akan dismiss
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
swipeRefreshLayout.setRefreshing(false);
}
@Override
public void onPageFinished(WebView view, String url) {
swipeRefreshLayout.setRefreshing(false);
}
});
webView.setWebViewClient(new SSLTolerentWebViewClient());
webView.loadUrl(URL);
}catch (Exception e){
Toast.makeText(this, "Error "+e.getMessage().toString(), Toast.LENGTH_SHORT).show();
}
}
@Override
public void onRefresh() {
// Untuk refresh webview dengan swipe
webView.reload();
}
@Override
public void onBackPressed() {
// Jika Webview bisa di back maka backward page sebelumnya
if (webView.canGoBack()) {
webView.goBack();
} else {
finish();
System.exit(0);
}
}
private class SSLTolerentWebViewClient extends WebViewClient {
public void onReceivedSslError(WebView view, final SslErrorHandler handler, SslError error) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
AlertDialog alertDialog = builder.create();
String message = "SSL Certificate error.";
switch (error.getPrimaryError()) {
case SslError.SSL_UNTRUSTED:
message = "The certificate authority is not trusted.";
break;
case SslError.SSL_EXPIRED:
message = "The certificate has expired.";
break;
case SslError.SSL_IDMISMATCH:
message = "The certificate Hostname mismatch.";
break;
case SslError.SSL_NOTYETVALID:
message = "The certificate is not yet valid.";
break;
}
message += " Do you want to continue anyway?";
alertDialog.setTitle("SSL Certificate Error");
alertDialog.setMessage(message);
alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Ignore SSL certificate errors
handler.proceed();
}
});
alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
handler.cancel();
}
});
alertDialog.show();
}
}
}
Jika sudah jalankan programnya, :D
Happy sharing, Happy Coding
Happy sharing, Happy Coding
Semoga bermanfaat :D
Comments
Post a Comment