'How-to: Pure Html Android Application'에 해당되는 글 1건

  1. 2010.09.24 How-to: Pure Html Android Application
04.Anddoid2010. 9. 24. 16:28
반응형

How-to: Pure Html Android Application

Cansin

In this tutorial I’ll talk about creating an Android application which will get all it’s data from Http requests. To develop the application, I used WebView and WebViewClient classes of the Webkit.

This is rather a straight forward tutorial. To show a WebView in our application we need to add a WebView to our /res/layout/main.xml file. Simply add the following element under <LinearLayout> tag.

<WebView
    android:id="@+id/webView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
/>

Then we need to access this WebView at our onCreate method and make necessary manipulations. We can get our view by R.findViewById(R.id.webview). To have a native look, we disabled some functionalities of WebView object.

The important part of our code is to listen URL loadings. By default, when you click a HTML link in a WebView, page is loaded at Browser. We want to override this behavior so that when we click a link it will be loaded at our WebView. It’s pretty simple actually. All we have to do is to set a WebViewClient object to our WebView and implement its shouldOverrideUrlLoading(WebView,String) method. We can even send Get request by doing so. Actually we can’t add any get request to our url. If we try to use loadUrl function to do this, we’ll loose our previous post/get objects which are sent to the url. But if you don’t have any previous request objects, it’s safe to use loadUrl to manipulate it.

So here is the source code of the tutorial. Hope, it’ll be useful.

P.s: Don’t forget to add following field to AndroidManifest.xml to have an internet connection.

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
package praeda.muzikmekan;
 
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;
import android.webkit.WebViewClient;
 
public class Index extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
 
        WebView web = (WebView) findViewById(R.id.webView);
        web.getSettings().setJavaScriptEnabled(true); 
        web.getSettings().setJavaScriptCanOpenWindowsAutomatically(false);
        web.getSettings().setPluginsEnabled(false);
        web.getSettings().setSupportMultipleWindows(false);
        web.getSettings().setSupportZoom(false);
        web.setVerticalScrollBarEnabled(false);
        web.setHorizontalScrollBarEnabled(false);
 
        //Our application's main page will be loaded
        web.loadUrl("http://senior.ceng.metu.edu.tr/2009/praeda/");
 
        web.setWebViewClient(new WebViewClient() {
            @Override public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }
        });
    }
}

출처: http://senior.ceng.metu.edu.tr/2009/praeda/2009/01/11/how-to-pure-html-android-application/
Posted by 1010