Thursday, November 10, 2011

Holy Cow, I Wrote An App !

(Of course the title refers to Raymond Chens post about the only book he ever wrote..)


In 15 lines of code... And it's an unique one. And it does something that is probably useful to (a maximum of) 10 people in the whole world.
Last year I bought an Archos 7HT Android Tablet. It was one of the cheapest tablets then and this means it has limited capabilities. One of the limitations is that it does not have an option to switch to portrait mode. It has no tilt sensor and there is no special button or built-in option for it. So if you display a webpage it is always in landscape mode which is sometimes inconvenient. Like when I want to view the a popular site that shows when and where it will rain in the coming hours. As I live in a small country, it could fit in one screen IF displayed in portrait mode.
So I decided to look into the possibility to force the standard browser to start in portrait mode, which is (as far as I'm aware) not possible. Then I found a reference to the 'WebView' ("A View that displays web pages") component and guessed that this could be a way to solve the problem.  And it is. I just created a standard Android application, followed the instructions on the WebView reference page to place the component on the main page, and forced the view to portrait using the appropriate function (that I found on StackOverflow).. That's all. Here is the code:

package CScope.Buienradar;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.webkit.WebView;

public class BuienRadarActivity extends Activity {
    /** Called when the activity is first created. */
     WebView mWebView;
     Button myButton;
     @Override
     public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mWebView = (WebView) findViewById(R.id.webview);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.loadUrl("http://buienradar.mobi");
           setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
    }

The blue lines are actually the only code written by me, all the rest was generated by the Eclipse 'New Project' wizard. And when you press Run or Build a complete installation package is created in the 'bin' folder of the project that can be copied to the device.  Let's face it, this is almost too simple.... So that even left me time to create a fancy icon.


(And if you by coincidence are one of these 10 people: you can download it it from here: BuienRadar,apk)