Quickly create a ‘Website Viewer’ Android App using WebChromeClient.

(by AXDS)

Follow the bold words to speed up the tutorial šŸ™‚

Create a new Android Studio Project. For this simple application a blank template will be a good choice.

Once it has all loaded replace the contents of the ‘mainActivity.xml’Ā file with:


<!--?xml version="1.0" encoding="utf-8"?--> <webview xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/webview" android:layout_width="fill_parent" android:layout_height="fill_parent"></webview>

Add to the ‘AndroidManifest.xml’:

<uses-permission android:name="android.permission.INTERNET"> </uses-permission>

Replace nearly allĀ the contents, don’t removeĀ your package declaration ofĀ ‘mainActivity.java’ with:

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class WebPageLoader extends Activity
{
    final Activity activity = this;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
        setContentView(R.layout.mainActivity);
        WebView webView = (WebView) findViewById(R.id.webview);
        webView.getSettings().setJavaScriptEnabled(true);

        webView.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress)
            {
                activity.setTitle("Loading...");
                activity.setProgress(progress * 100);

                if(progress == 100)
                    activity.setTitle(R.string.app_name);
            }
        });

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {
                view.loadUrl(url);
                return true;
            }
        });

        webView.loadUrl("http://www.informaticskb.wordpress.com");
    }
}

Connect your phone and run the application!

Advertisements