webview in android studio tutorial


0

In this lecture, we will provide an example of how to create webview
Using Android Studio

You can use the code to view your site through the Android application
You will design it yourself

what we need ??

1- Create a new project in Android Studio :

2- you need to make a New activity with name : MainActivity

the Code :

package com.cashfast.app;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {

// https://github.com/rupok/webview/blob/master/Android%20Native%20WebView/src/io/codetic/webview/ShowWebView.java
    private boolean haveNetworkConnection() {
        boolean haveConnectedWifi = false;
        boolean haveConnectedMobile = false;

        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo[] netInfo = cm.getAllNetworkInfo();

        for (NetworkInfo ni : netInfo) {
            if (ni.getTypeName().equalsIgnoreCase("WIFI"))
                if (ni.isConnected())
                    haveConnectedWifi = true;
            if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
                if (ni.isConnected())
                    haveConnectedMobile = true;
        }
        return haveConnectedWifi || haveConnectedMobile;
    }



    private WebView mWebView;

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mWebView = findViewById(R.id.activity_main_webview);
        mWebView.setWebViewClient(new WebViewClient());
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        if(haveNetworkConnection()){

        // REMOTE RESOURCE
         mWebView.loadUrl("https://mobilerewards.io/mobile/?id=89b5e559fb664dc7bf138a872402d373");
         mWebView.setWebViewClient(new MyWebViewClient());

        // LOCAL RESOURCE
        // mWebView.loadUrl("file:///android_asset/index.html");


        } else {
            mWebView.loadUrl("file:///android_asset/error.html");
        }
    }

    @Override
    public void onBackPressed() {
        if(mWebView.canGoBack()) {
            mWebView.goBack();
        } else {
            super.onBackPressed();
        }
    }
}

change “https://mobilerewards.io/mobile/?id=89b5e559fb664dc7bf138a872402d373” to your link or blog

activity_main.xml :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <WebView
        android:id="@+id/activity_main_webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

now , we will make WebViewClient class with name “MyWebViewClient”

package com.cashfast.app;

import android.content.Intent;
import android.net.Uri;
import android.webkit.WebView;
import android.webkit.WebViewClient;

class MyWebViewClient extends WebViewClient {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        Uri uri = Uri.parse(url);
        if (uri.getHost() != null && uri.getHost().contains("mobilerewards.io")) {
            return false;
        }

        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        view.getContext().startActivity(intent);
        return true;
    }
}

Note : you nede to change the : mobilerewards.io Domain

make assets Floder and put into it the error.html

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="mobile-web-app-capable" content="yes">
    <title>No Connection</title>

    <!-- Stylesheets-->
    <style type="text/css">
 body{
  background: #E1e1e1;
}
#cloud{
  width: 300px;
  height: 120px;
  background: #676767;
  background: -webkit-linear-gradient(-90deg,#676767 5%, #676767 100%);
  -webkit-border-radius: 100px;
  -moz-border-radius: 100px;
  border-radius: 100px;
  position: relative;
  margin: 150px auto 0;
  opacity: .5;
}
#cloud:before, #cloud:after{
  content: '';
  position:absolute;
  background: #676767;
  z-index: -1;
}
#cloud:after{
  width: 100px;
  height: 100px;
  top: -50px;
  left:50px;
  -webkit-border-radius: 100px;
  -moz-border-radius: 100px;
  border-radius: 100px;
}
#cloud:before{
  width: 120px;
  height: 120px;
  top: -70px;
  right: 50px;
  -webkit-border-radius: 200px;
  -moz-border-radius: 200px;
  border-radius: 200px;
}
.shadow {
  width: 300px;
  position: absolute;
  bottom: -10px;
  background: black;
  z-index: -1;
  -webkit-box-shadow: 0 0 25px 8px rgba(0,0,0,0.4);
  -moz-box-shadow: 0 0 25px 8px rgba(0,0,0,0.4);
  box-shadow: 0 0 25px 8px rgba(0,0,0,0.4);
  -webkit-border-radius: 50%;
  -moz-border-radius: 50%;
  border-radius: 50%;
}
h2 {
  color: #fff;
  font-size: 20px;
  padding-top: 15px;
  text-align: center;
  margin: 5px auto;
}
h4 {
  color: #fff;
  font-size: 12px;
  margin: 0 auto;
  padding: 0;
  text-align: center;
}
 </style>

<body>
<div id="cloud"> <h2>No Connection :(</h2>
    <h4>Check your WiFi or Mobile Internet!</h4>
    <span class="shadow"></span></div>

</body>
</html>

the manifest.xml code :

<?xml version="1.0" encoding="utf-8"?>
<manifest
    package="com.cashfast.app"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <application
        android:allowBackup="true"
        android:fullBackupContent="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        tools:ignore="GoogleAppIndexingWarning">
        <activity
            android:name="com.cashfast.app.MainActivity"
            android:label="@string/app_name" >
            <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 Code : Github


Like it? Share with your friends!

0
admin

0 Comments

Your email address will not be published. Required fields are marked *