Android AJAX Auto Load

What is Ajax Auto Load?

If you take a look at dynamic websites, the content of the website will be refreshed automatically without user intervention. Example – CricInfo in which the score card will be refreshed in a time interval and display the latest score.

AJAX Auto Load in Android

The Android application which we are going to develop will refresh the content of activity and show the latest content retrieved from the server through AJAX call. Our application gonna get the server’s time through AJAX call for every 5 seconds and update the server time inside Android activity. This will happen without user intervention (like Tap, Scroll, Click etc.,).

Interesting? Let’s us start create the application.

I would recommend you to take a look at AJAX Tutorial before we start developing the application as it will give you an idea about AJAX Auto Load.

[pgsubscribe]

As this application requires Internet connectivity, we need to check if Internet connectivity is available or not.

Here are the tutorials which talk about checking Wifi/Mobile Internet:

Android Check Wi-Fi Internet Connection
Android Check Mobile Internet Connection

Quick Links

Project Structure

Create layout XML

  • Create new android project [File >> New >> Android Project] with project name AndroidAJAXAutoLoad
  • Click next and select target android device version [I chose version 2.2]
  • Click next and enter package name – ‘com.prgguru.android’
  • Click finish

Code Listings

Open main.xml, now you can view the layout as either XML or in graphical view and just replace the XML with below one:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="@string/content" android:gravity="center"/>

    <WebView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/webkit"
        android:layout_width="fill_parent"
        android:layout_height="306dp" />

</LinearLayout>

MainActivity.java
Here is MainActivity code. Each line is explained with comment:

package com.prgguru.android;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.webkit.WebView;

@SuppressLint("SetJavaScriptEnabled")
public class MainActivity extends Activity {
	 WebView browser;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //Refer WebView
        browser=(WebView)findViewById(R.id.webkit);
        //Enable Javascript
        browser.getSettings().setJavaScriptEnabled(true);
        //Set font size to 20
        browser.getSettings().setDefaultFontSize(20);
        //Load the URL
        browser.loadUrl("http://programmerguru.com/ajax-demos/auto_load.html");        
    }
}

[pglinkadssmall1]

Demo

Let us test the application:
Run click on the project >> Run as >> Android application
You could see following screen:



Download Source Code

Entire project is zipped and is available for download. Unzip the downloaded project and to import the project into eclipse, launch eclipse >> File >> Import.. >> Choose downloaded project(How to import android project in eclipse). If you just want to run the application in your mobile and see the output but don’t want to hit your head with source code, download application(apk) file and install it in your mobile device.

Download Source Code Download Application(apk)

*apk in Android is the installation file simliar to exe in windows.

[pglinkadssmall]

I hope you enjoyed the post!! πŸ™‚
Keep us posted with your valuable comments below πŸ™‚

[pgfeedback]
[pgwriteforus]

Related Articles

Author: Udhay

Share This Post On
468 ad
Join now to get more access to our Android Tutorials

Join now to get more access to our Android Tutorials

Once you Join, you will receive Mail containing latest Android Tutorials once a month !!

I promise I wont spam you !!

You have Successfully Subscribed!

Pin It on Pinterest

Shares