Android Interface/Native Android GUI

From Lazarus wiki
Revision as of 15:15, 15 February 2011 by Sekelsenmat (talk | contribs)
Jump to navigationJump to search

Go back to Android Interface

Android API Hello World in Pascal

Here is an example Pascal application written for Android.

The full directory structure can be download with this svn command:

 svn co https://p-tools.svn.sourceforge.net/svnroot/p-tools/PascalNotes4Android PascalNotes4Android

Here is the Pascal code from this example:

<delphi> program pascalnotes4android;

{$mode objfpc}{$H+}

uses

 Classes, androidpipescomm, androidui, javalang;

{$R *.res}

var

 layout: TAbsoluteLayout;
 params: TAbsoluteLayout_LayoutParams;
 tv: TTextView;
 et: TTextView;
 btn: TButton;

begin

 // Here add any initialization.
 // Any initialization code will be run inside Activity.onCreate,
 // so keep it as short as possible!
 // It should mostly contain GUI initialization
 // User interface
 // Prepares the UI of the program
 layout := TAbsoluteLayout.Create;
 tv := TTextView.Create;
 tv.setText('The first Pascal Android application =)');
 params := TAbsoluteLayout_LayoutParams.Create(320, 300, 0, 120);
 layout.addView(tv, params);
 params.Free;
 et := TEditText.Create;
 et.setText('edit me please');
 params := TAbsoluteLayout_LayoutParams.Create(320, 50, 0, 0);
 layout.addView(et, params);
 params.Free;
 btn := TButton.Create;
 btn.setText('Go!');

{ btn.setOnClickListener(buttonClickCallback);}

 params := TAbsoluteLayout_LayoutParams.Create(320, 50, 0, 60);
 layout.addView(btn, params);
 params.Free;
 Activity.setContentView(layout);
 // Now tell Java that the initialization has finished
 myAndroidPipesComm := TAndroidPipesComm.Create;
 myAndroidPipesComm.InitializationFinished();
 // Here you can add any other initialization,
 // specially non-GUI code
 // Now we block our execution waiting for callbacks from Java
 myAndroidPipesComm.MessageLoop();

end. </delphi>

Simple Android app.png

Compiling the example project in Linux

1> Get a working arm-linux cross-compiler which generates ARMv5 eabi with softfloat (as many phones like HTC Wildfire have no FPU)

To do this one can download an unofficial build from here:

http://members.yline.com/~tom_at_work/fpc-2.4.2.UNOFFICIAL.arm-linux.tar . Compiled for ARMv5 eabi with softfloat.

Or build your own. There are instructions here: Setup_Cross_Compile_For_ARM

2> Install the Android SDK. Instructions here: Android_Interface/Using_the_Android_SDK,_Emulator_and_Phones#Using_the_Android_SDK

3> Install ant, for example in Mandriva Linux:

 urpmi ant

4> Open the project PascalNotes4Android/pascalsrc/pascalnotes4android.lpi in Lazarus and build it's build mode Android

5> Copy the generate executable

 cp pascalsrc/pascalnotes4android libs/armeabi/libpascalnotes4android.so 

if the directory libs/armeabi doesn't exist, create it

6> Build the APK file in debug mode

 ant debug

7> Connect your phone and make sure you can connect to it via ADB. More info here: Android_Interface/Using_the_Android_SDK,_Emulator_and_Phones#Recognition_of_devices_under_Linux

8> Install the APK file in your phone via ADB or whatever other method you prefer:

 ../android-sdk-linux_x86/tools/adb install bin/PascalNotes4Android-debug.apk

If the package is already installed you need need to do this instead:

 ../android-sdk-linux_x86/tools/adb uninstall com.pascalnotes
 ../android-sdk-linux_x86/tools/adb install bin/PascalNotes4Android-debug.apk

Using other APIs

Using the Timer

The Android API bindings include a handy timer control called TAndroidTimer. It works just like a Runnable, and inside it a Handler class is utilized to run the Runnable in the main GUI thread so that event executed in this timer can call Android APIs.

Timer example

<delphi> program turbochessclock4android;

{$mode objfpc}{$H+}

uses

 Classes, SysUtils, androidpipescomm, androidview, javalang,
 androidapp, androidtimer;//, gles11;

type

 TEventHandler = class
 public
   procedure HandleOnTimer(ASender: TObject);
   procedure buttonClickCallback(v: TView);
 end;

var

 //...
 TimerCount: Integer = 0;
 MyTimer: TAndroidTimer;
 MyEventHandler: TEventHandler;

procedure TEventHandler.buttonClickCallback(v: TView); begin

 MyTimer.postDelayed(100);

end;

procedure TEventHandler.HandleOnTimer(ASender: TObject); begin

 Inc(TimerCount);
 tv.setText(Format('Timer event #%d', [TimerCount]));
 MyTimer.postDelayed(1000);

end;

begin

 MyEventHandler := TEventHandler.Create;
 // ...

end. </delphi>