Posts Tagged codenameone

Codename One Dynamic Ads Component

Codename One (CN1) really took off with a blast. Although still in beta, but loads of people are already using it for various apps, from banking to a mobile front for this. What is commendable is the ease with with you can build mobile apps that are truly cross platform without having to install tools for each platform.

If you are still wondering what CN1 is, please head to CN1 Website and read up about it for yourself.

I am going to be doing a series of tutorials or useful code snippets for CN1. Today we are looking at the ads component.

To use codename one ads, you need to register and have an account on inner active

Here are the steps to getting your ads on your CN1 app

  1. Create an account on inner active
  2. After login in, click the Add App tab and provide details for your app. The trick is to create different apps for different platforms so that inner active don’t go displaying a download app link on the appstore to blackberry users. You can also profile by age group and/or location.
  3. For every App you added like this, inner active will generate a unique key for you. Key this key.
  4. Now in your code, create an hash table that holds all the keys mapped to the specific platform and populate it. Then write a method as in below to add the ads component to the form.
    public static final Hashtable adKeys = new Hashtable();
    .............
    adKeys.put("rim", "[inner_active_rim_ad_key]);
    adKeys.put("and", "[inner_active_android_ad_key]");
    adKeys.put("me", "[inner_active_others_ad_key]]");
    ......................
    public static void showAds(Form f) {
        Ads ads = new Ads(adKeys.get(Display.getInstance().getPlatformName()).toString());
        if (!f.contains(ads)) {
            f.addComponent(BorderLayout.NORTH, ads);
        }
    }

Now for every page where I want to show the ad, I just call the above method and pass the form instance. Notice however that all my forms are BorderLayout and the North part is reserved for ads.

If you want to however use any type of layout other than border layout, you can use this code to add the ads component to the top of the page

Ads ads = new Ads(Helpers.adKeys.get(Display.getInstance().getPlatformName()).toString());
if (!f.contains(ads)) {
f.addComponent(0, ads);
}

That’s it, you now have Ads. You can visit your inneractive dashboard to see how your App is doing.

Advertisements

Comments (6)

CodenameOne: Another Java Promise Delivered

Back in 2001 when I started reading about this new programming language  called Java, I have only being a programmer for a few months and I see nothing serious about the mantra, “write once, run anywhere“. Don’t blame me, I have just learnt how to make a div jump using javascript and wrote a few arithmetic functions in pascal.

Fast forward to 2003, I have read about C/C++ and I have done more than my fare share of pascal, I never really got the hang of web programming, so I really suck at javascript then, but once again I started reading about Java and this time around something really caught my attention: The book I was reading said “Java can run on anything that has a memory and a microprocessor“. The author said,

soon your fridge will be able to determine when you are low on say milk and automatically make an order using you predefined credit card, soon, you TV will be able to send emails, soon, your credit card will be able to store more information that your account number, and these will be made possible by java.

Now those are pretty big claims to make in 2003, now? not anymore, even though your fridge can’t yet make orders, but the possibility is there and is not really something very difficult to do. But I digress, last week I was installing JRE for a client and Oracle said on the installer that Java runs on 2.5 billion devices! Great numbers right? but no, those numbers meant nothing to me if Java can’t run on iOS. iOS is the biggest mobile operating system out there and powers millions (if not billions) of devices and yet, Java can’t run on them. Those iOS devices are actually in every home right now (maybe not), but the point is Java could run on a gazillion devices, but they meant nothing if it isn’t running where it matters. One can argue that Java powers Android devices, yes, but that doesn’t dispute the fact that iOS devices are actually very important if you are an app developer.

Another place where Java is failing in it’s promises is the write once, run everywhere mantra, no it doesn’t work for mobile devices. There’s no way your can write once and run everywhere in mobile space, you write and write and write and hope and pray it runs on a few devices.

But all that is about to change. Enter Codename One. I don’t even know how to define what Codename One is, all I know is

  1. Now you can write once and run on every mobile platform.
  2. Your Java code can finally be made to run on iOS devices.

You can head to the Codename One Website to learn more about the goodies that it has to offer. I have successfully tested and seen a single code base running on Android (ICS), Blackberry (OS 4.2 and above) and Java ME feature phones (MIDP 2.0 and CLDC 1.0 and above). I didn’t have to make any changes to the codebase, and believe me the UI looks pretty cool.

Development is also easy, all you need is Netbeans. It comes with a GUI builder and a Simulator. One thing you will notice again is that the Simulator is fast. Very fast! Have you tried to develop for Android and Blackberry? Then you will notice that the Codename One Simulator is order of magnitude fast.

If you’ve been following the mobile app development space for a while, you would have heard of LWUIT, Codename One is built on LWUIT but is far more than that, the GUI Builder generates one of the most efficient code I have seen a GUI Builder generate. It throws away most of the fluff GUI Builders add to your code. It also builds your GUI as a state machine, such that you can think of your application as a series of states.

I can go on and on.

Codename One is currently in private beta and I believe beta is not far out (I am not speaking on behalf of the developers, but based on my perceived success of the private beta). I recommend you give it a try, you’ll be amazed.

Below are some sample screen shots from the application I built using Codename One private beta

twitpic.com/8nc5if yfrog.com/kio3ivp yfrog.com/oessyqp yfrog.com/kjm7rgp

And here is a live application

J2ME and RIM: http://www.getjar.com/mwc12

Android: https://market.android.com/details?id=com.israelmobilesummit.mwcevents&feature=search_result#?t=W251bGwsMSwxLDEsImNvbS5pc3JhZWxtb2JpbGVzdW1taXQubXdjZXZlbnRzIl0.

iOS: http://itunes.apple.com/us/app/mobile-world-congress-events/id497801040?ls=1&mt=8

Comments (2)