Just An Application

March 1, 2010

MIDP 3.0 On Android: A TabbedPane And Some Lists

Filed under: Android, Java, JME, LCDUI, MIDP, MIDP3 — Tags: , , , , , , — Simon Lewis @ 11:59 am

I first wrote about running MIDlets on Android a bit over two years ago (see here). At that point there was one fairly fundamental obstacle to running MIDlets on Android which I didn’t mention at the time, which was the presence in Android of stubbed out versions of some javax.lcdui classes. As there were also no publicly available functioning ClassLoaders the only way to get a MIDlet using LCDUI to run was to modify it at the source level to use a shadow version of LCDUI. That is what I did, but it is also why I didn’t do anything more.

Since then the mysterious javax.lcdui class stubs have disappeared and publicly available ClassLoaders have appeared. Although these implementation level obstacles have been removed, there are still some impedance mismatches between MIDP and Android. At a high-level there are differences in the provisioning, security, and application models. At a very low-level there is the use of Dex and Dalvik. At an intermediate level there is, for example, the contrast between the use of static resources to build Android application UIs and the more or less dynamic construction of UI elements using LCDUI.

Having said that, if you have a 99.9% pure Java MIDP 3 implementation, which happily I do, it is possible to get MIDP 3 MIDlets running on Android 2.1, albeit with a judicious amount of hacking,

This is a very simple MIDlet which uses the new MIDP 3 TabbedPane


import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.TabbedPane;

import javax.microedition.midlet.MIDlet;

public final class SimpleTabbedPaneMIDlet
                   extends
                       MIDlet
                   implements
                       CommandListener
{
    public void destroyApp(boolean ignore)
    {
    }
	
    public void startApp()
    {
        List exclusive = new List(
                                 "Exclusive", 
                                 Choice.EXCLUSIVE, 
                                 new String[] { "One", "Two", "Three", "Four", "Five" }, 
                                 null);
        List implicit   = new List(
                                  "Implicit",  
                                  Choice.IMPLICIT,  
                                  new String[] { "Foo", "Bar", "Baz" }, 
                                  null);
        List multiple  = new List(
                                  "Multiple",  
                                  Choice.MULTIPLE,  
                                  new String[] { "Up", "Down", "Left", "Right" }, 
                                  null);
		
       exclusive.setCommandListener(this);
       implicit.setCommandListener(this);
       multiple.setCommandListener(this);
		
       TabbedPane tp = new TabbedPane("TabbedPane", true, false);
		
       tp.addTab(exclusive, null);
       tp.addTab(implicit, null);
       tp.addTab(multiple, null);
       Display.getDisplays(0)[0].setCurrent(tp);
    }
	
    //
	
    public void commandAction(Command theCommand, Displayable theDisplayable)
    {
        System.out.println("SimpleTabbedPaneMIDlet.commandAction(" + theCommand + ", " + theDisplayable + ")" );
    }
}


and this is what it looks like running on the emulator.



This MIDlet illustrates quite nicely the difference between constructing a UI in Android and in MIDP. At first glance it looks as though the class

       android.widget.TabHost

is a straight correlate of

    javax.microedition.lcdui.TabbedPane

However, if you simply construct an instance of TabHost programatically using the one arg constructor, and then and start trying to add tabs to it, it doesn’t work. You get some helpful exceptions, and once you’ve worked out how to fix those, a very unhelpful NullPointerException, at which point there is nothing for it except to resort to the source code of TabHost.

It turns out that it is possible to create a functioning instance of TabHost programatically, see above, but the expectation is that it will actually be created by inflating it from a layout resource, which is fair enough, it is certainly a lot easier to do it that way.


Copyright (c) 2010 By Simon Lewis. All Rights Reserved.

Advertisements

6 Comments »

  1. […] again, this time running on the N97 emulator. The only difference between this and the Android version is that the LCDUI backend is implemented using vanilla Qt rather than Android […]

    Pingback by MIDP 3.0 On Symbian: A TabbedPane And Some Lists « Just An Application — March 17, 2010 @ 9:02 am

  2. […] JME, Maemo, Maemo5, Maemo5Qt, MIDP, MIDP3, Qt — Simon Lewis @ 1:43 pm It’s that MIDlet again. This time running on the Maemo5 emulator. The implementation code is identical to the […]

    Pingback by MIDP 3.0 On Maemo 5: A TabbedPane And Some Lists … Again « Just An Application — March 18, 2010 @ 1:43 pm

  3. Hi Simon,
    Reading that took me back to the Symbian Ltd days!
    >>if you have a 99.9% pure Java MIDP 3 implementation,
    Does that mean you install this as a Java library and use the classloaders in order to get the midleta to load the library classes?
    Vinod

    Comment by Vinod — October 18, 2010 @ 10:24 pm

    • Hi Vinod,

      >Does that mean you install this as a Java library and use the classloaders in order
      >to get the midleta to load the library classes?

      You need an entry point, i.e, an Activity or Service, for Android to run since it doesn’t
      understand MIDlets by default, so you need a stub Android Application or Applications
      as well as the library

      Simon

      Comment by Simon Lewis — October 19, 2010 @ 5:55 am

  4. it is not worjking in android 3.o can you tell me why ?

    Comment by ashish — March 13, 2012 @ 3:50 pm

    • Not sure what you are asking. What isn’t working ?

      simon

      Comment by Simon Lewis — March 13, 2012 @ 9:31 pm


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: