Pages

Tuesday 10 May 2011

Lwuit with android using Netbeans IDE:
If you want  LWUIT code was run an android  just follow the steps
Step 1:
First Create Android Application (  Netbeans Configure with Android  )
 <?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="org.me.list">
    <application>
         <activity android:name=".SearchListActivity" android:label="SearchListActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>
</manifest>
Step 2:
Just Change your android Appliction project AndroidManifest.xml for supporting  Android Devices      
To be able to use the vibrator you have to add the following tag to your
            AndroidManifest.xml file:
            <uses-permission android:name="android.permission.VIBRATE" />
        In some special cases, you may want to bypass restarting of your activity
        based on one or more types of configuration changes. This is done with
        the android:configChanges attribute in its manifest. For any types of
        configuration changes you say that you handle there, you will receive a
        call to your current activity's onConfigurationChanged(Configuration)
        method instead of being restarted. If a configuration change involves any
        that you do not handle, however, the activity will still be restarted and
        onConfigurationChanged(Configuration) will not be called."        
         interesting info on 'sensor' orientation detection (or the lack of ..)
I strongly suggest using the following attribute within the <activity/> tag in the
          AndroidManifext.xml file:
              android:configChanges="keyboardHidden|orientation"
            // http://android-developers.blogspot.com/2009/04/updating-applications-for-on-screen.html
The virtual keyboard behavior can be configured from the AndroidManifest.xml file.  For example:
     <activity name="myactivity"
        android:windowSoftInputMode="adjustResize">
        ...
    </activity>
    This will shrink the LWUIT form once the keyboard appears.  By default the keyboard will be shown on top it seems.
<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"  android:versionCode="1"
          android:versionName="1.0.0"
               package="org.me.list">
    <application>
        <activity android:name=".SearchListActivity " android:screenOrientation="sensor" android:configChanges="keyboardHidden|orientation" android:windowSoftInputMode="stateVisible|adjustResize"  android:label="SearchListActivity ">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
                <uses-permission android:name="android.permission.VIBRATE" />
            </intent-filter>
        </activity>
    </application>
</manifest>
Step 3:
Create Assets( we wanted to add some resources in the assets directory for use an Android Application if you not create assets raise File not found exception)
  1. Create a directory called ‘assets’ at your project root (same dir that AndroidManifest.xml lives)
  2. In /nbproject/project.properties, change
assets.dir=
to
 assets.dir=assets
assets.available=true
asset.dir=assets
  1. In /nbproject/build-impl.xml, there is line in the “if=assets.available” target that reads
or Line 427,443
<arg value="${asset.dir}"/>
that needs to be changed to
<arg value="${assets.dir}"/>
That’s it – you should be all set and “file:///android_asset/” should be accessible to your application and contain the contents of your project’s assets directory.
Step 4:
  1. Fetch LWUIT code from SVN link is: https://svn.java.net/svn/lwuit~svn/trunk
  2. Remove LWUIT classes that use APIs unsupported on Android:
  1. Remove the com.sun.lwuit.impl.midp package.
  2. Remove all 3D stuff (M3G and Transition3d)
  3. Remove SVGImage class.
  4. Remove the com.sun.lwuit.util.Log class.
  5. Remove the com.sun.lwuit.impl.ImplementationFactory class (use the one from this repository).
  6. Add LWUIT with Android supported Classes 
Extend the com.sun.lwuit.impl.android.LWUITActivity class and implement the abstract method that returns the path to your MIDlet class.  sample:
MyActivity.java
                public class MyActivity extends LWUITActivity{
                    public String getFullApplicationPath(){
                        return "com.demo.Sample.MainMIDlet";
                        // (make sure that obfuscators don't mess with the class name and path.)
                    }
                }
MainMIDlet.java
package com.demo.Sample;
import com.sun.lwuit.Display;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
/**
 *
 */
public class MainMIDlet extends MIDlet  implements BaseClass {
    private MainMIDlet midlet = null;
    public MainMIDlet() {
    }
    @Override
    protected void startApp() throws MIDletStateChangeException {
        if(midlet == null){
            midlet = this;
            Display.init(midlet);
            new MyApp(midlet).startMyApp();
        }
    }
    @Override
    protected void pauseApp() {
    }
    @Override
    protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
        this.exitMyApplication();
    }
    public void exitMyApplication() {
        this.notifyDestroyed();
    }
}
BaseClass.java
package com.demo.Sample;
/**
 * implement platform dependent methods.
 */
public interface BaseClass {
    public void exitMyApplication();
}
MyApp.java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.java.lwuit.demo.Sample;
import com.sun.lwuit.Command;
import com.sun.lwuit.Component;
import com.sun.lwuit.Display;
import com.sun.lwuit.Font;
import com.sun.lwuit.Form;
import com.sun.lwuit.Label;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.plaf.Style;
import com.sun.lwuit.plaf.UIManager;
import com.sun.lwuit.util.Resources;
import java.io.IOException;
/**
 *
 * @author Surendran
 */
public class MyApp {
    private final BaseClass baseClass;
    public MyApp(BaseClass baseClass){
        this.baseClass = baseClass;
    }
    public void startMyApp() {
        try {
            Resources r = Resources.open("/androidTheme.res");
            UIManager.getInstance().setThemeProps(r.getTheme(r.getThemeResourceNames()[0]));
            if(Display.getInstance().isTouchScreenDevice()){
                UIManager.getInstance().getLookAndFeel().setTouchMenus(true);
                UIManager.getInstance().getLookAndFeel().setTactileTouchDuration(50);
                Font font = Font.getDefaultFont();
                final int height = font.getHeight()/2;
                Style buttonStyle = UIManager.getInstance().getComponentStyle("SoftButton");
                buttonStyle.setPadding(height, height, buttonStyle.getPadding(Component.LEFT), buttonStyle.getPadding(Component.RIGHT));
                UIManager.getInstance().setComponentStyle("SoftButton", buttonStyle);
            }
            final Form demoForm = new Form();
            demoForm.setScrollable(false);
            demoForm.setLayout(new BorderLayout());
            demoForm.addCommand(new Command("Exit") {
                public void actionPerformed(ActionEvent evt) {
                    baseClass.exitMyApplication();
                }
            });
            demoForm.setTactileTouch(false);
            demoForm.addComponent(BorderLayout.CENTER, new Label("We got it"));
            demoForm.show();
        } catch (IOException ex) {
            ex.printStackTrace();
            baseClass.exitMyApplication();
        }
    }
}
4.Load image,Txt,and other resources to load in Assets
Step 5:
Build Android Appliaction,then Run the Appliaction
Sample Project Link
Note:
While when run android application run on Netbeans IDE before start Android SDK Virtual Devices

No comments:

Post a Comment