Difference between revisions of "Android Interface/Native Android GUI"

From Lazarus wiki
Jump to navigationJump to search
m (Fixed syntax highlighting)
 
(18 intermediate revisions by 7 users not shown)
Line 1: Line 1:
 +
{{Platform only|Android|Android|Android}}
 
Go back to [[Android Interface]]
 
Go back to [[Android Interface]]
  
==Android API Hello World in Pascal==
+
__TOC__
 +
==Android4Pascal==
  
Here is an example Pascal application written for Android.
+
See the page about [[Android4Pascal]], which is the bindings between Pascal programs and the Android Java APIs.
  
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:
+
==Manifest configuration==
  
<delphi>
+
===Control the program restart===
program pascalnotes4android;
 
  
{$mode objfpc}{$H+}
+
By default the program will restart on orientation change, on keyboard change, and in a lot of other cases. This is usually unwanted. To disable program restart on keyboard showing and orientation changed add this to the manifest file:
  
uses
+
<syntaxhighlight lang=pascal>
   Classes, androidpipescomm, androidui, javalang;
+
   <activity
 +
    ...
 +
    android:configChanges="orientation|keyboardHidden"
 +
</syntaxhighlight>
  
{$R *.res}
+
See also:
  
var
+
* http://developer.android.com/guide/topics/manifest/activity-element.html#config
  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
+
==Resource files==
  layout := TAbsoluteLayout.Create;
 
  
  tv := TTextView.Create;
+
===Guidelines for the Icons===
  tv.setText('The first Pascal Android application =)');
 
  params := TAbsoluteLayout_LayoutParams.Create(320, 300, 0, 120);
 
  layout.addView(tv, params);
 
  params.Free;
 
  
  et := TEditText.Create;
+
Read here: http://developer.android.com/guide/practices/ui_guidelines/icon_design.html
  et.setText('edit me please');
 
  params := TAbsoluteLayout_LayoutParams.Create(320, 50, 0, 0);
 
  layout.addView(et, params);
 
  params.Free;
 
  
  btn := TButton.Create;
+
==Using other APIs==
  btn.setText('Go!');
 
{  btn.setOnClickListener(buttonClickCallback);}
 
  params := TAbsoluteLayout_LayoutParams.Create(320, 50, 0, 60);
 
  layout.addView(btn, params);
 
  params.Free;
 
  
  Activity.setContentView(layout);
+
===Using the Timer===
  
  // Now tell Java that the initialization has finished
+
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.
  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
+
====Timer example====
  myAndroidPipesComm.MessageLoop();
 
end.
 
</delphi>
 
  
[[Image:Simple Android app.png]]
+
<syntaxhighlight lang=pascal>
 +
program turbochessclock4android;
  
===Compiling the example project in Linux===
+
{$mode objfpc}{$H+}
  
1> Get a working arm-linux cross-compiler which generates ARMv5 eabi with softfloat (as many phones like HTC Wildfire have no FPU)
+
uses
 +
  Classes, SysUtils, androidpipescomm, androidview, javalang,
 +
  androidapp, androidtimer;//, gles11;
  
To do this one can download an unofficial build from here:
+
type
 +
  TEventHandler = class
 +
  public
 +
    procedure HandleOnTimer(ASender: TObject);
 +
    procedure buttonClickCallback(v: TView);
 +
  end;
  
http://members.yline.com/~tom_at_work/fpc-2.4.2.UNOFFICIAL.arm-linux.tar . Compiled for ARMv5 eabi with softfloat.
+
var
 +
  //...
 +
  TimerCount: Integer = 0;
 +
  MyTimer: TAndroidTimer;
 +
  MyEventHandler: TEventHandler;
  
Or build your own. There are instructions here: [[Setup_Cross_Compile_For_ARM]]
+
procedure TEventHandler.buttonClickCallback(v: TView);
 +
begin
 +
  MyTimer.postDelayed(100);
 +
end;
  
2> Install the Android SDK. Instructions here: [[Android_Interface/Using_the_Android_SDK%2C_Emulator_and_Phones#Using_the_Android_SDK]]
+
procedure TEventHandler.HandleOnTimer(ASender: TObject);
 +
begin
 +
  Inc(TimerCount);
 +
  tv.setText(Format('Timer event #%d', [TimerCount]));
 +
  MyTimer.postDelayed(1000);
 +
end;
  
3> Install ant, for example in Mandriva Linux:
+
begin
 
+
   MyEventHandler := TEventHandler.Create;
   urpmi ant
+
   // ...
 
+
end.
4> Open the project PascalNotes4Android/pascalsrc/pascalnotes4android.lpi in Lazarus and build it's build mode Android
+
</syntaxhighlight>
 
 
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%2C_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
+
[[Category:Android]]
  ../android-sdk-linux_x86/tools/adb install bin/PascalNotes4Android-debug.apk
 

Latest revision as of 08:17, 9 February 2020

Android robot.svg

This article applies to Android only.

See also: Multiplatform Programming Guide

Go back to Android Interface

Android4Pascal

See the page about Android4Pascal, which is the bindings between Pascal programs and the Android Java APIs.


Manifest configuration

Control the program restart

By default the program will restart on orientation change, on keyboard change, and in a lot of other cases. This is usually unwanted. To disable program restart on keyboard showing and orientation changed add this to the manifest file:

  <activity
     ...
     android:configChanges="orientation|keyboardHidden"

See also:

Resource files

Guidelines for the Icons

Read here: http://developer.android.com/guide/practices/ui_guidelines/icon_design.html

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

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.