Pages

Showing posts with label Android with Netbeans. Show all posts
Showing posts with label Android with Netbeans. Show all posts

Tuesday, 10 May 2011

J2me run an android using Netbeans IDE



If you want  J2ME code was run an android  just follow the steps

Step 1:
First Create Android Application (  Netbeans Configure with Android  )

Step 2:


    <?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>


 
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 ..)
             http://androidguys.com/?p=2914
              
              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
4.      <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.Download Code from  following Link4Source copy  that files and load to the Src directory
2.Find Src\org\meteoroid\test\ and load ur J2ME MIDlet File and supported Files
Chage ActivityName,Package name in AndroidManifest.xml
3.Set MIDlet File location on assets\META-INF\MANIFEST-MF change
MIDlet-1: MIDlet_Title,/icon.png,org.App.test.MainMidlet
MIDlet-2: Canvas,/icon.png,org.App.test.TestMIDlet2
.
.
.
4.Load image,Txt,and other resources to load in Assets
Step 5:
Build Android Appliaction,then Run the Appliaction

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

  Android Configure with NetBeans : (http://wiki.netbeans.org/IntroAndroidDevNetBeans)
Step 1:
Netbeans ==>MeunBar==>Tools==>Plugins==>Settings(Tab)==>Click Add Button

 
2.Update Center Customizer
Name : nbandroid or any name…
3.Another Alternative Method for to add android Plugin .Just Download latest AndroidPlugin(.nbm files) from  http://kenai.com/projects/nbandroid/downloads
1.Tools==>Plugins==>Downloaded(Tab)==>Add plugins==>Browse The latest Android Plugin(.nbm files)==>Install
Step 2:
  1. Plugins==>Available Plugins(Tab)==>type Android in Search Box
  2. Then Click Android CheckBox ==>Install
 
 
Step 3:  
Netbeans==>File==>Open Project
Step 4:
1.Enter Project Name, Set Package Name
2.Android Platform hasn’t been Specified, So first set Android SDK Platform.
3.Click Mange Platform or ToolsรจJava Platform
4.JavaPlatform Addplatform  
5.Select Google Android Handheld Platform
6.Browse Android SDK Loaction(E.g: D:\Software\Android\Android_SDK_2_2\Android SDK)
 7.Final Set Android Platform Name: Android or any Name….
8.If  you like add no of android platform on different names.
Step 5:
Sample Project List Download Link
New Project Structure Flow:
The file SearchListActivity.java is as shown below:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.me.list;
import android.app.Activity;
import android.os.Bundle;
/**
 *
 * @author Surendran
 */
public class SearchListActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        // ToDo add your GUI initialization code here  
       
    }
}
 An Android application is organized as a set of activities. The file SearchListActivity.java that has been generated is the searchlist activity class for this application.

Whenever run your android Appliction in Netbeans before just create  the  preferred AVD
Then run your application otherwise at the runtime you got server not found error
1.Select Android SDK Manager Loacation
2.Select Virtual Devices then create New Android Virtual Device
  1. Select whatever platform level you need maximum select low level because your App’s  run on old android devices
  1. Click Start for launch  Android Virtual device

Running your project
Now, build and Run your project as you would do with any other NetBeans project. Your project builds successfully and the Android emulator boots up: 
Where is our Hello World?
Just add
setContentView(R.layout.main);
to the above file in place of
// ToDo add your GUI initialization code here
This should have been generated by the plugin, because the main.xml containing the main view was generated.

Alternatively we can do the following.
We shall now add support to the skeleton code to display the text- Hello, Android. Add the following to the above file in place of the "// ToDo add your GUI initialization code here":
TextView tv = new TextView(this);
tv.setText("Hello, Android");
setContentView(tv);
You will notice that the IDE complains- "Cannot find symbol". This is because we need to import the class TextView. Its easy with NetBeans. Just click on the yellow bulb and select "Add import for.."
The file should now look like:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.me.list;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
 /**
 *
 * @author Surendran
 */
public class SearchListActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
       
       TextView tv = new TextView(this);
       tv.setText("Hello, Android");
       setContentView(tv);
//setContentView(R.layout.main);
//output:Hello Android from Netbeans
    }
}
Now, run the project again

Prevent Selection of Target on Each Run 
When you run your project you are given a dialog asking which android device or emulator you would like to use. This can get annoying after several compile/run iterations. To set a default target, right click on the project name, select Properties. Select Run. Click on Automatic and set the preferred AVD. If you do not set the preferred AVD, NetBeans will continue to ask you at each run instance.